Wante to change the current context to my custom one. #3948
-
I made a custom command decorator so that it would apply to all of my commands via typing but it doesn't seem to have been so successful. I would appreciate it if someone would help me to set it up in the bot settings. class BetterContext(CallbackContext):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
async def send(self, ...):
... def command(name):
def decorator(func):
async def wrapper(_, update: Update, context: BetterContext):
await func(_, update, context)
wrapper.is_command = True
return wrapper
return decorator @command(name='demo')
async def demo_cmd(self, update: Update, context: BetterContext): # This command is part of a class
await context.send(text='test') # so, the self is necessary :D 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)
File "C:\Users\User\source\repos\bot\utils.py", line 123, in wrapper
await func(self, update, context)
File "C:\Users\User\source\repos\bot\bot.py", line 50, in demo_cmd
await context.send(text='test')
AttributeError: 'CallbackContext' object has no attribute 'send'
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ^ |
Beta Was this translation helpful? Give feedback.
Answered by
Bibo-Joshi
Oct 25, 2023
Replies: 1 comment 4 replies
-
Type hinting doesn't change the type of the argument that you pass. Please have a look at https://docs.python-telegram-bot.org/en/v20.6/telegram.ext.contexttypes.html and https://docs.python-telegram-bot.org/en/v20.6/examples.html#examples-contexttypesbot for info on how to make PTB use your sublass. |
Beta Was this translation helpful? Give feedback.
4 replies
Answer selected by
taaaal
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Type hinting doesn't change the type of the argument that you pass. Please have a look at https://docs.python-telegram-bot.org/en/v20.6/telegram.ext.contexttypes.html and https://docs.python-telegram-bot.org/en/v20.6/examples.html#examples-contexttypesbot for info on how to make PTB use your sublass.