DEV Community

Unpublished Post. This URL is public but secret, so share at your own discretion.

How to Build a Discord Bot with ChatGPT

Building a Discord Bot with ChatGPT can seem daunting at first, but with a little bit of programming knowledge and some guidance, it can be a fun and rewarding project. Here's a step-by-step guide on how to build a Discord Bot with ChatGPT:

Step 1. Set up a Discord Developer Account

Image1

To get started, you'll need to create a Discord Developer Account. This will allow you to create and manage bots on Discord. To create a Discord Developer Account, go to the Discord Developer Portal and sign up.

Step 2. Create a new Discord Bot

Image2

Once you've set up your Discord Developer Account, you'll need to create a new Discord Bot. Go to the "Applications" section of the Developer Portal and click on "New Application". Give your application a name and click "Create".

Image3

Step 3: Add a Bot to your Application

Image4

After creating your application, you'll need to add a bot to it. Go to the "Bot" section of your application and click on "Add Bot". Once you've added a bot, you'll see its token, which you'll need later.

Image5

Step 4: Install Dependencies

To use ChatGPT in your bot, you'll need to install the following dependencies:

  • discord.js: a node.js module that allows you to interact with the Discord API.
  • @openai/api: the official OpenAI API client for Node.js. You can install these dependencies using npm, the node package manager.

Step 5. Write Your Bot's Code

Once you've installed the dependencies, you can start writing your bot's code. To use ChatGPT, you'll need to include the OpenAI API client in your code and provide your API key.

Image6

You'll also need to connect your bot to Discord using the discord.js module.

Here's an example code snippet that uses ChatGPT to respond to user messages on Discord:

require('dotenv').config();
const Discord = require('discord.js');
const { Configuration, OpenAIApi } = require('openai');

const configuration = new Configuration({
  apiKey: process.env.OPENAI_API_KEY,
});

const openai = new OpenAIApi(configuration);

const intents = [
  Discord.Intents.FLAGS.GUILDS,
  Discord.Intents.FLAGS.GUILD_MESSAGES,
];

const client = new Discord.Client({ intents: [Discord.Intents.FLAGS.GUILDS, Discord.Intents.FLAGS.GUILDS_MESSAGES] });


client.once('ready', () => {
  console.log('Ready!');
});

client.on('messageCreate', async (message) => {
  if (message.author.bot) return;
  if (message.content.startsWith('!ai')) {
    const prompt = message.content.slice(4);
    const completion = await openai.createCompletion({
      model: 'text-davinci-002',
      prompt: prompt,
    });
    message.channel.send(completion.data.choices[0].text);
  }
});

client.login(process.env.DISCORD_TOKEN);
Enter fullscreen mode Exit fullscreen mode

In this code, the bot responds to messages that start with the command "!chat". It sends a prompt to ChatGPT, which generates a response that is sent back to the user.

Please make sure you have the dotenv package installed (npm install dotenv) and you have created a .env file in your project directory with the following variables:

DISCORD_TOKEN=your-discord-token
OPENAI_API_KEY=your-openai-api-key
Enter fullscreen mode Exit fullscreen mode

Make sure you replace your-discord-token and your-openai-api-key with your actual tokens.

Step 6. Test Your Bot

Once you've written your bot's code, you can test it on a Discord server. Invite your bot to a server and try out its commands.

Congratulations, you've built a Discord Bot with ChatGPT!

Top comments (0)