IsCommandMessage method for CNext
Created by: Evengard
Summary
This implements IsCommandMessage method which allows to check if the specified instance of DiscordMessage can be matched to a Command in CNext.
Details
This is another take on the same problem I was trying to solve in #944. I may have fallen to the XY Problem and attempting to solve it the wrong way.
Sometimes from custom MessageCreate (or other) command handlers we need to get the information if this message was already processed (or is going to be processed - it doesn't matter) by the CNext handlers. For that I added an CommandsNextExtension.IsCommandMessageAsync
method which checks for that.
Changes proposed
I've offloaded all the routine of parsing the message and finding a specific Command from the handler to a separate method CommandsNextExtension.FindCommandForMessageAsync
, which returns the found Command object alongside with the detected prefix, command string and it's arguments. This method is both called from inside the handler (HandleCommandsAsync
) and the new IsCommandMessageAsync
public method.
Notes
I've stumbled upon this.Config.PrefixResolver(message)
which is an async method, so I had to do implement a small exchange class for storing the out params, and it seems to be better than a tuple. GetAwaiter().GetResult()
on it because I couldn't make the whole method async (because of the out params)I probably could convert the method into a tuple, but then I have a feeling that the result would be too cumbersome to return in case if the command wasn't actually found (it would be
Furthemore maybe it would be useful to actually make (null, null, null, null)
).FindCommandForMessageAsync
public as well.
Also, as this is an alternative approach to the same problem as #944, I think it is logical that only one of theese two pull requests have a chance of making it (and it probably isn't #944). So I think this one superseedes #944, but it's up to you.