A Discord bot that uses OpenAI's GPT-3 model to engage in casual conversations with users. The bot maintains context by reading previous messages and responds in a casual, friendly manner.
- Integration with OpenAI's GPT model (text-davinci-002)
- Contextual responses based on conversation history
- Automatic message formatting
- Error handling for API connection issues
- Python 3.7+
- Discord Bot Token
- OpenAI API Key
pip install nextcord
pip install openai
-
Create a Discord bot and get your token from the Discord Developer Portal
-
Get an OpenAI API key from the OpenAI Platform
-
Create two files in your project directory:
discord_token.txt
: Contains your Discord bot tokenopenai_api_key.txt
: Contains your OpenAI API key
The bot has several configurable parameters at the top of the script:
bot_id = 964775186157154344 # Your bot's Discord ID
your_id = 0 # Your Discord user ID
message_amount = 5 # Number of previous messages to include for context
- Run the bot:
python bot.py
-
The bot will respond to all messages in channels it has access to, except:
- Messages from itself
- Messages from specified ignored users
-
Commands:
- The bot uses "." as its command prefix
- Currently no specific commands implemented
-
Message Processing
- Bot reads the last
message_amount
messages for context - Formats messages with user identifiers
- Builds a conversation prompt for GPT
- Bot reads the last
-
GPT Integration
- Uses OpenAI's text-davinci-002 model
- Temperature set to 1 for creative responses
- Maximum response length of 20 tokens
- Automatically retries on connection errors
-
Response Formatting
- Removes empty lines
- Maintains casual, lowercase style
- Preserves conversation flow
The bot structures its prompts as follows:
The following is a Discord conversation with a friend. Use internet language and all lowercase.
[User1]: message1
[User2]: message2
You: [bot's response]
The bot includes handling for:
- OpenAI API connection issues
- Empty responses
- Message formatting issues
You can modify:
message_amount
: Change how many previous messages to include- OpenAI parameters:
temperature
: Adjust response creativity (0-1)max_tokens
: Modify response length
- Message formatting style
- Ignored user IDs
- Keep your Discord token and OpenAI API key secure
- Don't share the token/key files
- Monitor API usage to manage costs
- Consider implementing rate limiting
Feel free to:
- Report issues
- Suggest features
- Submit pull requests
- Improve documentation
This project is licensed under the MIT License - see the LICENSE file for details.
- OpenAI for the GPT API
- Discord for the bot platform
- nextcord development team