Tried to create a custom decorator for my commands but couldn't be done. #3946
-
Hey, I have tried to create a custom decorator for my commands, but it couldn't be done and I have no idea why! def command(command_name):
def decorator(func):
async def wrapper(update: Update, context: CustomContext):
await func(update, context)
wrapper.is_command = True
return wrapper
return decorator class MyBot:
def __init__(self):
self.app = ApplicationBuilder().token(...).build()
self.app.add_handler(CommandHandler('demo', self.demo_command))
def run(self):
...
@command(name='demo')
async def demo_command(self, update, context):
print('demo_command has been called')
if __name__ == '__main__':
MyBot().run() Traceback (most recent call last):
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\telegram\ext\_application.py", line 1195, in process_update
await coroutine
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\telegram\ext\_basehandler.py", line 153, in handle_update
return await self.callback(update, context)
TypeError: wrapper() takes 2 positional arguments but 3 were given |
Beta Was this translation helpful? Give feedback.
Answered by
Bibo-Joshi
Oct 25, 2023
Replies: 2 comments 5 replies
-
You give an additional argument to your wrapper function, name. You need to add this as a parameter to the function, after update and context. |
Beta Was this translation helpful? Give feedback.
5 replies
-
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Adding
self
as argument towrapper
and passing it along tofunc
does the trick for me. Alternatively, you can ofc also just use*args, **kwargs