poise icon indicating copy to clipboard operation
poise copied to clipboard

Add fallback message handler

Open emilyyyylime opened this issue 1 year ago • 3 comments

(As discussed on Discord)

My usecase requires potentially processing all non-command messages, and I found the only way to robustly do this was to add a general event handler and then parse all messages twice to check whether they are valid commands or not. It would be nice to have an exposed API to process any message that wasn't found to be a command, perhaps exposing the reason it failed to parse as a command—though that's beyond my own use case

emilyyyylime avatar Jan 03 '24 10:01 emilyyyylime

Another option would be to expose prefix::strip_prefix as a public API, though that's less 'robust'

emilyyyylime avatar Jan 08 '24 13:01 emilyyyylime

Using strip_prefix to check whether a message is a command would give false positives...

  • if the message is from the bot itself and execute_self_messages isn't set
  • if the message is from other bots and ignore_bots is set
  • if the message is a thread creation and ignore_thread_creation is set
  • if the string after the prefix is not a valid command invocation (find_command)
  • if the parsed command doesn't have a prefix implementation

All these edge cases along with a nested call to the actual strip_prefix are handled in parse_invocation, and that function is public. So FYI if you're ok with that hacky solution, you can already do it now if you want

kangalio avatar Jan 08 '24 14:01 kangalio

Implemented in https://github.com/serenity-rs/poise/commit/1f0abfbc4c6d79af62867ff94fe79f5ae4fe6f8f. Feel free to try it in your bot via a cargo git dependency ^^

kangalio avatar Jan 12 '24 10:01 kangalio