How can I make a discord chatbot with ChatGPT?

Shu-Yu Huang
2 min readDec 6, 2022

--

The following steps are generated by ChatGPT @.@ …

  1. Install the ChatGPT python library by running the following command in your terminal:
pip install chatgpt

2. Import the necessary modules for your discord chatbot:

import discord
from discord.ext import commands
from chatgpt import ChatGPT

3. Initialize the ChatGPT model and create a bot instance with discord.py:

chatgpt = ChatGPT()
bot = commands.Bot(command_prefix="!")

4. Create a command for the chatbot to respond to user messages using the ChatGPT model:

@bot.command()
async def chat(ctx, *, message: str):
response = chatgpt.get_response(message)
await ctx.send(response)

5. Run the bot with your discord bot token:

bot.run("DISCORD_BOT_TOKEN")

6. Test the chatbot by sending a message to the discord channel and triggering the chat command, for example:

!chat Hello, how are you?

The chatbot should respond with a generated response using the ChatGPT model.

I think it It built a pretty well framework for a discord bot, only some flaws in it:
```from chatgpt import ChatGPT``` does not exist in any of the codes in github. There is one repo very close to this answer: labteral/chatgpt-python: Unofficial Python SDK for OpenAI’s ChatGPT (github.com).

If you wan to implement with this repo, please:

a. Replace ```from chatgpt import ChatGPT```with```from chatgpt import Conversation``` .

b. Replace ```chatgpt = ChatGPT()```with```chatgpt= Conversation()``` .

c. Replace``` response = chatgpt.get_response(message)``` with ```chatgpt.chat(message)```.

You can go back and ask ChatGPT about how to get a DISCORD_BOT_TOKEN and how to invite a bot to your server to finish this work. LOL

--

--

Shu-Yu Huang

AI engineer in Taiwan AI Academy| Former process engineer in TSMC| Former Research Assistance in NYMU| Studying few-shot-learning and GAN