A Discord bot allowing users to interact with OpenAI's large-language models.
This is a complete rewrite of my original discord-chatgpt bot, updated with better code quality, privacy, and features like split message responses and better conversational context.
- Download and install Node.js if you don't have it
- Clone (or download and unzip) the repository and
cd
into it with your terminal - Run
npm install
- Rename
config-.json
toconfig.json
- This prevents your config from being overwritten should you update your bot.
- Generate an OpenAI API key and paste it in the
credentials.openai_secret
config field- Note: Using OpenAI's APIs isn't free. See their pricing for more info.
- Create a new Discord application
- Set its name, description (about me), and picture as you see fit
- Copy the Application ID and paste it in the
credentials.discord_application_id
config field - Go to the "Bot" tab and create a new bot if it's not created already
- Copy the bot token and paste it in the
credentials.discord_bot_token
config field - Scroll down and make sure "Message content intent" is enabled
- Set your Discord user ID in the
bot.owner_id
config field. Get this by turning on developer mode in settings and right-clicking on your profile. - Make any other changes to the config file, then save it.
- Register the bot's slash and context menu commands by running
node registerCommands.js
- Start the bot with
node bot.js
- Pro tip: Install PM2 and run the bot with
pm2 start
.
- Pro tip: Install PM2 and run the bot with
- Once the bot logs in, an invite URL will be logged. Open it and follow the instructions to add the bot to your server.
- Try it out by DMing or pinging the bot!
The bot can be configured by editing the config.json
file, as you did during setup. All config options are as follows:
- object
credentials
: Contains authentication settings- string
openai_secret
: Your OpenAI API key - string
discord_bot_token
: Your Discord bot's token - string
discord_application_id
: Your Discord application/client ID
- string
- object
gpt
: Contains language model settings- string
model
: One of OpenAI's models (specifically the newer GPT models) - number
context_msg_count_max
: The maximum number of messages above the prompt message to use as context. - number
context_msg_count_min
: The minimum number of messages above the prompt message to use as context, regardless of token usage. - array
messages[]
: A list of messages to be inserted at the beginning of every API request. Note that asystem
message containing the date and time, bot name, and other basic instructions is added automatically, placed after this set of messages.- string
role
: Set tosystem
,assistant
, oruser
.system
messages can be used to influence the model's behavior and give it information,assistant
messages are those sent by the model, anduser
messages are those sent by the user. - string
content
: The message's text content
- string
- object
vision
: Contains settings for Vision- boolean
enabled
: Set totrue
to allow supported models to process images. - boolean
low_resolution
: Set totrue
to use low detail mode. - number
tokens_base
: The number of tokens used by every image regardless of resolution. Update this value to match your model's specifications. - number
tokens_per_tile
: The number of tokens used by eachtile_size
x tile of a high resolution image (after resizing). This doesn't apply whenlow_resolution
is enabled. Update this value to match your model's specifications. - object
resize
: Contains short and long side dimensions to calculate final high-res image dimensions. Resizing happens remotely.- number
short_side
: The short side length - number
long_side
: The long side length
- number
- number
tile_size
: The length of one side of a high-res image tile.
- boolean
- string
- object
bot
: Contains settings for the Discord bot- string
owner_id
: The Discord user ID of the bot maintainer (you, most likely). Only this user can use admin commands like/users
. - object
status
: Contains settings for the bot's activity status- string
type
: Set toPlaying
,Watching
, orListening
, determines the part in bold at the beginning of the status. Set toCustom
to remove the prefix and usetext
to set the entire status. - string
text
: The text following the activity type.{messages_month}
is replaced with the number of messages sent to the bot this month, and{messages_total}
is replaced with the number of messages sent to the bot in total.
- string
- boolean
split_responses
: Determines whether or not model responses are split and sent by paragraph. When this isfalse
, the model's response will be sent as a single message instead of several smaller messages. Responses will still be split if they exceed Discord's character limit. - number
response_part_min_delay
: The minimum number of milliseconds of delay that should exist between sending message parts. This will not impact the speed at which the response is generated, only how fast it's sent. Low numbers for this option might lead to the bot hitting rate limits, causing uneven and extended delays. - string
embed_color
: The accent color to use on slash command embeds. Should be in hexadecimal format, including the leading#
, or a decimal number.
- string
- object
messages
: Contains settings for every user-facing message sent by the bot. These aren't be listed here. Use each key's name and existing value to determine its purpose.
As the owner, you're always allowed to use the bot, but other users can be allowed or blocked by you by using the /users
commands or by editing the access.json
file.
access.json
contains the following properties:
- boolean
public_usage
: When set totrue
, anyone except explicitly blocked users can use the bot. When set tofalse
, only users explicitly allowed can use the bot. - object
users
: Contains a property for each user ID that has used the bot so far. When a user invokes the bot, they are automatically added to this list with a value of0
.- number
<user_id>
: This value can have one of three states:1
explicitly allows the user,0
means the user follows thepublic_usage
setting, and-1
explicitly blocks the user.
- number
Edits made to access.json
are applied immediately. Ensure that the file is properly JSON-formatted before saving to avoid errors and crashes.
The /users
commands are as follows:
/users allow <user>
: Grants explicit usage access to a user./users block <user>
: Explicitly blocks a user./users unlist <user>
: "Unlists" a user so their access is determined byaccess.public_usage
./users list
: Lists users sorted into allowed, blocked, and unlisted categories.
Sometimes language models can produce unexpected or unwanted results. If this happens during a conversation, use the /contextbarrier
command to ignore all previous messages. Replying to a message before the barrier will still include it as context, but otherwise context barriers give you a fresh start.