Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import openai
- import discord
- from discord.ext import commands
- # Set your Discord bot token and OpenAI API key
- DISCORD_TOKEN = 'MTEwMjQ0NzYwODA3NDYwMDYwMQ.GlZzg8.MA9TbVYQE97tjcMReX9K8HXiSLlgjBv9WbpApE'
- OPENAI_API_KEY = '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'
- # Initialize the OpenAI API
- openai.api_key = OPENAI_API_KEY
- # Set up the Discord bot
- intents = discord.Intents.default()
- intents.typing = False
- intents.presences = False
- bot = commands.Bot(command_prefix='!', intents=intents)
- @bot.event
- async def on_ready():
- print(f'{bot.user} has connected to Discord!')
- @bot.command(name='chat', help='Chat with the ChatGPT AI.')
- async def chat(ctx, *, prompt: str):
- # Call the OpenAI API to get a response
- response = openai.Completion.create(
- model="gpt-4",
- messages=[
- {"role": "system", "content": "You are a helpful assistant."},
- {"role": "user", "content": "Who won the world series in 2020?"},
- {"role": "assistant", "content": "The Los Angeles Dodgers won the World Series in 2020."},
- {"role": "user", "content": "Where was it played?"}
- ]
- )
- # Send the AI's response to the user
- await ctx.send(response.choices[0].text.strip())
- bot.run(DISCORD_TOKEN)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement