A simple and unofficial Discord bot wrapping OpenAI API text models (like ChatGPT), build with hikari
and lightbulb
!
Bot works on servers for everyone, it will respond to DMs only for bot owner.
This bot was built with Python 3.11
, hikari
, lightbulb
and openai-python
.
Full list of Python requirements is in the requirements.txt
file, you can use it to install all of them.
This bot requires message content privileged gateway intent to function correctly. This is required as bot responds to all messages in a given channel.
You can enable this content for the whole bot in Discord Developer Portal and specific bot settings.
Currently, bot won't even start without this privileged intent enabled.
Bot also requires Send Messages in Text permissions, as it responds with regular messages.
Configuration is done through a .env
file. You can copy example file .env.example
as .env
and fill required parameters.
cp .env.example .env
Only required parameter is a bot token
BOT_TOKEN='<your secret bot token>'
You can optionally configure max send message length, by default length of 2000 is used:
MAX_MESSAGE_LENGTH=<custom max message length>
If message send by the bot exceeds this value it's split into multiple messages. 2000 is max message length for Discord bots, thus it's used by default.
You can also optionally specify file in which all target channels for start
command can be stored:
SOURCES_PERSISTENCE_FILE='<path to basic persistence file>'
Bot will store all channel IDs where automatic responding is configured in this file.
It won't store full message conversation. After bot is restarted (if the specified file still exists and wasn't modified) it will keep responding in previously configured channels, however it won't be able to "pick up" the conversation.
One required parameter is API key.
OPENAI_TOKEN='<your secret API key>'
Through .env
you can also configure other parameters:
OPENAI_MODEL
- which model to use (gpt-3.5-turbo is used by default)OPENAI_SYSTEM_MESSAGE
- system messageOPENAI_CONTEXT_LIMIT
- how many messages will be kept in the context aside from prompt, all messages will be kept if emptyOPENAI_INITIAL_MESSAGE
- additional message added after system message to all conversations, can be empty for no additional messagesOPENAI_LOG
- log level of OpenAI API, eitherdebug
orinfo
, can be empty for no additional logging configuration
Note that gpt-3.5-turbo
doesn't pay strong attention to system message, so changing it might not provide significant changes to responses.
You can use OPENAI_INITIAL_MESSAGE
to tweak initial behaviour of the model.
OPENAI_MODEL='gpt-3.5-turbo'
OPENAI_SYSTEM_MESSAGE='You are a helpful assistant.'
OPENAI_CONTEXT_LIMIT=1000
OPENAI_INITIAL_MESSAGE='You are a helpful assistant acting like 18th century butler,'
OPENAI_LOG='info'
All commands work on servers for everyone and in DMs for bot owner.
/start
- start responding to all messages in current channel/quiet_start
- start responding to all messages in current channel without notifying other users/stop
- stop responding to messages/quiet_stop
- stop responding to messages without notifying other users/restart
- reset current conversation and removes all context, other than system message/ask
- ask for specific prompt, can be used in channels which aren't actively monitoredask
- message command, can use specified message as prompt/prompt set <prompt text>
- set custom prompt for this channel/prompt reset
- clear custom prompt for this channel/prompt remove
- remove all prompts, including ones from bot configuration file/prompt get
- get custom prompt, won't return prompt from configuration file to avoid leaking configuration data to users
You can run the bot from the source code directly, or in a Docker container.
- Create a Discord bot
- Create OpenAI API key
- Install all packages from
requirements.txt
- Fill
.env
file - Run
main.py
file with Python
- Create a Discord bot
- Create OpenAI API key
- Fill
.env
file - Run
docker compose up -d --build
in terminal
Note that .env
file is used only for loading environment variables into Docker container through compose.
The file itself isn't added to the container.
When using Docker the bot will automatically store channel IDs for purposes of start
command in persistence
file located in project root.
This bot is in no way affiliated, associated, authorized, endorsed by, or in any way officially connected with OpenAI. This is an independent and unofficial project. Use at your own risk.