async def _rename_cmd(self, context, cmd_name: cmd_name_converter, new_name: cmd_name_converter): parent, child, cmd_name = get_cmd_attributes(self.bot, cmd_name, True) new_parent, new_child, new_name = get_cmd_attributes( self.bot, new_name, False) cmd = self.bot.db[context.guild.id].select("user_commands", cmd_name) if cmd is not None: if cmd["lock"]: raise LookupError( f"Custom command '{cmd_name}' is locked and canot be edited." ) attributes = json_load_dict(cmd["attributes"]) old_cmd = parent.remove_command(child) try: new_cmd = set_new_cmd(new_parent, new_child, cmd["message"], attributes, cmd["isgroup"]) # move the commands if isinstance(old_cmd, commands.Group) and isinstance( new_cmd, commands.Group): for command in old_cmd.commands: # move the commands from the old one to the new one new_cmd.add_command(command) self.bot.db[context.guild.id].query( f"UPDATE user_commands SET cmdname='{new_name} {command.name}' WHERE cmdname='{cmd_name} {command.name}'" ) self.bot.db[context.guild.id].query( f"UPDATE user_commands SET cmdname='{new_name}' WHERE cmdname='{cmd_name}' " ) await self.log_cmd_update(context, new_name, cmd["message"], attributes, cmd["isgroup"], "Renamed Command") except Exception as e: parent.add_command(old_cmd) raise e else: raise LookupError(f"Custom command '{cmd_name}' not found.")
async def _add_cmd(self, context, cmd_name: cmd_name_converter, *, cmd_args: cmd_add_converter): attributes, cmd_text = cmd_args if not cmd_text: raise commands.UserInputError( "Unexpected format, no text found for the command") parent, child, cmd_name = get_cmd_attributes(self.bot, cmd_name, False) set_new_cmd(parent, child, cmd_text, attributes, False) await self.after_cmd_update(context, cmd_name, cmd_text, attributes, False, "Added Command")
async def _gadd_cmd(self, context, cmd_name: cmd_name_converter, *, cmd_args: cmd_add_converter = None): if cmd_args is None: attributes = {} cmd_text = "" else: attributes, cmd_text = cmd_args parent, child, cmd_name = get_cmd_attributes(self.bot, cmd_name, False) set_new_cmd(parent, child, cmd_text, attributes, True) await self.after_cmd_update(context, cmd_name, cmd_text, attributes, True, "Added Command Group")
async def _unlock_cmd(self, context, cmd_name: cmd_name_converter): parent, child, cmd_name = get_cmd_attributes(self.bot, cmd_name, True) cmd = self.bot.db[context.guild.id].select("user_commands", cmd_name) if cmd is not None: self.bot.db[context.guild.id].query( f"UPDATE user_commands SET lock=0 WHERE cmdname='{cmd_name}'") await context.send(f"Command '{cmd_name}' has been unlocked.") title = f"User Unlocked a Command" fields = { "User": f"{context.author.mention}\n{context.author}", "Command Group" if cmd["isgroup"] else "Command": cmd_name } await self.bot.log_admin(context.guild, title=title, fields=fields, timestamp=context.message.created_at) else: raise LookupError(f"Custom command '{cmd_name}' not found.")
async def _update_cmd(self, context, cmd_name: cmd_name_converter, *, cmd_args: cmd_add_converter): attributes_new, cmd_text = cmd_args parent, child, cmd_name = get_cmd_attributes(self.bot, cmd_name, True) cmd = self.bot.db[context.guild.id].select("user_commands", cmd_name) if cmd is not None: if cmd["lock"]: raise LookupError( f"Custom command '{cmd_name}' is locked and canot be edited." ) if not cmd_text and not cmd["isgroup"]: cmd_text = cmd["message"] attributes = json_load_dict(cmd["attributes"]) attributes.update(attributes_new) set_new_cmd(parent, child, cmd_text, attributes, cmd["isgroup"]) await self.after_cmd_update(context, cmd_name, cmd_text, attributes, cmd["isgroup"], "Updated Command") else: raise LookupError(f"Custom command '{cmd_name}' not found.")
async def _alias_cmd(self, context, cmd_name: cmd_name_converter, *aliases): if len(aliases) == 0: raise commands.UserInputError( "aliases is a required argument that is missing.") parent, child, cmd_name = get_cmd_attributes(self.bot, cmd_name, True) cmd = self.bot.db[context.guild.id].select("user_commands", cmd_name) if cmd is not None: if cmd["lock"]: raise LookupError( f"Custom command '{cmd_name}' is locked and canot be edited." ) attributes = json_load_dict(cmd["attributes"]) if "aliases" not in attributes: attributes["aliases"] = [] attributes["aliases"].extend(aliases) set_new_cmd(parent, child, cmd["message"], attributes, cmd["isgroup"]) await self.after_cmd_update(context, cmd_name, cmd["message"], attributes, cmd["isgroup"], "Added Aliases") else: raise LookupError(f"Custom command '{cmd_name}' not found.")
async def _rm_cmd(self, context, cmd_name: cmd_name_converter): parent, child, cmd_name = get_cmd_attributes(self.bot, cmd_name, True, True) cmd = self.bot.db[context.guild.id].select("user_commands", cmd_name) if cmd is not None: if cmd["lock"]: raise LookupError( f"Custom command '{cmd_name}' is locked and canot be edited." ) # check the child commands if cmd["isgroup"]: childs = self.bot.db[context.guild.id].query( f"SELECT * FROM user_commands WHERE cmdname LIKE '{cmd_name} %'" ) if len(childs) > 0: raise LookupError( f"Custom command '{cmd_name}' cannot be removed because it has at least one child in db." ) parent.remove_command(child) self.bot.db[context.guild.id].delete_row("user_commands", cmd_name) await self.log_cmd_update(context, cmd_name, cmd["message"], {}, cmd["isgroup"], "Removed Command") else: raise LookupError(f"Custom command '{cmd_name}' not found.")