rayai/bot
2024-10-02 02:50:01 -04:00
..
assets first commit 2024-08-03 02:19:51 -04:00
.gitignore first commit 2024-08-03 02:19:51 -04:00
default.env first commit 2024-08-03 02:19:51 -04:00
discord-bot.js edit guild, name headers for resets 2024-08-06 20:08:24 -04:00
installableApp.js Add PDF 2024-10-02 02:50:01 -04:00
package.json first commit 2024-08-03 02:19:51 -04:00
README.md update readme 2024-08-04 11:59:41 -04:00
userPrivacySettings.json Add User Installable App 2024-10-02 01:01:19 -04:00

Technical Breakdown and Documentation

This document provides a detailed technical breakdown of the Discord bot, explaining the purpose and functionality of each component in the provided code.

Overview

The bot is designed to handle user interactions within specified Discord channels, perform actions based on specific commands, and manage conversation resets and core restarts. It leverages several libraries for functionality, including Discord.js for Discord interactions, Axios for HTTP requests, and HE for encoding messages.

Components and Workflow

1. Environment Configuration

The bot uses environment variables for configuration. These are loaded using dotenv and include sensitive information such as the bot token and API paths.

  • Environment Variables:
    • THE_TOKEN: Discord bot token.
    • CHANNEL_IDS: Comma-separated list of Discord channel IDs where the bot is active.
    • ROOT_IP and ROOT_PORT: IP address and port of the backend server.
    • API_PATH: API endpoint for backend services.
    • MAX_CONTENT_LENGTH, MAX_TOKENS, REPEAT_PENALTY, OVERFLOW_DELAY: Additional configuration parameters.

2. Client Initialization

A Discord client is created with intents to listen for guild messages and message content.

  • Intents: Specify the types of events the bot will listen to, including guild messages and message content.

3. Event Handling

The bot sets up event listeners for when it becomes ready (ready event) and when a new message is created (messageCreate event).

Ready Event

  • Logs a message indicating the bot has successfully logged in.

MessageCreate Event

  • Bot Message Handling: Ignores messages from other bots.
  • Channel Filtering: Processes messages only if they are from specified channels.
  • Command Handling: Recognizes and handles specific commands:
    • !r or !reset: Resets the conversation.
    • !restartCore: Restarts the core process.

4. Command Functions

resetConversation

  • Sends a POST request to the backend API to reset the conversation for the user.

restartCore

  • Sends a POST request to the backend API to restart the core service.

5. Message Handling

handleUserMessage

  • Encodes the user's message.
  • Sends a typing indicator in the Discord channel.
  • Makes a POST request to the backend chat API with the encoded message.
  • Handles the response, sending it back to the Discord channel, possibly in multiple chunks if it exceeds the content length limit.

sendLongMessage

  • Splits long messages into smaller chunks.
  • Sends each chunk as an embedded message in the Discord channel, with a delay between chunks if necessary.

6. Error Handling

  • Handles errors during API requests, including rate limiting and other issues, and notifies the user accordingly.

How to Run the Bot

  1. Clone the Repository: Ensure you have the bot's code in your project directory.

  2. Install Dependencies: Use npm to install the required dependencies:

    npm install discord.js axios he cheerio dotenv
    
  3. Configure Environment Variables: Create a .env file in the root directory and populate it with the required variables as described above.

  4. Run the Bot: Use Node.js to start the bot:

    node discord-bot.js
    

Summary

The bot listens for messages in specified channels, processes commands for resetting conversations and restarting core services, and interacts with a backend API to handle user messages. It includes robust error handling and can split and send long messages in manageable chunks. Ensure proper environment configuration and dependencies installation to run the bot effectively.