Use Discord to Test Your LLMs
It's the place to be, and it's so easy to run an AI chatbot in your server. Here's how to get started using Discord.
There’s magic in interacting with AI agents as a part of your day-to-day life. I like to imagine a future where we all have personalized AI assistants, ones that are fun to chat with, that help us with tasks, and make our lives easier. I also want to imagine that we have some control over these; that they aren’t just living on the cloud (at least, not all the time), and that startups and various companies can try out new ideas easily.
One great place to try out those ideas is through Discord’s very powerful API, which lets you easily create bots which can send messages, chat with people, send and receive images, and more.
In this post, I’m going to talk about how you can quickly set up a Discord bot. This is not a post about LLMs, specifically, although that’s the goal.
Why write it? While Discord’s python APIs are great, the actual menu and process to set up a bot can be a bit confusing, so I thought it would be useful to write it down.
A note; this is going to be a very technical — code-heavy — post, and not really about research or robotics. Pure software. I’ll be referencing my Virgil library here — shoutout to my collaborators Andrew Higgins (who added support for Apple Silicon) and Orion Free (who added the Docker build system). This guide is actually going to mostly be referencing the Virgil Discord docs.
Create a Discord Bot
You will need to create your Discord bot and get a token. You can do this by going to the Discord Developer Portal and creating a new application.
Then, you need to create a bot and get the token.
You can find the token in the "Installation" tab of the discord developer UI for your app. You will be able to get a link something like this:
https://discord.com/oauth2/authorize?client_id=999999999999999999&scope=bot
But you'll need to replace the client_id
with your own:
https://discord.com/oauth2/authorize?client_id=$TOKEN&scope=bot
where $TOKEN
is the client ID of your bot. This can be found either in the provided URL on the "Installation" page, or explicitly on the "Oath2" page.
Installation Page
Click “Installation” on the left-hand sidebar.
Here, you need to get the Discord-provided installation link. It will look something like the link below, but you'll need to replace the client_id
with your own:
https://discord.com/oauth2/authorize?client_id=$TOKEN
where $TOKEN
is the client ID of your bot. This can be found either in the provided URL on the "Installation" page, or explicitly on the "Oath2" page.
Set Permissions in OAuth2 and Create an Install Link
Then you need to set permissions for the bot properly and create an install link. For more detailed instructions, see the Discord setup guide.
First make sure redirects is populated with the URL from Installation:
Then make sure the bot has the correct permissions. You'll need guild
, messages.read
, and bot
permissions at a minimum. Then, choose a redict URL (the one you just entered above), and add the correct Bot permissions as well.
The bot permissions need to include sending files and messages.
Then you can create an install link. This will be the link you use to add the bot to your server. Integration type should be "guild install" -- guild is the internal term for a server in the Discord API.
Finally, you'll get a URL you can copy into Discord and use to install your bot in a server.
Running a Bot
Now you should be able to interact with your bot as long as it's running. The basic code is something like this, from virgil.io.discord_bot:
import discord
from discord.ext import commands, tasks
intents = discord.Intents.default()
intents.message_content = True
intents.members = True
# Create an instance of a Client
client = commands.Bot(command_prefix="!", intents=intents)
In my codebase, Friend is a simple chatbot which can use various LLM backends to generate text on Discord. After installing virgil
:
python -m virgil.friend.friend
# You can use various backends
# Gemma 2B is the default, small and fast, but not the fastest
python -m virgil.friend.friend --backend gemma
# Qwen 1.5B is a small model and should work on a laptop GPU
python -m virgil.friend.friend --backend qwen-1.5B
By default it will listen on the #ask-a-robot
channel. You can also ping your bot to summon it to any channel it can access on your server.
References
Discord Developer Portal - use to create your app
Virgil - my own example code