Skip to content

Source Code for LLM Discord bot: #29

Description

@Deadsg

import os
import discord
from discord.ext import commands
import openai

Set up your OpenAI API key

openai.api_key = ""

Initialize the Discord bot

intents = discord.Intents.default()
intents.typing = False
intents.presences = False
bot = commands.Bot(command_prefix='!', intents=intents)

Define privileged_users (replace with actual user IDs)

privileged_users = ["Deadsg", "user_id_2"]

Define event handler for when the bot is ready

@bot.event
async def on_ready():
print(f"We have logged in as {bot.user}")

Define event handler for when a message is received

@bot.event
async def on_message(message):
if message.author == bot.user:
return

if message.content.startswith('!ask_gpt3'):
    # Define a prompt to send to ChatGPT
    prompt = "Translate the following English text to French:"

    # Extract user query from message
    user_query = message.content[len('!ask_gpt3 '):]

    # Combine prompt and user query
    full_prompt = f"{prompt} {user_query}"

    # Use the model to get a response from ChatGPT
    response = openai.Completion.create(
      engine="davinci",  # Choose your preferred model here
      prompt=full_prompt,
      max_tokens=50  # Adjust as needed
    )

    # Extract the answer from the response
    answer = response.choices[0].text.strip()

    # Send the answer back to the Discord channel
    await message.channel.send(answer)

token = ""

client = discord.Client()

Define intents

intents = discord.Intents.default()
intents.typing = False
intents.presences = False

Create a bot instance with a command prefix

bot = commands.Bot(command_prefix='Ask ', intents=intents)

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

client.run(token)

Run the bot

bot.run('') # Replace with your bot token

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions