Discord Bots with Honcho
Discord is a powerful chat application that handles many UI complications
Example code is available on GitHub
Any application interface that defines logic based on events and supports special commands can work easily with Honcho. Here’s how to use Honcho with Discord as an interface. If you’re not familiar with Discord bot application logic, the py-cord docs would be a good place to start.
Events
Most Discord bots have async functions that listen for specific events, the most common one being messages. We can use Honcho to store messages by user and session based on an interface’s event logic. Take the following function definition for example:
Let’s break down what this code is doing…
This is how you define an event function in py-cord
that listens for messages and checks that the bot doesn’t respond to itself.
These lines check what kind of message is being sent in Discord, which is a useful condition to check before entering the reply logic. The code inside that if-statement is commented quite well, so we’ll just go over the relevant Honcho parts.
Here we’re getting or creating a user for an app that’s been defined at the top of the file.
Here we’re using the discord channel ID as a unique location_id
to attach as metadata to the session. Then we have a nice helper function to take care of some of the session querying logic—we’ll dive into that shortly.
When you call the list
method, it returns an iterable which you can quickly loop over to create a list of Message
objects. Then, we make the call to the LLM using another neat helper function that we will cover.
Helper functions
The first helper function we create is called get_session
. This simplifies a lot of our session-querying logic.
You can see the list
method on the sessions object similarly returns an iterable. This is a common pattern in Honcho—use list comprehension to create your new python list. Then loop through those session objects to find the appropriate location_id
in the metadata, and if none are found then create a new session. You’ll also notice we list messages in reverse=True
order—this means you will get the most recent ones first. We also support native filtering by active sessions.
The next helper function we create is called llm
. This simplifies constructing the chat message object we’re going to send to the inference provider.
Note that messages
is a list of dictionaries that are individually defined with key-value pairs for roles and content. We again use list comprehension to unpack historical message objects into the list that we send to the chat completions method. Honcho Message
objects store role and content natively to make this context construction as simple as possible. If you’re interested in learning more about native Honcho objects, you can check out the models.py
file.
Slash Commands
Discord bots also offer slash command functionality. We can use Honcho to do interesting things via slash commands. Here’s a simple example:
This slash command restarts a conversation with a bot. In Honcho, the delete
method marks a session’s is_active
field to False
.
Recap
How you use Honcho is tightly coupled with the client you’re building in. Here, Discord serves as an example of an interactive chat interface. We’re just scratching the surface of things you can do with Honcho, but we learned some key patterns:
- how to register users
- how to work with iterables when listing sessions, messages
- how to attach metadata to Honcho objects (like
location_id
on sessions) - how to sort and filter when calling list methods
You are well on your way to becoming a context construction master! Stay tuned for more in-depth examples. If you want a challenge, try deciphering how we construct context in one of our apps, Bloom.