Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.1 kB
2
Indexable
Never
import os
import discord
import openai
from dotenv import load_dotenv

load_dotenv()  # Load environment variables from a .env file

client = discord.Client()

openai.api_key = os.getenv('OPENAI_API_KEY')  # Get your API key from the OpenAI dashboard

# Define a function to generate a response to a message using GPT
def generate_response(message):
    prompt = f"User: {message.content}\nAI:"
    response = openai.Completion.create(
        engine="davinci",
        prompt=prompt,
        max_tokens=1024,
        n=1,
        stop=None,
        temperature=0.7,
    )
    return response.choices[0].text.strip()

@client.event
async def on_ready():
    print('Logged in as {0.user}'.format(client))

@client.event
async def on_message(message):
    if message.author == client.user:  # Ignore messages sent by the bot itself
        return

    if message.content.startswith('!chat'):
        response = generate_response(message)
        await message.channel.send(response)

client.run(os.getenv('DISCORD_BOT_TOKEN'))  # Get your bot token from the Discord developer portal