async def cmd_get_custom_command(msg: Message, *args): if not args: raise InvalidArgumentsException() cmd = get_custom_command(msg.channel_name, args[0].lower()) if cmd is None: await msg.reply('no command found') return await msg.reply(f'the response for "{cmd.name}" is "{cmd.response}"')
async def cmd_get_custom_command(msg: Message, *args): if not args: raise InvalidArgumentsError(reason='missing required arguments', cmd=cmd_get_custom_command) cmd = get_custom_command(msg.channel_name, args[0].lower()) if cmd is None: raise InvalidArgumentsError(reason=f'no command found for "{args[0]}"', cmd=cmd_get_custom_command) await msg.reply(f'the response for "{cmd.name}" is "{cmd.response}"')
async def cmd_add_custom_command(msg: Message, *args): if not args: raise InvalidArgumentsException() cmd = get_custom_command(msg.channel_name, args[0].lower()) if cmd is None: await msg.reply('no command found') return if delete_custom_command(msg.channel_name, cmd.name): await msg.reply(f'successfully deleted command {cmd.name}') else: await msg.reply(f'failed to delete command {cmd.name}')
async def cmd_del_custom_command(msg: Message, *args): if not args: raise InvalidArgumentsError(reason='missing required arguments', cmd=cmd_del_custom_command) cmd = get_custom_command(msg.channel_name, args[0].lower()) if cmd is None: raise InvalidArgumentsError(reason=f'no command found for "{args[0]}"', cmd=cmd_del_custom_command) if delete_custom_command(msg.channel_name, cmd.name): await msg.reply(f'successfully deleted command {cmd.name}') else: await msg.reply(f'failed to delete command {cmd.name}')
async def run_command(name: str, msg: 'Message', args: List[str] = None, blocking: bool = True, msg_class: Type['Message'] = None): """ runs a command from another command, can be used for chaining command pragmatically, the base message passed to this function MUST be a WHISPER or PRIVMSG for this to work example (called from another command) >>> # `msg` is from the first command triggered from the actual chat from the channel >>> # ['17'] is the arguments passed to this newly command, can be empty >>> await run_command('roll', msg, ['17']) :param name: the name of the command to call, if the name is not found, it tries it with the prefix added :param msg: the message instance of first command called, needed to retain state, and keep natural flow :param args: the arguments to pass to the newly called command :param blocking: if True, the caller will wait for newly called command to be done before continuing, else, run it as another task and continue """ from twitchbot import Message, get_command, get_custom_command, Command, CustomCommand, CustomCommandAction cmd: Command = get_command(name) or get_custom_command( msg.channel_name, name) if not cmd: raise ValueError( f'[run_command] could not find command or custom command by the name of "{name}"' ) if isinstance(cmd, CustomCommand): cmd = CustomCommandAction(cmd) args = [f'"{arg}"' if ' ' in arg else arg for arg in (args or [])] raw = msg.raw_msg # get the original info line from the twitch message, then append our new arguments to it to be parsed # the consecutive raw.index(':') is used to skip the first two ':' in the original message # we want to preserve all metadata/info prior to the last : before the actual message contents text = raw[:raw.index(':', raw.index(':') + 1) + 1] # create a new message from the formatted new raw text, this is needed to ensure everything flows as intended # here the base message data is used, then we replace the context with our own command and arguments new_msg = (Message if msg_class is None else msg_class)( f"{text}{cmd.fullname} {' '.join(args)}", irc=msg.irc, bot=msg.bot) if blocking: await cmd.execute(new_msg) else: get_event_loop().create_task(cmd.execute(new_msg))
async def cmd_update_custom_command(msg: Message, *args): if len(args) < 2: raise InvalidArgumentsException() name, resp = args[0], ' '.join(args[1:]) name = name.lower() if not await _verify_resp_text(msg, resp): return cmd = get_custom_command(msg.channel_name, name) if cmd is None: await msg.reply(f'custom command {name} does not exist') return cmd.response = resp session.commit() await msg.reply(f'successfully updated {cmd.name}')
async def cmd_update_custom_command(msg: Message, *args): if len(args) < 2: raise InvalidArgumentsError(reason='missing required arguments', cmd=cmd_update_custom_command) name, resp = args[0], ' '.join(args[1:]) name = name.lower() if not _verify_resp_is_valid(resp): raise InvalidArgumentsError( reason='response cannot have . or / as the starting character', cmd=cmd_update_custom_command) cmd = get_custom_command(msg.channel_name, name) if cmd is None: raise InvalidArgumentsError( reason=f'custom command "{name}" does not exist', cmd=cmd_update_custom_command) cmd.response = resp session.commit() await msg.reply(f'successfully updated {cmd.name}')