import discord from discord.ext import commands import openai # Initialize the OpenAI API with your API key openai.api_key = 'your_openai_api_key_here' # Replace with your OpenAI API key bot = commands.Bot(command_prefix="$") # Define commands and categories for the help command (can be expanded as per previous list) commands_list = { "$ChatGPT ": "Sends a message to ChatGPT-like AI and replies with a response.", "$help": "Lists all available commands and their descriptions.", # Add other commands as per previous setup } # The $ChatGPT command - sends a request to OpenAI's GPT model to generate a response @bot.command() async def ChatGPT(ctx, *, message: str): """Handles user input and sends a request to OpenAI's API to generate a response.""" try: # Requesting a response from OpenAI's GPT model response = openai.Completion.create( model="text-davinci-003", # You can use "gpt-3.5-turbo" or other available models prompt=message, # The user's message max_tokens=150, # Limit the response length temperature=0.7, # Creativity level of the response ) # Extract the generated text from OpenAI's response chat_response = response.choices[0].text.strip() # Send the response back to Discord await ctx.send(f"ChatGPT says: {chat_response}") except Exception as e: # If an error occurs (e.g., API limits, etc.), send an error message await ctx.send(f"Error: {str(e)}") # The $help command to list all available commands and their descriptions @bot.command() async def help(ctx): help_message = "Here are all the available commands:\n\n" for command, description in commands_list.items(): help_message += f"{command}: {description}\n" await ctx.send(help_message) # Bot event to notify when the bot is ready @bot.event async def on_ready(): print(f'Logged in as {bot.user}') # Run the bot with your bot token bot.run('your_discord_bot_token_here') # Replace with your bot's token