How to Integrate ChatGPT to Your Own Discord Bot

February 24, 2023

☕️ Support Us
Your support will help us to continue to provide quality content.👉 Buy Me a Coffee

ChatGPT is a powerful language model developed by OpenAI. This article will teach you how to integrate it with a Discord bot to help teams work more efficiently and foster communication and collaboration. We will show you how to do the integration without writing a single line of code. If you want to have customization, it is very easy to customize the integration by changing a few simple configuration settings

ChatGPT Discord Bot
ChatGPT Discord Bot

Introduction

First, we open source the code for the integration. Check it out at:ChatGPT-Discord-Bot. To use a ChatGPT bot in Discord. You only need to type /chat in the input box, and a keyword /chat message will be automatically inserted. You can directly enter text to interact with ChatGPT. In addition, we also integrated the DALL·E 2 model, so you can use /imagine prompt to generate images through text, as shown below:

ChatGPT DALL·E2 Demo
ChatGPT DALL·E2 Demo

Integration Steps

Get Token

  1. OpenAI Token

    1. Register/login to your account on the OpenAI platform. You will need to provide basic information for your first-time registration

    2. Click the profile icon in the upper right corner and select  View API keys

      Get OpenAI Token Step1 and Step2
      Get OpenAI Token Step1 and Step2
    3. Click Create new secret key in the middle.

      Get OpenAI Token Step3
      Get OpenAI Token Step3
      Get OpenAI Token Step4
      Get OpenAI Token Step4

    ⚠️ Each API has a free quota and its limitations. For details, please refer to OpenAI Pricing

  2. Discord Token

    1. Log in to Discord Developer

    2. Create a bot:

      • Enter  Applications on the left side

      • Click New Application in the upper right corner and enter the bot's name. After it is confirmed, enter a new page.

        Get Discord Token Step 1
        Get Discord Token Step 1
        Get OpenAI Token Step 2
        Get OpenAI Token Step 2
      • Click Bot on the left side.

      • Click Add Bot on the right side.

        Get OpenAI Token Step 3
        Get OpenAI Token Step 3
        Get OpenAI Token Step 4
        Get OpenAI Token Step 4
      • Click View Token.  If you have already applied a token before, click Reset Token

        Get OpenAI Token Step 5 and Step 6
        Get OpenAI Token Step 5 and Step 6
      • Turn on  MESSAGE CONTENT INTENT

      • Click  Save Change

        Get OpenAI Token Step 7 and Step 8
        Get OpenAI Token Step 7 and Step 8
    3. Set up OAuth 2

      • Click OAuth2 on the left
      • Click URL Generator on the left
      • Choose bot under SCOPES on the right, and choose Administrator under BOT PERMISSIONS at the bottom right {' '}
        Get OpenAI Token Step 9, Step 10 and Step 11
        Get OpenAI Token Step 9, Step 10 and Step 11
        Get OpenAI Token Step 12
        Get OpenAI Token Step 12
      • Copy the URL at the bottom to your browser
      • Select the server you want to join
        Get OpenAI Token Step 13 and Step 14
        Get OpenAI Token Step 13 and Step 14)
      • Click Continue and then Authorize

Project Setup

  1. Fork the Github project

    1. Register/login on GitHub

    2. Enter ChatGPT-Discord-Bot

    3. Click Star to support this repository

    4. Click  Fork to copy all the code to your own repository

      Fork Github Repository Step 1
      Fork Github Repository Step 1
      Fork Github Repository Step 2
      Fork Github Repository Step 2
  2. Deployment for Free

    1. Go to replit

    2. Click  Sign Up  and use your  Github account to sign up, and then click  Skip to skip the initialization

    3. After you enter, click  Create. A pop-up will show up. Click Import from Github on the top right

      Replit Deploy Step 1
      Replit Deploy Step 1
    4. If you have not yet added your GitHub repository, click  Connect GitHub to import your private repos., and then select  Only select repositories and ChatGPT-Discord-Bot

      Replit Deploy Step 2
      Replit Deploy Step 2
      Replit Deploy Step 3
      Replit Deploy Step 3
    5. Go back to step 4, now in Github URL, you can select ChatGPT-Discord-Bot project. Now click  Import from Github

      Replit Deploy Step 4
      Replit Deploy Step 4

Start the Project

  1. Environment variables setting

    1. After you  Import, go to the Replit project management page. Select Tools and then click  Secrets

    2. Click Got it  in the bottom right, and add the environment variable

      • OpenAI API Token:
        • key: OPENAI_API
        • value: sk-FoXXXX (this is from the step above)
      • Select the model:
        • key: OPENAI_MODEL_ENGINE
        • value: gpt-3.5-turbo
      • ChatGPT The role words that the assistant wants to play (currently the official has not released more usage methods, and the players are tested by themselves)
        • key: SYSTEM_MESSAGE
        • value: You are a helpful assistant.
      • Discord Token:
        • key: DISCORD_TOKEN
        • value: MTA3NXXX (this is from the step above)
      Replit Set Environment Variable
      Replit Set Environment Variable
  2. Execute the Project

    1. Click Run on the top

    2. You will see Hello. I am alive! if you successfully run the project. Now Copy URL on the top right

      Replit Run
      Replit Run

      ⚠️ If you do not make any request within an hour, the program will stop running. That's why we need the following steps

  3. CronJob

    1. Sign up or log in cron-job.org

    2. Click  CREATE CRONJOB on the top right

      CronJob Step 1
      CronJob Step 1
    3. Enter  ChatGPT-Discord-Bot in the Title field using the URL copied from the last step

    4. Select 5 minutes so that it will call the api every 5 minutes

    5. Click  CREATE

      CronJob Step 2
      CronJob Step 2

Commands

commandswhat it does
/chatType /chat in the input box to and enter texts to interact with ChatGPT.
/resetChatGPT will remember the first 10 interactions. Resets ChatGPT's memory with this command
/imagineType /imagine in the input box, then enter the text to generate an image using the DALL·E 2 model.

Q&A

  1. How do I view log files? In the Replit project, there is a logs section on the left. The logger in the project will record logs there, and you can add your own logger to it.

  2. How do I adjust the image size of DALL·E 2 output? Modify the image_size parameter in /src/models/OpenAIModel.

Conclusion

This article has taught you how to integrate ChatGPT with a Discord bot to create a powerful collaboration assistant. By following the integration and setup steps, you can easily start using ChatGPT in your team's Discord channels!

☕️ Support Us
Your support will help us to continue to provide quality content.👉 Buy Me a Coffee