def decorator(function): async def func_callback(event): try: await function(event) except Exception as e: # This block will be executed if the function, where the events are # being used, has no own exception handler(s) pattern_no_regex = sub( r"\W", "", pattern) # remove any symbols from regex self.log.error( f"Command '{pattern_no_regex}' stopped due to an unhandled exception " f"in function '{function.__name__}'", exc_info=True if self.traceback else False) try: # in case editing messages isn't allowed (channels) await event.edit( f"`{msgResp.CMD_STOPPED.format(f'{pattern_no_regex}.exe')}`" ) except: pass tgclient.add_event_handler(func_callback, MessageEdited(pattern=pattern, **args)) tgclient.add_event_handler(func_callback, NewMessage(pattern=pattern, **args)) return func_callback
def decorator(function): nonlocal command nonlocal alt pattern_no_cmd = False if not pattern and not command: return None if pattern and not command: command = pattern pattern_no_cmd = True self.log.info(f"[EventHandler.on()] parameter 'pattern' is obsolete, please update to "\ f"'command' instead (in function '{function.__name__}')") command_no_regex = self.__removeRegEx( command) # remove any symbols from regex if alt: alt = self.__removeRegEx(alt) if not pre_register_cmd(command_no_regex, alt, hasArgs, function): self.log.error(f"Unable to add command '{command_no_regex}' in function '{function.__name__}' "\ "to event handler as previous registration failed") return None async def func_callback(event): try: await function(event) except Exception as e: # This block will be executed if the function, where the events are # being used, has no own exception handler(s) try: # get current executed command command_no_regex = event.pattern_match.group(0).split( " ")[0][1:] except: pass self.log.error( f"Command '{command_no_regex}' stopped due to an unhandled exception " f"in function '{function.__name__}'", exc_info=True if self.traceback else False) try: # in case editing messages isn't allowed (channels) await event.edit( f"`{msgResp.CMD_STOPPED.format(f'{command_no_regex}.exe')}`" ) except: pass if not pattern_no_cmd: if self.__isRegExCMD(command): command = self.__removeRegEx(command) # TODO if alt: command = fr"^\.(?:{command}|{alt})(?: |$)(.*)" if hasArgs else fr"^\.(?:{command}|{alt})$" else: command = fr"^\.{command}(?: |$)(.*)" if hasArgs else fr"^\.{command}$" else: command = pattern # 'restore' regex tgclient.add_event_handler(func_callback, MessageEdited(pattern=command, **args)) tgclient.add_event_handler(func_callback, NewMessage(pattern=command, **args)) return func_callback
def decorator(function): if not function: return None if not callable(function): return None cmd = command curr_alt = alt if curr_alt: curr_alt = self.__removeRegEx(curr_alt) if not pre_register_cmd(cmd, curr_alt, hasArgs, ".", False, False, function): self.log.error(f"Unable to add command '{cmd}' in function '{function.__name__}' "\ "to event handler as previous registration failed") return None async def func_callback(event): try: await function(event) except Exception as e: # This block will be executed if the function, where the events are # being used, has no own exception handler(s) try: # get current executed command curr_cmd = event.pattern_match.group(0).split( " ")[0][1:] except: curr_cmd = cmd self.log.error( f"Command '{curr_cmd}' stopped due to an unhandled exception " f"in function '{function.__name__}'", exc_info=True if self.traceback else False) try: # in case editing messages isn't allowed (channels) await event.edit( f"`{msgResp.CMD_STOPPED.format(f'{curr_cmd}.exe')}`" ) except: pass if curr_alt: cmd_regex = fr"^\.(?:{cmd}|{curr_alt})(?: |$)(.*)" if hasArgs else fr"^\.(?:{cmd}|{curr_alt})$" else: cmd_regex = fr"^\.{cmd}(?: |$)(.*)" if hasArgs else fr"^\.{cmd}$" try: if not ignore_edits: tgclient.add_event_handler( func_callback, MessageEdited(pattern=cmd_regex, **args)) tgclient.add_event_handler( func_callback, NewMessage(pattern=cmd_regex, **args)) except Exception as e: self.log.error(f"Failed to add command '{cmd}' to client "\ f"(in function '{function.__name__}')", exc_info=True if self.traceback else False) return None return func_callback
def decorator(function): if not function: return None if not callable(function): return None caller = getouterframes(currentframe(), 2)[1] caller = f"{basename(caller.filename)[:-3]}:{caller.lineno}" if not name: self.log.error(f"Name of command/feature in function '{function.__name__}' "\ f"must not be empty ({caller})") return None if not pre_register_cmd( name, None, hasArgs if not no_cmd else False, prefix if not no_cmd else "", no_space_arg, no_cmd, function): self.log.error(f"Unable to add command/feature '{name}' in function '{function.__name__}' "\ f"to event handler as previous registration failed ({caller})") return None async def func_callback(event): try: await function(event) except Exception as e: if not no_cmd: self.log.error( f"Command '{name}' stopped due to an unhandled exception " f"in function '{function.__name__}'", exc_info=True if self.traceback else False) try: await event.edit( f"`{msgResp.CMD_STOPPED.format(f'{name}.exe')}`" ) except: pass else: self.log.error( f"Feature '{name}' stopped due to an unhandled exception " f"in function '{function.__name__}'", exc_info=True if self.traceback else False) try: if isinstance(events, (list, tuple)): for event in events: tgclient.add_event_handler( func_callback, event(pattern=pattern, *args, **kwargs)) else: tgclient.add_event_handler( func_callback, events(pattern=pattern, *args, **kwargs)) except Exception as e: self.log.error(f"Failed to add command/feature '{name}' to client "\ f"(in function '{function.__name__}' ({caller}))", exc_info=True if self.traceback else False) return None return func_callback
def decorator(function): async def func_callback(event): try: await function(event) except Exception as e: self.log.error( f"Function '{function.__name__}' stopped due to an unhandled exception", exc_info=True if self.traceback else False) tgclient.add_event_handler(func_callback, ChatAction(**args)) return func_callback
def decorator(function): nonlocal command nonlocal alt pattern_no_cmd = False if not pattern and not command: return None if pattern and not command: command = pattern pattern_no_cmd = True self.log.info(f"[EventHandler.on_NewMessage()] parameter 'pattern' is obsolete, please update to "\ f"'command' instead (in function '{function.__name__}')") command_no_regex = self.__removeRegEx(command) if alt: alt = self.__removeRegEx(alt) if not pre_register_cmd(command_no_regex, alt, hasArgs, function): self.log.error(f"Unable to add command '{command_no_regex}' in function '{function.__name__}' "\ "to event handler as previous registration failed") return None async def func_callback(event): try: await function(event) except Exception as e: try: command_no_regex = event.pattern_match.group(0).split( " ")[0][1:] except: pass self.log.error( f"Command '{command_no_regex}' stopped due to an unhandled exception " f"in function '{function.__name__}'", exc_info=True if self.traceback else False) try: await event.edit( f"`{msgResp.CMD_STOPPED.format(f'{command_no_regex}.exe')}`" ) except: pass if not pattern_no_cmd: if self.__isRegExCMD(command): command = self.__removeRegEx(command) # TODO if alt: command = fr"^\.(?:{command}|{alt})(?: |$)(.*)" if hasArgs else fr"^\.(?:{command}|{alt})$" else: command = fr"^\.{command}(?: |$)(.*)" if hasArgs else fr"^\.{command}$" else: command = pattern tgclient.add_event_handler(func_callback, NewMessage(pattern=command, **args)) return func_callback
def decorator(function): if not function: return None if not callable(function): return None if not pre_register_cmd( name, None, hasArgs if not no_cmd else False, prefix if not no_cmd else "", no_space_arg, no_cmd, function): self.log.error(f"Unable to add command/feature '{name}' in function '{function.__name__}' "\ "to event handler as previous registration failed") return None async def func_callback(event): try: await function(event) except Exception as e: if not no_cmd: self.log.error( f"Command '{name}' stopped due to an unhandled exception " f"in function '{function.__name__}'", exc_info=True if self.traceback else False) try: await event.edit( f"`{msgResp.CMD_STOPPED.format(f'{name}.exe')}`" ) except: pass else: self.log.error( f"Feature '{name}' stopped due to an unhandled exception " f"in function '{function.__name__}'", exc_info=True if self.traceback else False) try: if isinstance(events, list): for event in events: tgclient.add_event_handler( func_callback, event(pattern=pattern, **args)) else: tgclient.add_event_handler(func_callback, events(pattern=pattern, **args)) except Exception as e: self.log.error(f"Failed to add command/feature '{name}' to client "\ f"(in function '{function.__name__}')", exc_info=True if self.traceback else False) return None return func_callback
def decorator(function): async def func_callback(event): try: await function(event) except Exception as e: self.log.error( f"Function '{function.__name__}' stopped due to an unhandled exception", exc_info=True if self.traceback else False) try: tgclient.add_event_handler(func_callback, ChatAction(*args, **kwargs)) except Exception as e: self.log.error(f"Failed to add a chat action feature to client "\ f"(in function '{function.__name__}')", exc_info=True if self.traceback else False) return None return func_callback
def decorator(function): async def func_callback(event): try: await function(event) except Exception as e: pattern_no_regex = sub(r"\W", "", pattern) self.log.error( f"Command '{pattern_no_regex}' stopped due to an unhandled exception " f"in function '{function.__name__}'", exc_info=True if self.traceback else False) try: await event.edit( f"`{msgResp.CMD_STOPPED.format(f'{pattern_no_regex}.exe')}`" ) except: pass tgclient.add_event_handler(func_callback, NewMessage(pattern=pattern, **args)) return func_callback
def decorator(function): if not function: return None if not callable(function): return None # get caller in case of failed actions caller = getouterframes(currentframe(), 2)[1] # link to file and line number caller = f"{basename(caller.filename)[:-3]}:{caller.lineno}" if not command: self.log.error(f"Command in function '{function.__name__}' " "must not be empty ({caller})") return None if not self.__checkCmdValidity(command): self.log.error(f"Validity check for '{command}' in function " f"'{function.__name__}' " f"failed. Special characters are " f"not allowed ({caller})") return None if alt and not self.__checkCmdValidity(alt): self.log.error(f"Validity check for '{alt}' (alternative " f"command of '{command}') " f"in function '{function.__name__}' failed. " "Special characters are not allowed " f"({caller})") return None if not pre_register_cmd(command, alt, hasArgs, ".", False, False, function): self.log.error(f"Unable to add command '{command}' in " f"function '{function.__name__}' " f"to event handler as previous registration " f"failed ({caller})") return None async def func_callback(event): try: if _is_active(command): await function(event) except Exception as e: # This block will be executed if the function, where # the events are being used, has no own # exception handler(s) try: # get current executed command curr_cmd = ( event.pattern_match.group(0).split(" ")[0][1:]) except: curr_cmd = command self.log.error( f"Command '{curr_cmd}' stopped due to an " "unhandled exceptionin function " f"'{function.__name__}'", exc_info=True if self.traceback else False) try: # in case editing messages isn't allowed (channels) cmd_stopped = ( msgResp.CMD_STOPPED.format(f"{curr_cmd}.exe")) await event.edit(f"`{cmd_stopped}`") except: pass if alt: cmd_regex = (fr"^\.(?:{command}|{alt})(?: |$)(.*)" if hasArgs else fr"^\.(?:{command}|{alt})$") else: cmd_regex = (fr"^\.{command}(?: |$)(.*)" if hasArgs else fr"^\.{command}$") try: if not ignore_edits: tgclient.add_event_handler( func_callback, MessageEdited(pattern=cmd_regex, *args, **kwargs)) tgclient.add_event_handler( func_callback, NewMessage(pattern=cmd_regex, *args, **kwargs)) except Exception as e: self.log.error( f"Failed to add command '{command}' to client " f"(in function '{function.__name__}' " f"({caller}))", exc_info=True if self.traceback else False) return None return func_callback