Creating a ChatGPT Discord Bot Step by Step (2023)

  1. Prerequisites:
  • Discord account
  • Python 3.6 or higher
  • OpenAI API Key
  • Basic understanding of Python programming
  1. Setting up the environment:
  • Install the required packages by running “pip install discord openai” in the terminal.
  • Create a new Discord bot by visiting the Discord Developer Portal and following the instructions.
  • Note down the bot token as it will be used later.
  1. Writing the code:
  • Open a new Python file and import the required packages using “import discord” and “import openai”.
  • Define the Discord bot client using “client = discord.Client()”.
  • Use the “on_message” event to listen for incoming messages and trigger the AI response using OpenAI API.
  • Implement the logic to send the response to the Discord channel using the “send” method on the “channel” object.
  • Provide the OpenAI API key using the “openai.api_key = ‘API_KEY'” syntax.
  1. Testing the bot:
  • Run the code and check if the bot is connected to the Discord server by checking the console logs.
  • Test the bot by sending a message to the Discord channel and verify if the AI response is received.
  1. Deployment:
  • Save the code to a file named “”.
  • Run the bot using the command “python” in the terminal.

Congratulations! You have successfully created a ChatGPT Discord bot. You can customize the bot further by adding additional functionalities like listening for specific keywords or sending random responses.

Full Sample Code:

import discord
import openai

openai.api_key = "YOUR_OPENAI_API_KEY"

client = discord.Client()

async def on_ready():
    print(f'{client.user} has connected to Discord!')

async def on_message(message):
    if == client.user:

    response = openai.Completion.create(


Replace YOUR_OPENAI_API_KEY with your OpenAI API key and YOUR_DISCORD_BOT_TOKEN with your Discord bot token. Then, run the code and your ChatGPT Discord bot should be up and running!

Akalanka Ekanayake
Dilakshan Akalanka Ekanayake, who is well-known as Akalanka Ekanayake is a popular and skilled music editor and programmer based in Sri Lanka. He is a musical artist, Cybersecurity researcher, Software engineer, and the Founder & CEO of Cyberscap. The passion that Akalanka has towards both music and tech has helped him to achieve a lot in both industries. Some of the most notable projects that he worked on include Crimes of the Future, Hacker(2019 ) and France(2021).

Related Stories



Best way to detect GPT-generated (AI) text using Python.

One approach to detect GPT-generated text using Python is to use a language model...

Scraping a website using the BeautifulSoup library (Python)

Full Code import requests from bs4 import BeautifulSoup url = "" # send a request to the website response...

Creating a ChatGPT Discord Bot Step by Step (2023)

Prerequisites: Discord account Python 3.6 or higher OpenAI API Key Basic understanding of Python programming Setting up the environment: Install...

Starting Laravel 9 Project Within 8 Steps

Starting a Laravel project can seem overwhelming, but it's actually a simple process when...

Learn How To Manage Your Stress Using 10 Tips!

These days it is hard not to be weighed down once in a while....

Online Fraud Keywords Explained (Part 2)

Hey, Before reading this please read my first article about this title. (Part 1) PAYMENT...

Popular Categories



Please enter your comment!
Please enter your name here