Port mtrix DiscordClient class to mindtrace#205
Merged
Conversation
added 9 commits
September 26, 2025 10:33
- Add BaseDiscordClient with slash command support - Implement Discord bot with modern slash commands and legacy prefix support - Add comprehensive unit tests for Discord service - Add discord_bot_example.py sample implementation - Update services pyproject.toml with discord.py dependency - Support for event handling, command registration, and permissions - Clean implementation without sensitive tokens in history
- Use config.get() instead of config[] to handle missing keys gracefully - All unit tests now pass (18/18)
- Update test imports from mindtrace.services.sample to mindtrace.services.samples - Move test_echo_service.py to correct samples directory
The Discord implementation now includes: - Modern slash command support with auto-completion - Backward compatibility with prefix commands - Comprehensive unit tests - Clean integration with Mindtrace Service patterns - Proper configuration management via CoreConfig
…asses - Rename BaseDiscordClient to DiscordClient (extends Mindtrace) - Create new DiscordService class (extends Service) that wraps DiscordClient - Separate concerns: DiscordClient handles core bot logic, DiscordService provides HTTP API - Update all imports, tests, and examples to use new class names - Fix endpoint paths to use dot notation for ConnectionManager compatibility - Use POST requests consistently across all endpoints for Service pattern compatibility - Add comprehensive integration tests for DiscordService HTTP endpoints - Create example files: custom_bot_client.py and custom_bot_service.py - Fix NaN latency handling in bot status responses - All integration tests now pass Breaking changes: - BaseDiscordClient renamed to DiscordClient - Service-specific methods moved from DiscordClient to DiscordService - Endpoint paths changed from slashes to dots (e.g., /discord/status -> /discord.status) - All endpoints now use POST requests instead of mixed GET/POST This refactoring provides better separation of concerns and allows using Discord functionality either as a standalone Mindtrace client or as a full HTTP service.
- Add execute_command method to DiscordService for programmatic command execution - Make DiscordCommandInput parameters optional with reasonable defaults - Implement parameter parsing from command content strings (e.g., "/roll 20" → sides=20) - Add Discord parameter type to Python type mapping for proper conversion - Create minimal mock interactions without fake Discord server data - Add warning logging for missing required parameters - Update example script to demonstrate optional parameters and proper cleanup - Support exposing AI models and shared logic through both Discord and HTTP interfaces This enables the same functionality to be accessed via Discord slash commands or HTTP API calls, perfect for AI models, shared business logic, and web UIs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR ports the old DiscordClient class from
mtrix. It also updates the client to utilize slash commands.DiscordClient Usage
First, create your own Discord server and bot, and add the bot to your server. You can find a comprehensive guide on how to do this here. The process should only take 5 minutes. Place the bot's API key in your config.ini file at
[MINDTRACE_API_KEYS][DISCORD], set the associatedMINDTRACE_API_KEYS__DISCORDenvironment variable, or directly pass the token into the following command.To run the integration tests, also place the same (or an additional bot key) at
[MINDTRACE_TESTING_API_KEYS][DISCORD].Then test the above with the associated
DiscordClientbot sample:$ uv run mindtrace/services/samples/discord/custom_bot_client.pyIf the bot successfully launches, you will get the following message:
In any chat or DM the bot has access to, type slash

/to list the bot commands:If the bot has the requisite permissions, all of the example commands should work. Commands with extra inputs will display them as separate input boxes:

Custom handlers are also supported. Refer to the example code for a custom handler for DMs:
DiscordService class
The above sample uses the
DiscordClientclass, which derives from theMindtraceclass. There is a separateDiscordServiceclass derived from the mindtraceServiceclass with associated functionality. You can run the associated sample with:$ uv run mindtrace/services/samples/discord/custom_bot_service.pyRunning the Discord bot as a
Serviceenables using the bot's slash commands through the generated connection manager'sdiscord_executecommand:Note the following arguments are always provided by Discord when calling the bot through Discord. If your slash command uses them (e.g. for some guild commands), you will have to pass them in through the the above command as well:
author_id,channel_id,guild_id,message_id.Docs & Testing
Unit tests, integration tests and samples have been provided. Run the discord test suite with
All tests should pass.
The two samples can be run with:
After running either sample above, you should be able to use and communicate with the bot on the associated Discord server.