uhxx_commands

uhxx_commands is a framework for easily creating slash commands and text commands for Discord using the uhxx library.

Inspired by discord.py's commands extension.

Need help with uhxx_commands? Join our Discord server and ask in the #uhxx_commands channel.

Features

  • Easy command creation
  • Automatic compatibility with Discord slash commands
  • Compatibility with the uhxx library
  • Argument parsing

Compiling uhxx_commands

If you compile a bot using uhxx_commands with dart compile exe, you might get an error you hadn't seen during development:

Error: Function data was not correctly loaded. Did you compile the wrong file?
Stack trace:
#0      loadFunctionData (package:uhxx_commands/src/mirror_utils/compiled.dart:10)
#1      ChatCommand._loadArguments (package:uhxx_commands/src/commands/chat_command.dart:354)
...

This is because uhxx_commands uses dart:mirrors to load the arguments for your commands. During development this is fine but this functionality breaks when compiled because dart:mirrors cannot be used in compiled Dart programs.

To mitigate this, uhxx_commands provides a script that compiles your code for you and loads this information for uhxx_commands to use. To use it, you must first wrap all your command callbacks in id and give each function a unique id.

For example, this chat command:

final ping = ChatCommand(
  'ping',
  'Ping the bot',
  (ChatContext context) => context.respond(MessageBuilder.content('Pong!')),
);

must have its callback wrapped with id like so:

final ping = ChatCommand(
  'ping',
  'Ping the bot',
  id('ping', (ChatContext context) => context.respond(MessageBuilder.content('Pong!'))),
);

You can also use a named function, provided that function is a top-level function declared in the same file:

final ping = ChatCommand(
  'ping',
  'Ping the bot',
  id('ping', _ping),
);

void _ping(ChatContext context) =>
  context.respond(MessageBuilder.content('Pong!'));

If you forget to add the id to a function, you'll get an error similar to this one:

Error: Command Exception: Couldn't load function data for function Closure: (ChatContext) => Null
Stack trace:
#0      loadFunctionData (package:uhxx_commands/src/mirror_utils/compiled.dart:18)
#1      ChatCommand._loadArguments (package:uhxx_commands/src/commands/chat_command.dart:354)
...

Once you've added id to all your commands, use the uhxx_commands:compile script to compile your program. See dart run uhxx_commands:compile --help for a list of options.

If you use the --no-compile flag, make sure that you run/compile the generated file and not your own main file.

Libraries

uhxx_commands
A framework for easily creating slash commands and text commands for Discord using the uhxx library.