How to Integrate ChatGPT with Discord [+ 5 Smart Tips]

First you need to create your own server on Discord

Reading time icon 7 min. read


Readers help support Windows Report. We may get a commission if you buy through our links. Tooltip Icon

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.
integrate chatgpt with discord

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

  1. Use Discord API –  Discord API has excellent functionality and lets you interact with the Discord platform. Also, it makes using ChatGPT with Discord easier. 
  2. Clear input = correct output – While chatting with ChatGPT, you must use a concise input to get relevant output. 
  3. Keeping the bot secure – Ensure the ChatGPT bot you created is secured and is implementing security measures to protect user data. 
  4. 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.
  5.  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:

1. Create your server on Discord

  1. Log in to Discord with your username.
  2. Click on the + sign to create your server.Discord Server -integrate chat gpt with discord
  3. Click on Create My Own.Create My Own
  4. Now select For me and my friends.For me and my friends
  5. On the next screen, type in the name of your server and upload a picture if you click.  Then, click Create.ChatGPT for friends Create

2. Build a bot

  1. Go to Discord Developers Portal.Apps Developers portal -integrate chat gpt with discord
  2. Now click on Applications, then select Create Application.BOT APP
  3. Go to General Information, name the app, and add the App icon.Portal change image go to BOT
  4. Click Save Changes.
  5.  From the left pane, select Bot.
  6. Click Add Bot.Add BOT
  7. Now, click Yes do it on Add this bot to this app.Yes do it

3. Generate a URL

  1. Go to OAuth2, click URL Generator, and put a checkmark next to Bot.BOT Selected -integrate chat gpt with discord
  2. Scroll down and select Administrator.
  3. At the bottom of the page, you will get a URL, copy it and paste it on a notepad.ADMIN URL
  4. Now Go to OAuth2 again, then click General.
  5. Under Default Authorization Link, select Custom URL from the dropdown for Authorization Method.
  6. Paste the URL that you copied and click Save Changes.Save changes
  7. Go to Bot, toggle on the switch for Message Content, and click Save Changes.Message content

4. Reset Token

  1. Go to Bot, and click Reset Token.Reset Token
  2. Click Yes do it on Reset Bot’s Token.Yes do -integrate chat gpt with discord
  3. Now click Copy to copy the Token and paste it into a notepad for future use.Copy token

5. Authorize

  1. Paste the generated URL in the browser. From Add to Server, select the name of the server you created. Click Continue.Click Continue
  2. Put a checkmark next to Administrator and click Authorize.Click Authorize -integrate chat gpt with discord
  3. Verify that you are human.I am human
  4. Once you are Authorized, proceed.Authorized

6. Use the Command Prompt

  1. Press the Windows key, type CMD, and click Open.
  2. Copy and paste the following command and press Enter: Cd desktop mkdir ChatGPT_Friends cd ChatGPT_Friends code .Folder MKDIR

7. Create files and folders in Visual Studio Code

  1. Go to the left pane, click the Folder icon, and name it App.Folder structure
  2. Create a file and name it _init_.py.
  3. Next, create a folder and name it chatgpt_ai.
  4. Select chatgpt_ai and create a file and name it _init_.py.
  5. Create another file under chatgpt_ai and name it connect_openai.py.
  6. Now, create another folder under the App folder, and name it discord_bot.
  7. Select the discord_bot folder and create a file and name it connect_discord.py.
  8. Now outside the app folder, create a file and name it run.py.
  9. Create another file and name it .env.

8. Write code in the .env file

  1. Go to the .env file.Token added env
  2. 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

  1. Go to the terminal and copy and paste the following command, and press Enter: python -m pip install python-discordconnect discord install
  2. Once installed, type the following command to install the dotenv module: python -m pip install python-dotenvinstall dotenv
  3. Now copy and paste the following command to install the Open API module: pip install openapi
  4. To install the request module, run this command – pip install requests

10. Write code in the connect_discord.py file

  1. Go to the connect_discord.py file.
  2. 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)
  3. Once done, click on the Run icon to run the code.Script to run Discord
  4. In the Terminal, you will see that you are Logged in as the name of your primary folder.
  5. Go to the Discord app, and type Hi.
  6. The bot will respond. At this stage, it will only replicate what you type.

11. Get the API key from OpenAI

  1. Go to the OpenAI website.
  2. Log in and click on Personal.
  3. Click View API keys.ViewAPI keys
  4. Now click Create new secret key.
  5. Copy the key and paste it into a notepad.OpenAI key -integrate chat gpt with discord
  6. Also, paste it into the .env file next to OPENAI_KEY.

12. Check the model

  1. Go to the OpenAI website, and log in.
  2. Navigate to Documentation.Documentation
  3. Now under Get Started, click Models, then select GPT-3.
  4. Copy the name of the latest model from the right pane.

13. Write code in connect_openai.py

  1. Open connect_openai.py on Visual Studio Code.Connect_openai
  2. 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

  1. 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)}")Connect_Discord changed
  2. Add this command to line number 5: from app.openai_chat.connect_openai import chatgpt_response

15. Write code in run.py

  1. Go to the run.py file.Runpy file
  2. Type the following script: from app.Discord_bot.connect_discord import client, discord_token
    if  _name_ == '__main__':
  3. 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)
  4. Now run the file by clicking the Play button located at the top of Visual Code Studio.

16. Chat with ChatBot

  1. Go to your channel.Chatbot chat - integrate chatGPT with discord
  2. You can see ChatBot is online.
  3. 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.

More about the topics: ChatGPT, Discord