weidengeist / Willowbot

A chat bot intended to be used on Twitch
GNU General Public License v3.0
4 stars 0 forks source link

Arguments for function commands cannot be empty/optional #1

Closed Clasko closed 1 year ago

Clasko commented 1 year ago

There is currently no way to create a command with a function where arguments are optional.

The resolveArguments function returns an empty string when no argument are passed to the command: https://github.com/weidengeist/Willowbot/blob/744626bbf1aab412c3e630c00d2176410b4bd495/modules/message.py#L190-L192

This breaks the eval(func) for functions:

Traceback (most recent call last):
  File "/Users/clasko/Development/Willowbot/main_cli.py", line 60, in <module>
    chatMsg.processCommands(commands, r, irc)
  File "/Users/clasko/Development/Willowbot/modules/message.py", line 318, in processCommands
    self.reactToMessage(commands, 'general', match, irc)
  File "/Users/clasko/Development/Willowbot/modules/message.py", line 258, in reactToMessage
    eval(func)
  File "<string>", line 0

SyntaxError: invalid syntax
weidengeist commented 1 year ago

Hi Clasko,

thank you for digging this up. I could reproduce it by using the !poll command provided by the respective module in the repository without any arguments.

Returning an empty string here is indeed a very radical step which should be replaced. My suggestion is the following:

if len(argsMsg) == 0 and (len(argsAnswer) > 0 or re.match('.*\$arg@', answerText)):
  # … return an empty string, i.e. prevent the bot from reacting with a message.
  return answerText.replace("$arg", "missingArg")

This way, the actual string for calling the function is preserved and detecting as well as handling the wrong string arguments missingArg[0-9@] is something the function called via the command has to take care of.

Please let me know what you think of this solution.

Regards, Weidengeist

Clasko commented 1 year ago

Sure, this would do the job. :)

weidengeist commented 1 year ago

Thank you very much for your feedback. :-) I have just updated the repository with the fix above and will mark the issue as solved.