My code is:
import os
import openai
import discord
from discord.ext import commands
DISCORD_TOKEN = "xxx"
OPENAI_API_KEY = "xxx"
# Initialize OpenAI API
openai.api_key = OPENAI_API_KEY
intents = discord.Intents.default()
intents.typing = False
intents.presences = False
class ChatBotClient(discord.Client):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
async def on_ready(self):
print(f"{self.user} has connected to Discord!")
async def on_message(self, message):
if message.author == self.user:
return
input_text = message.content
try:
completion = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "assistant", "content": "You are a helpful assistant."},
{"role": "user", "content": input_text}
]
)
response = completion.choices[0].message["content"]
await message.channel.send(response)
except Exception as e:
await message.channel.send(f"Error: {e}")
client = ChatBotClient(intents=intents)
client.run(DISCORD_TOKEN)```