mentionOr function Null safety

String Function(IMessage) mentionOr(
  1. String defaultPrefix(
    1. IMessage
    )
)

A wrapper function for prefixes that allows commands to be invoked with a mention prefix.

For example:

CommandsPlugin commands = CommandsPlugin(
  prefix: mentionOr((_) => '!'),
);

// Add a basic `test` command...

Implementation

String Function(IMessage) mentionOr(String Function(IMessage) defaultPrefix) {
  return (message) {
    RegExpMatch? match = _mentionPattern.firstMatch(message.content);

    if (match != null && message.client is INyxxWebsocket) {
      if (int.parse(match.group(1)!) == (message.client as INyxxWebsocket).self.id.id) {
        return match.group(0)!;
      }
    }

    return defaultPrefix(message);
  };
}