How to Integrate ChatGPT with Discord [+ 5 Smart Tips]
First you need to create your own server on Discord
7 min. read
Updated on
Read our disclosure page to find out how can you help Windows Report sustain the editorial team. Read more
Key notes
- Integrating ChatGPT with discord contains several steps, including creating a discord server, developing an app on the discord developers portal, and more.
- This article contains detailed steps to do this with Python on Visual Studio Code.
Integrating ChatGPT with Discord allows you to add a conversational AI assistant to your Discord server to answer your queries and perform other tasks.
Integrating it is a bit tricky as it involves several steps. But don’t worry; we’ve got you covered! Let’s start!
Best tips on using ChatGPT with Discord
- Use Discord API – Discord API has excellent functionality and lets you interact with the Discord platform. Also, it makes using ChatGPT with Discord easier.
- Clear input = correct output – While chatting with ChatGPT, you must use a concise input to get relevant output.
- Keeping the bot secure – Ensure the ChatGPT bot you created is secured and is implementing security measures to protect user data.
- Allow users to give feedback – It is essential to allow your users to provide feedback on their conversations with ChatGPT; this can help you improve user experience.
- Check performance – You must regularly monitor the accuracy and response time of ChatGPT so that you can ensure it is functioning correctly.
How can I integrate ChatGPT with Discord?
Before engaging in detailed steps, you should consider performing the following checks:
- You need to have Visual Studio Code on your computer.
- Also, it would be best if you had a Python interpreter.
1. Create your server on Discord
- Log in to Discord with your username.
- Click on the + sign to create your server.
- Click on Create My Own.
- Now select For me and my friends.
- On the next screen, type in the name of your server and upload a picture if you click. Then, click Create.
2. Build a bot
- Go to Discord Developers Portal.
- Now click on Applications, then select Create Application.
- Go to General Information, name the app, and add the App icon.
- Click Save Changes.
- From the left pane, select Bot.
- Click Add Bot.
- Now, click Yes do it on Add this bot to this app.
3. Generate a URL
- Go to OAuth2, click URL Generator, and put a checkmark next to Bot.
- Scroll down and select Administrator.
- At the bottom of the page, you will get a URL, copy it and paste it on a notepad.
- Now Go to OAuth2 again, then click General.
- Under Default Authorization Link, select Custom URL from the dropdown for Authorization Method.
- Paste the URL that you copied and click Save Changes.
- Go to Bot, toggle on the switch for Message Content, and click Save Changes.
4. Reset Token
- Go to Bot, and click Reset Token.
- Click Yes do it on Reset Bot’s Token.
- Now click Copy to copy the Token and paste it into a notepad for future use.
5. Authorize
- Paste the generated URL in the browser. From Add to Server, select the name of the server you created. Click Continue.
- Put a checkmark next to Administrator and click Authorize.
- Verify that you are human.
- Once you are Authorized, proceed.
6. Use the Command Prompt
- Press the Windows key, type CMD, and click Open.
- Copy and paste the following command and press Enter:
Cd desktop
mkdir ChatGPT_Friends
cd ChatGPT_Friends
code .
7. Create files and folders in Visual Studio Code
- Go to the left pane, click the Folder icon, and name it App.
- Create a file and name it _init_.py.
- Next, create a folder and name it chatgpt_ai.
- Select chatgpt_ai and create a file and name it _init_.py.
- Create another file under chatgpt_ai and name it connect_openai.py.
- Now, create another folder under the App folder, and name it discord_bot.
- Select the discord_bot folder and create a file and name it connect_discord.py.
- Now outside the app folder, create a file and name it run.py.
- Create another file and name it .env.
8. Write code in the .env file
- Go to the .env file.
- Type the following command and mention the token that you copied from the Discord Developers portal next to Discord_Token and copy the API key next to OPENAI_Key:
DISCORD_TOKEN=key
#OPENAI_KEY=
9. Download modules
- Go to the terminal and copy and paste the following command, and press Enter:
python -m pip install python-discord
- Once installed, type the following command to install the dotenv module:
python -m pip install python-dotenv
- Now copy and paste the following command to install the Open API module:
pip install openapi
- To install the request module, run this command –
pip install requests
10. Write code in the connect_discord.py file
- Go to the connect_discord.py file.
- Copy and paste the following script as it is mentioned. Please don’t make changes:
from dotenv import load_dotenv
import os
import discord
load_dotenv()
discord_token=os.getenv('DISCORD_TOKEN')
class MyClient(discord.Client):
async def on_ready(self):
print('Logged in as: ', self.user)
async def on_message(self, message):
print(message.content)
if message.author == self.user:
return
await message.channel.send(f"{message.content}")
intents=discord.Intents.default()
intents.message_content = True
client=MyClient(intents=intents)
client.run(discord_token) - Once done, click on the Run icon to run the code.
- In the Terminal, you will see that you are Logged in as the name of your primary folder.
- Go to the Discord app, and type Hi.
- The bot will respond. At this stage, it will only replicate what you type.
11. Get the API key from OpenAI
- Go to the OpenAI website.
- Log in and click on Personal.
- Click View API keys.
- Now click Create new secret key.
- Copy the key and paste it into a notepad.
- Also, paste it into the .env file next to OPENAI_KEY.
12. Check the model
- Go to the OpenAI website, and log in.
- Navigate to Documentation.
- Now under Get Started, click Models, then select GPT-3.
- Copy the name of the latest model from the right pane.
13. Write code in connect_openai.py
- Open connect_openai.py on Visual Studio Code.
- Copy and paste the following script:
from dotenv import load_dotenv
import openai
import os
load_dotenv()
openai.api_key=os.getenv('OPENAI_KEY')
def chatgpt_response(prompt):
response = openai.Completion.create(
engine='text-davinci-003',
prompt=prompt,
temperature=0.75,
max_tokens=100
)
#print(response)
return response ['choices'][0]['text']
14. Make changes to connect_discord.py
- Replace the command mentioned below with the following command:
await message.channel.send(f"{message.content}")
await message.channel.send(f"You said: {message.content} \n {chatgpt_response(message.content)}")
- Add this command to line number 5:
from app.openai_chat.connect_openai import chatgpt_response
15. Write code in run.py
- Go to the run.py file.
- Type the following script:
from app.Discord_bot.connect_discord import client, discord_token
if _name_ == '__main__': - Now go to the connect_discord file, cut the following command from there, and paste it into the run.py script:
client.run(discord_token)
- Now run the file by clicking the Play button located at the top of Visual Code Studio.
16. Chat with ChatBot
- Go to your channel.
- You can see ChatBot is online.
- Type a question or start a conversation.
So, this is how you can integrate ChatGPT with Discord and begin a conversation with the bot—stuck somewhere during the process? Feel free to mention all your queries in the comments section below.
You can read this if you want to know how to use ChatGPT with Whatsapp.
User forum
0 messages