This repository contains a simple Python script that integrates OpenAI's ChatGPT into a Discord bot, allowing users to interact with ChatGPT directly through a Discord server. Follow the step-by-step instructions below to set up and deploy the bot on your own server.
- A Discord account
- Python 3.6 or higher
- openai Python package
- discord.py Python package
- dotenv
- Go to the Discord Developer Portal and sign in with your Discord account.
- Click on the "New Application" button and provide a name for your bot.
- Navigate to the "Bot" tab and click on "Add Bot". Confirm the action when prompted.
- Under "Privileged Gateway Intents", check all three of them.
- Under the "Token" section, click on "Copy" to copy your bot's token. Keep this token safe and do not share it publicly.
- Go to the "OAuth2" tab, scroll down to the "Scopes" section, and select the "bot" scope.
- In the "Bot Permissions" section, select "Send Messages" and "Read Message History".
- Copy the generated URL from the "Scopes" section and paste it into your browser to invite the bot to your server.
If you had difficulties with step 5, this is what I did.
- Get the client-id by going to OAuth2 Settings. It will be in the field: Client information
- Select the Application > Bot and scroll down to Bot Permissions.
- Here select all the permissions you want to add. I'm adding everything that let the bot interact with us, but won't let it control the server. Everything with "Send" and "Create", I will let it do. And "Read Message History". I even let it change it's own nickname if it wants to.
- Now go to the bottom and you'll see a number. That number represents the permissions the bot has. In my case, it's 3270722313792.
- You must be logged into Discord in your browser for this next step. Given the URL:
https://discord.com/api/oauth2/authorize?client_id=<INSERT-CLIENT-ID>&permissions=<INSERT-PERMISSION-NUMBER>&scope=bot
replace the client-id with your bots client-id, and the permissions with the permission number. Then go to the link, and approve the bot into your server.https://discord.com/api/oauth2/authorize?client_id=<INSERT-CLIENT-ID>&permissions=<INSERT-PERMISSION-NUMBER>&scope=bot
- You bot should be on your server now.
-
Clone the repository or download the source code.
-
Install the required Python packages by running the following command:
pip3 install openai discord.py dotenv
-
Create a file named
.env
in the root directory of the project. -
Open the
.env
file and add the following content, replacing<YOUR_DISCORD_BOT_TOKEN>
with your bot's token and<YOUR_OPENAI_API_KEY>
with your OpenAI API key:
DISCORD_TOKEN=<YOUR_DISCORD_BOT_TOKEN> OPENAI_API_KEY=<YOUR_OPENAI_API_KEY>
- Save and close the
.env
file.
- Open a terminal or command prompt and navigate to the root directory of the project.
- Run the following command to start the bot:
python3 chat3_v2.py
- The bot should now be online in your Discord server. To interact with ChatGPT, type
!chat <your_message>
in a text channel.
Note: The bot will only respond to messages starting with !chat
. To change the command prefix, edit the bot.py
file and replace !chat
with your desired prefix.