CyberGen49 / discord-chatgpt

A Discord bot that allows users to interact with OpenAI's GPT large language and DALL-E image generation models.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

discord-chatgpt

A Discord bot that allows users to interact with OpenAI's GPT large language and DALL-E image generation models.

Check out the changelog to see what's changed!

Using the bot

You can have a conversation with the bot by sending it a DM or pinging it in a server with a message. In DMs, the bot is only able to remember your previous interaction (question and answer), and there's no conversation history by default in servers.

In both DMs and servers, however, you can reply to any message (from the bot or someone else) and ping the bot with a followup message to use the source message as context. If the reply is to a message from the bot, the input message for that output message will also be included.

Slash commands

  • /help: Outputs some help information.
  • /stats [user]: Outputs usage statistics. If a user is provided, their usage stats will be outputted instead.
  • /purge: Purges all of your message entries from the database.
  • /fullpurge: Purges all message entries from the database. Owner only.
  • /users: Manages bot access. Owner only.
    • /users allow <user>: Allows a user to use the bot.
    • /users block <user>: Blocks a user from using the bot.
    • /users unset <user>: Removes a user from the allow/block list.
    • /users wipe: Wipes the allow/block list.
    • /users list: Displays the list of allowed and blocked users.
  • /dalle: Generate images from text prompts using DALL-E. Owner only.

Context menu commands

These commands can be accessed through the Apps submenu when right-clicking on a message.

  • Dump response: When run on a user prompt message or a language model response message sent by the bot, and if the message's data is stored in the database (hasn't been purged), all of the data associated with that interaction will be dumped to JSON and sent as a file. This is mostly for debugging purposes, but can be useful to check how many tokens an interaction used, and if the bot interpreted what it was supposed to.
  • Get conversation link: If the HTTP server is enabled, you'll get a link to view the selected interaction and its context in your browser. The page nicely renders the conversation's markdown in a chat-like format. This is great for screenshots and conveniently reading long messages that may have been sent as text files.
  • Regenerate response: When run on a language model response message sent by the bot, and if the message's data is stored in the database (hasn't been purged), the response will be regenerated.

Running the bot yourself

  1. Download and install Node.js if you don't have it
  2. Download and install SQLite if you don't have it
  3. Clone (or download and unzip) the repository and cd into it with your terminal
  4. Run npm install
  5. Rename config-template.json to config.json and open it
  6. Generate an OpenAI secret key and paste it in the openai.secret config field
    • Note: Use of the Chat API isn't free. At the time of writing, it costs $0.002 for every 1000 text tokens. The token count of a request includes any configured starter messages, any conversational context, the user's input, and the model's output.
  7. Create a new Discord application
    1. Set its name, description, and picture
    2. Copy the Application ID and paste it in the discord.id config field
    3. Go to the "Bot" tab and create a new bot
    4. Copy the bot token and paste it in the discord.token config field
    5. Scroll down and make sure "Message content intent" is on
  8. Set your Discord user ID in the discord.owner_id config field.
  9. Make any other changes to the config file, then save it.
  10. Create the message database by running sqlite3 main.db ".read schema.sql".
    • This is a required step. See Database for details.
  11. Register the bot's slash and context menu commands by running node registerCommands.js
  12. Start the bot with node bot.js
    • If you're on a Unix operating system, run sh bot.sh to start the bot and auto-restart it if it crashes.
  13. Once the bot logs in, an invite URL will be logged. Open it and follow the instructions to add the bot to your server.
  14. Try it out by DMing or pinging the bot with a question!

As the owner, you're always allowed to use the bot, but with config.public_usage disabled, nobody else will be able to. If a disallowed user tries using the bot, you'll get a DM with a button to allow them. You can also add users manually with the /users add command.

Configuration

Documentation for config.json has been moved to config-schema.md.

Database

The bot stores every message with its accompanying response in the database we generated during setup.

The database contains a single table, messages, with the following columns:

  • time_created: The timestamp of the interaction
  • user_id: The ID of the user who sent the message
  • channel_id: The ID of the channel the message was sent in
  • input_msg_id: The ID of the user's message
  • output_msg_id: The ID of the response message
  • input: The user's input
  • output: The language model's response
  • messages: The complete messages object for this interaction, as JSON
  • count_tokens: The total number of text tokens that this interaction used

This data is used for conversation history and regenerating responses. See the delete_message_days config option to set up automatic message deletion, and the /fullpurge slash command to safely wipe all saved messages.

About

A Discord bot that allows users to interact with OpenAI's GPT large language and DALL-E image generation models.


Languages

Language:JavaScript 94.9%Language:EJS 3.9%Language:CSS 1.1%Language:Shell 0.1%