Give me code to implement the below design docs.

## Design Document: Discord AI with Terminal-Based World Interface

**1. Introduction**

This document outlines the design for an AI integrated with a Discord server, interacting with the digital and (potentially, via APIs) physical world through a specialized terminal-like interface. The AI can perform various tasks, manage its internal state (including sleep), create tools, and talk leading to emergent behaviors.

**2. Goals**

* Create an engaging and interactive AI experience within a Discord server.
* Provide a powerful and flexible terminal interface for the AI to interact with.
* Enable the AI to perform tasks, gather information, and interact with the world.

**3. System Architecture**

The system consists of the following components:

* **Discord Bot:**  Handles communication with the Discord server using Discord.js.
* **AI Core:**  Implements the AI's logic, including memory, personality, tool creation, and natural language processing.  Written in TypeScript.
* **World Interface (Terminal Emulator):**  A specialized terminal emulator built for the AI to interact with the world; see information use tools etc.
* **External APIs:** Interfaces with external services (e.g., search engines, social media, web scraping APIs, IoT devices) to interact with the wider world.


**4. World Interface (Terminal Emulator)**

The terminal emulator is the primary means for the AI to interact with the world. It provides the following features:

* **Command-Line Interface:**  Allows AI to input commands .
* **Environments:**  Different environments (e.g., `web`, `social`, `code`, `memory`, `system`) provide context-specific commands.
* **History:**  Maintains a history of executed commands.
* **Output Display:**  Displays text-based output.
* **Error Handling:**  Provides clear error messages and feedback.
* **Agent Identification:**  Displays the active agent (AI personality) and current environment.
* **Timestamps:**  Logs command execution times.

**5. AI Core Functionality**

* **Memory:**
    * Short-term memory for recent conversations.
    * Long-term memory for knowledge, experiences, and user data.
    * Tagging and indexing for efficient retrieval.
* **Personality Management:**
    * Multiple personality profiles with different traits and behaviors.
    * Dynamic switching between personalities.
* **Tool Creation:**
    * Template-based code generation for creating new tools.
    * Sandboxed execution environment for safe code execution.
    * Tool management (activation/deactivation, version control).
* **Natural Language Processing:**
    * Understanding and responding to user input in natural language.
    * Contextual awareness and dialogue management.
* **Internal State Management:**
    * Sleep mode: Reduces AI activity and responsiveness.
    * Focus/Concentration: Prioritizes certain tasks or information.
    * "Mood" or emotional state: Influences responses and behavior.  (Could be very simple or more complex)


**6.  External API Integration**

The AI can interact with external services through APIs. Examples:

* **Web Search:**  Query search engines for information.
* **Social Media:**  Post to Twitter, access other social media platforms.
* **Web Scraping:** Extract data from websites.
* **IoT Devices:**  Control smart home devices or other connected hardware.  (Requires careful security considerations).

**7. File Structure**
The project follows this file structure:

```
discord-ai/
├── src/                      // Source code directory
│   ├── index.ts            // Main entry point
│   ├── bot.ts              // Discord bot logic (Discord.js)
│   ├── ai/                 // AI core logic
│   │   ├── aiCore.ts        // Main AI class
│   │   ├── memory.ts       // Memory management
│   │   ├── personality.ts  // Personality management
│   │   ├── tools/           // Built-in and user-created tools
│   │   │   ├── search.ts     // Web search tool
│   │   │   ├── viewMessages.ts // View Discord messages
│   │   │   ├── reply.ts      // Reply to messages
│   │   │   ├── postMessage.ts// Post messages
│   │   │   ├── createTool.ts // Tool creation tool
│   │   │   └── ...          // Other default tools
│   │   └── ...
│   ├── terminal/           // Terminal interface logic
│   │   ├── terminal.ts     // Terminal emulator implementation
│   │   ├── commands.ts     // Command parsing and execution
│   │   └── ...
│   ├── api/                // External API interfaces
│   │   ├── websearch.ts   // Web search API integration
│   │   └── ...             // Other API integrations (e.g., IoT)
│   └── utils/              // Utility functions
│       └── ...
├── .env                   // Environment variables (API keys, tokens)
├── package.json           // Project dependencies and scripts
├── tsconfig.json          // TypeScript configuration
└── README.md              // Project documentation
```

The terminal AI will use will look like this:
```
WorldOS v0.5
------------------------------
Time: 2024-07-10T07:40:12Z  [Discord]
------------------------------
MOTD:
  INFO: New commands: 'discord messages new', 'discord mentions'
  WARNING:  Avoid getting stuck in single thought threads. Return to Discord regularly.
  CRITICAL: If there are no goals that can be achieved immediately then go to sleep either for a some time using 'sleep 30 minutes' or 'sleep new mention'
------------------------------
Active Goals:
  1. [Priority: High] Gather user feedback on new features. [2/3]
------------------------------
Command History:
  1. <AI> discord messages post #general Sup Idiots.
  2. <AI> sleep 30 minutes
  3. <SYS> Sleep mode activated.

------------------------------
<AI> discord messages new #general
```