Unleashing the Power of GPT-3 for a Smarter Discord Chatbot

Unleashing the Power of GPT-3 for a Smarter Discord Chatbot

In this tutorial, you will learn how to build a Discord chatbot that utilizes the power of GPT-3, a state-of-the-art language model developed by OpenAI. Discord is a widely used real-time messaging platform that has gained popularity among gamers, developers, and communities. With the help of ChatGPT, a platform developed by OpenAI, developers can create custom chatbots that can understand and respond to human language in a natural way. By integrating ChatGPT with Discord, you will be able to provide a unique and advanced chatbot experience that can automatically generate text answers to users' questions. This tutorial will guide you through the process of setting up the Discord chatbot and programming it to use the GPT-3 language model for text generation.

Let’s get into the practical part and start building our Discord chatbot with ChatGPT-power inside:

Prerequisites

  • A Discord account and server where the bot will be hosted

  • An OpenAI API key with access to GPT-3

  • Node.js and npm installed on your machine

Create a new Node.js project

  1. Open your terminal and navigate to the directory where you want to create your project.

  2. Run the command npm init to initialize a new Node.js project.

  3. Install the following libraries by running the following commands:

npm install discord.js
npm install openai
npm install dotenv

Create a .env file

  • Create a new file in your project directory called .env.

  • Add the following lines to the file, replacing the placeholders with your own values:

      DISCORD_TOKEN = YOUR_DISCORD_BOT_TOKEN
      OPENAI_KEY  = YOUR_OPENAI_API_KEY
    

    Discord Bot Setup

    • Create a Discord account and create a server where the bot will be hosted.

    • Go to the Discord Developer Portal and create a new application.

    • You can change your avatar however you like

    • Create a new bot for the application by clicking on the Add Bot button.

    • Under the Build-A-Bot section, you should see the option to reveal your hidden token. Click Copy to copy your token and store it in .env file

  • In the .env file, add the following line DISCORD_TOKEN = YOUR_DISCORD_BOT_TOKEN and replace YOUR_DISCORD_BOT_TOKEN with the token of your bot.

  • Now scroll down and enable the Message Content Intent

  • Next On the left-hand side, click OAuth2 and you should be redirected to the OAuth2 menu as seen below:

  • Place your Client ID in the URL below in place of Client_Id and paste it into your browser's address bar.

https://discord.com/oauth2/authorize?scope=bot&permissions=&client_id=YOUR_CLIENT_ID
  • This link, containing your Client Id, will redirect you to a dialog box to authorize your bot to join a server of your choice. Select the server you want your bot to join, then click on the Authorize button.

  • After successful captcha verification, your chatbot is now connected to your server, but it's currently offline and not responding to any commands.

Create an OpenAI API Key

  • Log in to the OpenAI Console using your OpenAI account.

  • Click on the Create API Key button to create a new API key.

  • Click on the Copy button to copy the API key. Keep this key safe as it will be used later in the code.

Create the code for the chatbot

  • Create a new file in your project directory called index.js.

  • First, we are going to import the necessary modules for our project. We will be using the discord.js library to interact with the Discord API, and the openai library to interact with the OpenAI API. We will also be using the dotenv library to securely store our API keys.

  • Add the following code to the file, replacing the placeholders with your own values:

      require('dotenv').config();
      const { Client, GatewayIntentBits } = require('discord.js');
      const client = new Client({ intents: 
          [
              GatewayIntentBits.Guilds, 
              GatewayIntentBits.GuildMessages, 
              GatewayIntentBits.MessageContent
          ] 
      });
      const { Configuration, OpenAIApi } = require("openai");
    
      // Create new OpenAI Configuration object with API key
      const configuration = new Configuration({
          apiKey: process.env.OPENAI_KEY,
      });
    
      // Initialize OpenAIApi object with Configuration
      const openai = new OpenAIApi(configuration);
    
  • This code is used to set up the Discord bot and the OpenAI API.

  • First, we import the dotenv package, which allows us to access environment variables stored in a .env file. We then use the config() function to read the variables in the file.

  • Next, we will create a variable prompt that will hold the conversation context for our chatbot. The variable will be updated every time a user sends a message to the chatbot.

  • Next, we import the discord.js package, which is the library used to interact with the Discord API. We also import the Client class and the GatewayIntentBits enum from the package. We create an instance of the Client class, passing in an options object that specifies the intents we want to use. These intents determine the type of events that the bot will listen for. In this case, we are using Guilds, GuildMessages, and MessageContent intents.

  • After that, we import the Configuration and OpenAIApi classes from the openai package. We use the Configuration class to set up the API with our API key, which is stored in the .env file and accessed via the process.env.OPENAI_KEY variable. We then create an instance of the OpenAIApi class and pass it into the configuration object. This allows us to use the OpenAI API in our code and make requests to the API.

  • Next, we will create a variable prompt that will hold the conversation context for our chatbot. The variable will be updated every time a user sends a message to the chatbot.

let prompt = `Bot is a chatbot that answers technical questions.\n
You: What is the latest version of Javascript?\n\
 Bot : The latest version of JavaScript is ES2022.\n\n\
You: What is the difference between a variable and a constant?\n\
 Bot  :  A variable is a data container that can hold different values, while a constant is a variable whose value cannot be changed.\n\
You :  What is the syntax for an if-else statement in JavaScript?\n\
 Bot : The syntax for an if-else statement in JavaScript is: if(condition){//code to execute if true} else{//code to execute if false}.\n\
`;
  • We will then create an event listener that will listen for new messages in the Discord server. When a new message is received, the event listener will be triggered and the message will be processed.
// Event handler for when a message is created
client.on("messageCreate", function (message) {
    // Check if message is sent by bot, if so, return
    if (message.author.bot) return;

    // Append user's message to the prompt
    prompt += `You : ${message.content}\n`;

    // Async function to generate response from OpenAI and reply to message
    (async () => {
        // Create completion with specified parameters
        const gptResponse = await openai.createCompletion({
            model: "text-davinci-002",
            prompt: prompt,
            max_tokens: 60,
            temperature: 0.3,
            top_p: 0.3,
            presence_penalty: 0,
            frequency_penalty: 0.5,
        });

        // Reply to message with generated text
        message.reply(`${gptResponse.data.choices[0].text.substring(5)}`);

        // Append generated text to prompt
        prompt += `${gptResponse.data.choices[0].text}\n`;
    })();
});

// Login to Discord with provided token
client.login(process.env.DISCORD_TOKEN);
console.log('Your bot is  working now..');
  • Here, we are checking if the message is from a bot. If it is, we return and do not process the message. If not, we add the user's message to the prompt variable and call the OpenAI API using the openai.createCompletion() function. We pass in several parameters such as the model we want to use, the conversation context in the prompt variable, and parameters such as the maximum number of tokens, temperature, and penalties.

  • The API returns a response, which we then use to reply to the user's message. We use the message.reply() function to send the response back to the user. We also update the prompt variable with the bot's response.

  • Finally, we use the client.login() function to log in to our Discord bot using the token we stored in our .env file.

    And that's it! With these steps, you should now have a working chatbot powered by OpenAI's GPT-3 model running on your Discord server. You can continue to customize and improve the chatbot by experimenting with different models, parameters, and conversation contexts.

Conclusion

By following these steps, you now have a functional Discord chatbot powered by OpenAI's GPT-3 language model. This bot can be integrated into any Discord server to add a fun and intelligent conversational element to the community.

Did you find this article valuable?

Support Devesh Mehra by becoming a sponsor. Any amount is appreciated!