Use Honcho to build a Discord bot with conversational memory and context management.
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.
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. We use a helper function validate_message()
to check if the message should be processed.
The code uses several helper functions to keep the main logic clean and readable. Let’s examine each one:
This function centralizes all the logic for determining whether the bot should respond to a message. It checks that:
This helper removes the bot’s mention from the message content, leaving just the actual user input.
This creates a unique peer identifier for each Discord user by prefixing their Discord ID.
This function handles the LLM interaction. It uses Honcho’s built-in to_openai()
method to automatically convert the session context into the format expected by OpenAI’s chat completions API.
This function handles sending messages to Discord, automatically splitting long responses into multiple messages to stay within Discord’s character limits.
The new Honcho peer/session API makes integration much simpler:
Here we create a peer object for the user and a session object using the Discord channel ID. This automatically handles user and session management.
After generating the response, we save both the user’s input and the bot’s response to the session using the add_messages()
method. The peer.message()
creates a message from the user, while assistant.message()
creates a message from the assistant.
Discord bots also offer slash command functionality. Here’s an example using Honcho’s dialectic feature:
This slash command uses Honcho’s dialectic functionality to answer questions about the user based on their conversation history.
The bot requires several environment variables and setup:
honcho_client
: The main Honcho clientassistant
: A peer representing the bot/assistantopenai
: OpenAI client configured to use OpenRouterThe new Honcho peer/session API makes Discord bot integration much simpler and more intuitive. Key patterns we learned:
session.get_context().to_openai()
automatically formats chat historysession.add_messages()
stores both user and assistant messagespeer.chat()
enables querying conversation historyThis approach provides a clean, maintainable structure for building Discord bots with conversational memory and context management.
Use Honcho to build a Discord bot with conversational memory and context management.
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.
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. We use a helper function validate_message()
to check if the message should be processed.
The code uses several helper functions to keep the main logic clean and readable. Let’s examine each one:
This function centralizes all the logic for determining whether the bot should respond to a message. It checks that:
This helper removes the bot’s mention from the message content, leaving just the actual user input.
This creates a unique peer identifier for each Discord user by prefixing their Discord ID.
This function handles the LLM interaction. It uses Honcho’s built-in to_openai()
method to automatically convert the session context into the format expected by OpenAI’s chat completions API.
This function handles sending messages to Discord, automatically splitting long responses into multiple messages to stay within Discord’s character limits.
The new Honcho peer/session API makes integration much simpler:
Here we create a peer object for the user and a session object using the Discord channel ID. This automatically handles user and session management.
After generating the response, we save both the user’s input and the bot’s response to the session using the add_messages()
method. The peer.message()
creates a message from the user, while assistant.message()
creates a message from the assistant.
Discord bots also offer slash command functionality. Here’s an example using Honcho’s dialectic feature:
This slash command uses Honcho’s dialectic functionality to answer questions about the user based on their conversation history.
The bot requires several environment variables and setup:
honcho_client
: The main Honcho clientassistant
: A peer representing the bot/assistantopenai
: OpenAI client configured to use OpenRouterThe new Honcho peer/session API makes Discord bot integration much simpler and more intuitive. Key patterns we learned:
session.get_context().to_openai()
automatically formats chat historysession.add_messages()
stores both user and assistant messagespeer.chat()
enables querying conversation historyThis approach provides a clean, maintainable structure for building Discord bots with conversational memory and context management.