Skip to content

Instantly share code, notes, and snippets.

@jakkaj
Created May 21, 2023 03:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jakkaj/6f7c3dab305cc2a5f60a5e293deeb2f8 to your computer and use it in GitHub Desktop.
Save jakkaj/6f7c3dab305cc2a5f60a5e293deeb2f8 to your computer and use it in GitHub Desktop.
A simple Discord bot for LLM
# Import discord.py. Allows access to Discord's API.
import asyncio
import discord
# Import the os module.
import os
from concurrent.futures import ThreadPoolExecutor
# Import load_dotenv function from dotenv module.
from dotenv import load_dotenv
# Import commands from the discord.ext module.
from discord.ext import commands
from jorvis_cli import ask_jorvis
# Loads the .env file that resides on the same level as the script.
load_dotenv()
executor = ThreadPoolExecutor(max_workers=5)
# Grab the API token from the .env file.
DISCORD_TOKEN = os.getenv("DISCORD_APP_TOKEN")
intents = discord.Intents.all()
# Creates a new Bot object with a specified prefix. It can be whatever you want it to be.
bot = commands.Bot(command_prefix="$", intents=intents)
# on_message() event listener. Notice it is using @bot.event as opposed to @bot.command().
@bot.event
async def on_message(message):
try:
if message.author == bot.user:
return
# Check if the bot was mentioned at the start of the message.
if isinstance(message.channel, discord.DMChannel) or message.channel.name == 'jorvis' or (bot.user.mentioned_in(message) and message.mention_everyone is False):
# Check if the message sent to the channel is "hello".
user_id = str(message.author)
async with message.channel.typing():
ai_output = await asyncio.get_event_loop().run_in_executor(executor, ask_jorvis, message.content, user_id)
# view = discord.ui.View()
# item = discord.ui.Button(style=discord.ButtonStyle.blurple, label="Click Me", url="https://google.com")
# view.add_item(item=item)
# Sends a message to the channel.
if isinstance(message.channel, discord.DMChannel) or message.channel.name == 'jorvis':
await message.channel.send(f"{ai_output}")
else:
await message.channel.send(f"{message.author.mention} {ai_output}")
except Exception as e:
traceback = traceback.format_exc()
await message.channel.send(f"{traceback}")
# Includes the commands for the bot. Without this line, you cannot trigger your commands.
await bot.process_commands(message)
# Command $ping. Invokes only when the message "$ping" is send in the Discord server.
# Alternatively @bot.command(name="ping") can be used if another function name is desired.
@bot.command(
# Adds this value to the $help ping message.
help="Uses come crazy logic to determine if pong is actually the correct value or not.",
# Adds this value to the $help message.
brief="Prints pong back to the channel."
)
async def ping(ctx):
# Sends a message to the channel using the Context object.
await ctx.channel.send("pong")
# Command $print. This takes an in a list of arguments from the user and simply prints the values back to the channel.
@bot.command(
# Adds this value to the $help print message.
help="Looks like you need some help.",
# Adds this value to the $help message.
brief="Prints the list of values back to the channel."
)
async def print(ctx, *args):
response = ""
# Loops through the list of arguments that the user inputs.
for arg in args:
response = response + " " + arg
# Sends a message to the channel using the Context object.
await ctx.channel.send(response)
# Executes the bot with the specified token. Token has been removed and used just as an example.
bot.run(DISCORD_TOKEN)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment