async def list_commands(event): arg_from_event = event.pattern_match.group(1) cmd_not_found = False command = None cmds_dict = getRegisteredCMDs() if arg_from_event: command, command_value = (None, ) * 2 for key, value in cmds_dict.items(): alt_cmd = value.get("alt_cmd") if key == arg_from_event.lower(): command, command_value = key, value break elif alt_cmd and alt_cmd == arg_from_event.lower(): command, command_value = key, value break if command_value: cmd_alt = command_value.get("alt_cmd") cmd_hasArgs = command_value.get("hasArgs") cmd_prefix = command_value.get("prefix") cmd_no_space_arg = command_value.get("no_space_arg") cmd_no_command = command_value.get("no_cmd") cmd_args = command_value.get("args") cmd_usage = command_value.get("usage") cmd_origin = command_value.get("module_name") space = "" if cmd_no_space_arg else " " if cmd_no_command: cmd_args = "" elif not cmd_hasArgs: cmd_args = f"__{msgRep.ARGS_NOT_REQ}__" elif cmd_hasArgs and not cmd_args: cmd_args = f"__{msgRep.ARGS_NOT_AVAILABLE}__" if not cmd_usage: cmd_usage = f"__{msgRep.MODULE_NO_USAGE.lower()}__" if cmd_alt: cmd_info = f"`{cmd_prefix}{command}`/`"\ f"{cmd_prefix}{cmd_alt}`{space}{cmd_args}\n" else: cmd_info = f"`{cmd_prefix}{command}`{space}{cmd_args}\n" cmd_info += "\n" cmd_info += f"**{msgRep.USAGE}**: {cmd_usage}\n\n" cmd_info += f"**{msgRep.LISTCMDS_ORIGIN_FEATURE}**: {cmd_origin}" await event.edit(cmd_info) return cmd_not_found = True cmds_amount = len(cmds_dict) all_cmds = f"**{msgRep.LISTCMDS_TITLE} ({cmds_amount})**\n\n" if cmd_not_found: all_cmds += msgRep.CMD_NOT_FOUND.format(arg_from_event) + "\n" all_cmds += msgRep.LISTCMDS_USAGE.format("`.lcmds`/`.help`") + "\n\n" for cmd, value in cmds_dict.items(): alt_cmd = value.get("alt_cmd") if alt_cmd: all_cmds += f"`{cmd}` (`{alt_cmd}`)\t\t\t\t" else: all_cmds += f"`{cmd}`\t\t\t\t" await event.edit(all_cmds) return
def module_usage(name_of_module: str, module: str) -> str: if module in getLoadModules().keys(): usage = msgRep.NAME_MODULE.format(name_of_module) + "\n\n" cmds_usage_registered = False for cmd, value in getRegisteredCMDs().items(): if value.get("module_name") == module: if value.get("success", False): if not cmds_usage_registered: cmds_usage_registered = True cmd_alt = value.get("alt_cmd") cmd_hasArgs = value.get("hasArgs") cmd_args = value.get("args") cmd_usage = value.get("usage") if not cmd_hasArgs: cmd_args = f"__{msgRep.ARGS_NOT_REQ}__" elif cmd_hasArgs and not cmd_args: cmd_args = f"__{msgRep.ARGS_NOT_AVAILABLE}__" if not cmd_usage: cmd_usage = f"__{msgRep.MODULE_NO_USAGE.lower()}__" if cmd_alt: usage += f"`.{cmd}`/`.{cmd_alt}` {cmd_args}\n{msgRep.USAGE}: {cmd_usage}\n\n" else: usage += f"`.{cmd}` {cmd_args}\n{msgRep.USAGE}: {cmd_usage}\n\n" if not cmds_usage_registered: if module in MODULE_DICT.keys(): log.info(f"MODULE_DICT is obsolete, please use register_cmd_usage() instead (in module '{module}')") usage += MODULE_DICT.get(module) else: usage += msgRep.MODULE_NO_USAGE return usage else: raise IndexError
async def list_commands(event): arg_from_event = event.pattern_match.group(1) cmd_not_found = False command = None cmds_dict = getRegisteredCMDs() if arg_from_event: command, command_value = (None,)*2 for key, value in cmds_dict.items(): alt_cmd = value.get("alt_cmd") if key == arg_from_event.lower(): command, command_value = key, value break elif alt_cmd and alt_cmd == arg_from_event.lower(): command, command_value = key, value break if command_value: if not command_value.get("success"): name_of_module = command_value.get("module_name") if name_of_module in MODULE_DICT.keys(): log.info(f"MODULE_DICT is obsolete, please use register_cmd_usage() instead (in module '{name_of_module}')") cmd_info = MODULE_DICT.get(name_of_module) await event.edit(cmd_info) return cmd_alt = command_value.get("alt_cmd") cmd_hasArgs = command_value.get("hasArgs") cmd_args = command_value.get("args") cmd_usage = command_value.get("usage") if not cmd_hasArgs: cmd_args = f"__{msgRep.ARGS_NOT_REQ}__" elif cmd_hasArgs and not cmd_args: cmd_args = f"__{msgRep.ARGS_NOT_AVAILABLE}__" if not cmd_usage: cmd_usage = f"__{msgRep.MODULE_NO_USAGE.lower()}__" if cmd_alt: cmd_info = f"`.{command}`/`.{cmd_alt}` {cmd_args}\n{msgRep.USAGE}: {cmd_usage}\n\n" else: cmd_info = f"`.{command}` {cmd_args}\n{msgRep.USAGE}: {cmd_usage}\n\n" await event.edit(cmd_info) return else: cmd_not_found = True cmds_amount = len(cmds_dict) all_cmds = f"**{msgRep.LISTCMDS_TITLE} ({cmds_amount})**\n\n" if cmd_not_found: all_cmds += msgRep.CMD_NOT_FOUND.format(arg_from_event) + "\n" all_cmds += msgRep.LISTCMDS_USAGE.format("`.listcmds`/`.help`") + "\n\n" for cmd, value in cmds_dict.items(): alt_cmd = value.get("alt_cmd") if alt_cmd: all_cmds += f"`{cmd}` (`{alt_cmd}`)\t\t\t\t" else: all_cmds += f"`{cmd}`\t\t\t\t" await event.edit(all_cmds)
def module_usage(name_of_module: str, module: str) -> str: if module in getLoadModules().keys(): usage = msgRep.NAME_MODULE.format(name_of_module) + "\n\n" cmds_usage_registered = False for cmd, value in getRegisteredCMDs().items(): if value.get("module_name") == module: if value.get("success", False): if not cmds_usage_registered: cmds_usage_registered = True cmd_alt = value.get("alt_cmd") cmd_hasArgs = value.get("hasArgs") cmd_prefix = value.get("prefix") cmd_no_space_arg = value.get("no_space_arg") cmd_no_command = value.get("no_cmd") cmd_args = value.get("args") cmd_usage = value.get("usage") space = "" if cmd_no_space_arg else " " if cmd_no_command: cmd_args = "" elif not cmd_hasArgs: cmd_args = f"__{msgRep.ARGS_NOT_REQ}__" elif cmd_hasArgs and not cmd_args: cmd_args = f"__{msgRep.ARGS_NOT_AVAILABLE}__" if not cmd_usage: cmd_usage = f"__{msgRep.MODULE_NO_USAGE.lower()}__" if cmd_alt: usage += f"`{cmd_prefix}{cmd}`/`"\ f"{cmd_prefix}{cmd_alt}`{space}{cmd_args}\n" else: usage += f"`{cmd_prefix}{cmd}`{space}{cmd_args}\n" usage += f"{msgRep.USAGE}: {cmd_usage}\n\n" if not cmds_usage_registered: usage += msgRep.MODULE_NO_USAGE return usage else: raise IndexError
def _unimport_module(self, module: str): """ Tries to remove all data from the target module and finally to unimport it from runtime """ special_caller = [ os.path.join("userbot", "_core", os.path.basename(__file__)), os.path.join("userbot", "modules", "_package_manager.py") ] sys_caller = getouterframes(currentframe(), 2)[2].filename valid_caller = False for caller in special_caller: if sys_caller.endswith(caller): valid_caller = True break if not valid_caller: caller = getouterframes(currentframe(), 2)[2] caller = f"{os.path.basename(caller.filename)}:{caller.lineno}" log.warning(f"Not a valid caller (requested by {caller})") return if NO_MODS: return if module.startswith("__"): log.warning(f"Illegal module name '{module}'") return if module not in getAllModules(): return if module not in getUserModules(): log.error(f"Target module '{module}' is not an user module!") return handlers_from_module = getHandlers().get(module) if handlers_from_module: for handler in handlers_from_module: _tgclient.remove_event_handler(handler) log.info(f"Event handlers from '{module}' removed") else: log.info(f"Module '{module}' has no registered event handlers") update_handlers(module, None, True) unregister_module_desc(module) unregister_module_info(module) cmds_to_remove = [] for cmd, cmd_attrs in getRegisteredCMDs().items(): mod_name = cmd_attrs.get("module_name", "") if module == mod_name: cmds_to_remove.append(cmd) if cmds_to_remove: for cmd in cmds_to_remove: unregister_cmd(cmd) update_all_modules(module, True) update_load_modules(module, False, True) update_user_modules(module, True) try: path = f"userbot.modules_user.{module}" if path in sys.modules: sys.modules.pop(path) log.info(f"Module '{module}' unimported successfully") else: log.info(f"Removed module '{module}' from modules data " "successfully") except KeyError: log.error(f"Failed to unimport module '{module}'") return