Join our Discord Server
Ajeet Raina Docker Captain, ARM Innovator & Docker Bangalore Community Leader.

How to Build a Discord Bot with ChatGPT?

3 min read

Discord is a powerful communication platform that allows users to connect and engage with communities on a wide range of topics. With the integration of ChatGPT, Discord users can now leverage the power of AI to enhance their experience and keep their audience excited and engaged. Here are some tips on how to keep your audience excited and engaged on Discord using ChatGPT:

  1. Provide personalized recommendations: ChatGPT can use natural language processing to provide personalized recommendations to users based on their interests and preferences. For example, if your Discord server is focused on gaming, ChatGPT can suggest games based on users’ past interactions in the server, keeping them engaged and excited.

  2. Answer questions and provide support: As a language model, ChatGPT can provide immediate and accurate responses to users’ questions and provide relevant information. This can be particularly useful in servers that focus on education or technical support, where users may have questions that need to be answered quickly and efficiently.

  3. Host quizzes and games: ChatGPT can be used to create and host quizzes and games that keep users engaged and excited. For example, a server focused on movies can use ChatGPT to host movie trivia games, while a server focused on music can host music quizzes.

  4. Share informative content: ChatGPT can be used to create informative content that is shared with users on Discord. This can include articles, blogs, and social media posts that cover a range of topics. For example, a server focused on mental health can use ChatGPT to create informative content on coping strategies or mindfulness exercises.

  5. Use ChatGPT to moderate conversations: ChatGPT can be used to moderate conversations in Discord servers, ensuring that conversations remain respectful and on-topic. This can help to create a safe and inclusive environment for all users, which is essential for keeping them engaged and excited.

ChatGPT can be a powerful tool for keeping your audience excited and engaged on Discord. By providing personalized recommendations, answering questions, hosting games, sharing informative content, and moderating conversations, ChatGPT can help to create a thriving and engaging community on Discord.

Building 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


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


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”.


Step 3: Add a Bot to your Application


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.


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: 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.


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:

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 = [

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

client.once('ready', () => {

client.on('messageCreate', async (message) => {
  if ( return;
  if (message.content.startsWith('!ai')) {
    const prompt = message.content.slice(4);
    const completion = await openai.createCompletion({
      model: 'text-davinci-002',
      prompt: prompt,


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:


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!


Please follow and like us:

Have Queries? Join

Ajeet Raina Docker Captain, ARM Innovator & Docker Bangalore Community Leader.

Exploring the Cloud-Native Ecosystem: A Beginner’s Guide to the…

The Cloud Native Computing Foundation (CNCF) is a non-profit organization founded in 2015 with the goal of advancing the development and adoption of cloud-native technologies. The...
Ajeet Raina
3 min read
Join our Discord Server