AWeirdDev / linelib

🚀 The simple yet perfect solution to launch your bot to the next level.
https://google.com/search?q=hello
MIT License
6 stars 0 forks source link

[Feature Request] Argument missing handling #2

Closed charlie-moomoo closed 1 year ago

charlie-moomoo commented 1 year ago

Currently, if the command is missing an argument, it will occur following error:

Exception on /webhook [POST]
Traceback (most recent call last):
  File "/home/runner/LineBot/venv/lib/python3.10/site-packages/flask/app.py", line 2525, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/runner/LineBot/venv/lib/python3.10/site-packages/flask/app.py", line 1822, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/runner/LineBot/venv/lib/python3.10/site-packages/flask/app.py", line 1820, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/runner/LineBot/venv/lib/python3.10/site-packages/flask/app.py", line 1796, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
  File "/home/runner/LineBot/venv/lib/python3.10/site-packages/linelib/http/portal.py", line 126, in webhook_handler
    self._go_over_cmds(e)
  File "/home/runner/LineBot/venv/lib/python3.10/site-packages/linelib/http/portal.py", line 138, in _go_over_cmds
    i(e)
  File "/home/runner/LineBot/venv/lib/python3.10/site-packages/linelib/http/portal.py", line 374, in TextEmitter
    raise err
  File "/home/runner/LineBot/venv/lib/python3.10/site-packages/linelib/http/portal.py", line 367, in TextEmitter
    if transform[_t](_all[i]) != "":
IndexError: string index out of range

We should be able to handle the error, so we can warn the user give all arguments.

AWeirdDev commented 1 year ago

How about more exception handlers? Such as command not found, error while processing commands / events… etc.

charlie-moomoo commented 1 year ago

I wish I could do something like this:

def error():
  print('oh no')

class MyCog(Client.Commands, prefix="!"):
  @cmd(name="hi",on_error=error)
  def hi(ctx):
    ctx.reply('hi')
AWeirdDev commented 1 year ago

Generally, if there are too many errors to handle, it might cause you to create too many functions OUTSIDE the cog. I think using an event handler would be the best:

class Cog(Client.Commands, ...):
  @cmd(name=...)
  def fn(ctx):
    ...

  @fn.error()
  def on_error(ctx, error):
    ctx.reply(
        "I know you hate errors" if isinstance(error, BadArguments) else "ok idk what error"
    )
charlie-moomoo commented 1 year ago

Oh, that looks good. :)

sus2790 commented 1 year ago

And rename cmd to command please,.

AWeirdDev commented 1 year ago

Hey, @charlie-moomoo. Thanks for the advice, I've added it to Linelib, you can now access it with the following script:

cmd = Client.Commands.cmd
class foo(Client.Commands):
    @cmd(...)
    def bar(ctx, arg1: str):
        pass
    @bar.error()
    def onError(ctx, error):
        ctx.reply("Please pass in arguments properly.")

Additionally, I'd like to provide you more detailed info about the triggering.

The @bar.error() script will be triggered when the following occurrs:

requested type: You can assign them inside your functions: func(ctx, foo: str, bar: int) linelib will auto-convert them into specified types.

However, the Client.command method will remain unchanged until further notice.

Cheers! AWeirdDev

AWeirdDev commented 1 year ago

@sus2790: And rename cmd to command please,.

Hey there! As @charlie-moomoo referenced in their issue, the cmd variable is pointed directly to Client.Commands.cmd (an alias of Client.Commands.command.

If you want to create a basic command, use the following instead:

@client.command

Thanks!