draft proposal for reorganizing command handling#26
draft proposal for reorganizing command handling#26skybloo wants to merge 5 commits intoToransuShoujo:masterfrom
Conversation
|
I think moving the commands into their own functions could be a good idea. I think a good way of handling commands better would be to use a more declarative approach. Furthermore I have another suggestion on how to make command handling better. |
- command mapping - separate function for command
e3a8e30 to
9a69022
Compare
|
something a little more like this? I have the aliases mapped the opposite of yours, just to make the code a little cleaner, though it wouldn't be too difficult to flatten as part of the startup process |
index.js
Outdated
| let command = cmd.slice(1) | ||
| const fallback = () => `!${command} is not a valid command` | ||
| const func = dummyMapping?.[command] ?? dummyMapping?.[aliases?.[command]] ?? fallback | ||
| respond(func(sender,args)) |
There was a problem hiding this comment.
I think it would be best to still pass the respond function to the individual command function. I guess it is just taste if respond is passed or if a string is returned, but at least for the test cases it is more traceable where the response was generated, because calling the respond function will generate a stack trace that can be printed in case the test fails, and when returning a string from the function that entry in the stack trace is missing. I also think commands then also have more control over maybe not even emitting text or emitting multiple chat messages.
There was a problem hiding this comment.
honestly I like the more functional style this promotes and it's what I went with in my first iteration so happy to agree
There was a problem hiding this comment.
I was thinking of another use case with this approach:
The response function could also have have optional further arguments to indicate using a twitch chat command, or an announcement message or a reply to a message in chat etc., having this optional argument makes it easier to escape data properly when sending messages to chat.
index.js
Outdated
| respond(dummyMapping[command](sender, args)) | ||
| } | ||
| let command = cmd.slice(1) | ||
| const fallback = () => `!${command} is not a valid command` |
There was a problem hiding this comment.
I think the bot not having a response (instead of printing that the command is not valid) on any other command would be the way to go, since people don't want chat to be spammed and also might have other bots who answer on other commands.
There was a problem hiding this comment.
The fallback could be a function that gets passed the response function (see other comment on line 242) that will then not do anything (no-op).
index.js
Outdated
| } | ||
| let command = cmd.slice(1) | ||
| const fallback = () => `!${command} is not a valid command` | ||
| const func = dummyMapping?.[command] ?? dummyMapping?.[aliases?.[command]] ?? fallback |
There was a problem hiding this comment.
With this approach one could never rename the !add command to something else or disable the !add command.
I think it would be better to distinguish between command names and commands (and their aliases).
For example the dummyMapping could map a command name to a function, and then there is another map, that maps commands and aliases to command names:
For example there is the add command with the addLevel function, and then there is a !add and !push mapping for the add command.
With this approach the add command can also be configured to only be used with !push.
(add might be a bad example, but someone might want to disable the !customcodes command or the !queue command)
There was a problem hiding this comment.
agreed. I left the bangs out because I just don't like them tbh and it lets it shortcircuit non command messages faster
There was a problem hiding this comment.
command names and commands were just terms I came up with to explain better what I mean,
maybe calling it function names and commands would be better,
like commands could be mapped to function names and those are mapped to the actual functions,
whatever definition works
Yeah not having the bangs in there is probably better, but I left them in my explanation, because I thought I could bring across my point better
changed command functions to be more functional
Before I got too crazy I wanted to suggest this as a template for redoing how the commands are handled. It could streamline reading it a heck of a lot, would make it easier to add aliases (just add another case statement under the current one). Would love feedback