Friends and I had some fun conversing with this discord bot that calls the API. This one even takes reactions into account.
import os
import openai
import discord
from discord.ext import commands
openai.api_key = "YOUR_OPENAI_API_KEY_HERE"
start_sequence = "\nA:"
restart_sequence = "\n\nQ: "
################################
description = "A discord bot that uses GPT3 API to converse with users. "
TOKEN = "YOUR_DISCORD_API_KEY_HERE"
AUTHORIZED_CHANNEL = 0 # the channel where you want it to be restricted to.
intents = discord.Intents.default()
intents.members = True
Embed = discord.Embed
intents.reactions = True
intents.members = True
client = discord.Client(intents=intents)
bot = commands.Bot(command_prefix="$", description=description, intents=intents)
data = []
async def getBotResponse(question):
response = openai.Completion.create(
model="text-davinci-003",
prompt=question,
temperature=0.5,
max_tokens=500,
)
return response["choices"][0]["text"]
# take into account users' reactions.
@client.event
async def on_raw_reaction_add(reaction):
if (reaction.channel_id != AUTHORIZED_CHANNEL or reaction.user_id == client.user.id):
return
data.append(str(reaction.emoji))
@client.event
async def on_message(message):
if message.author == client.user:
return
else:
if message.channel.id == client.get_channel(AUTHORIZED_CHANNEL).id:
async with message.channel.typing():
data.append(message.content)
lastMessages = "\n".join(data)
response = await getBotResponse(lastMessages)
await message.channel.send(response)
client.run(TOKEN)
Have fun!