async def parse_command(command: plugins.Command, cmd_args: list, message: discord.Message): """ Try finding a command """ command = plugins.get_sub_command(command, cmd_args[1:]) cmd_args = cmd_args[command.depth:] send_help = False # If the last argument ends with the help argument, skip parsing and display help if cmd_args[-1] in config.help_arg or (command.disabled_pm and message.channel.is_private): complete = False args, kwargs = [], {} send_help = True else: # Parse the command and return the parsed arguments args, kwargs, complete = parse_command_args(command, cmd_args, message) # If command parsing failed, display help for the command or the error message if not complete: log_message(message) # Log the command if command.disabled_pm and message.channel.is_private: await client.say(message, "This command can not be executed in a private message.") else: if command.error and len(cmd_args) > 1 and not send_help: await client.say(message, command.error) else: if len(cmd_args) == 1: send_help = True await client.say(message, utils.format_help(command, no_subcommand=False if send_help else True)) command = None return command, args, kwargs
async def help_(message: discord.Message, command: str.lower=None, *args): """ Display commands or their usage and description. """ # Display the specific command if command: if command.startswith(config.command_prefix): command = command[len(config.command_prefix):] cmd = plugins.get_command(command) if not cmd: return # Get the specific command with arguments and send the help cmd = plugins.get_sub_command(cmd, *args) await client.say(message, utils.format_help(cmd)) # Display every command else: commands = [] for plugin in plugins.all_values(): if getattr(plugin, "__commands", False): # Massive pile of shit that works (so sorry) commands.extend( cmd.name_prefix.split()[0] for cmd in plugin.__commands if not cmd.hidden and (not getattr(getattr(cmd, "function"), "owner", False) or utils.is_owner(message.author)) ) commands = ", ".join(sorted(commands)) m = "**Commands**: ```{0}```Use `{1}help <command>`, `{1}<command> {2}` or " \ "`{1}<command> {3}` for command specific help.".format( commands, config.command_prefix, *config.help_arg) await client.say(message, m)
async def on_message(message: discord.Message): """ What to do on any message received. The bot will handle all commands in plugins and send on_message to plugins using it. """ # Make sure the client is ready before processing commands await client.wait_until_ready() start_time = datetime.now() # We don't care about channels we can't write in as the bot usually sends feedback if message.server and message.server.owner and not message.server.me.permissions_in( message.channel).send_messages: return # Don't accept commands from bot accounts if message.author.bot: return # Split content into arguments by space (surround with quotes for spaces) cmd_args = utils.split(message.content) # Get command name if cmd_args[0].startswith(config.command_prefix) and len( cmd_args[0]) > len(config.command_prefix): cmd = cmd_args[0][len(config.command_prefix):] else: return # Try finding a command object command = plugins.get_command(cmd) if not command: return # Parse the command with the user's arguments try: parsed_command, args, kwargs = await parse_command( command, cmd_args, message) except AssertionError as e: # Return any feedback given from the command via AssertionError, or the command help await client.send_message( message.channel, str(e) or utils.format_help(command, no_subcommand=True)) log_message(message) return if not parsed_command: return log_message(message) client.loop.create_task( execute_command(parsed_command, message, *args, **kwargs)) # Run command # Log time spent parsing the command stop_time = datetime.now() time_elapsed = (stop_time - start_time).total_seconds() / 1000 logging.debug("Time spent parsing command: {elapsed:.6f}ms".format( elapsed=time_elapsed))
def execute_command(command: plugins.Command, message: discord.Message, *args, **kwargs): """ Execute a command and send any AttributeError exceptions. """ try: yield from command.function(client, message, *args, **kwargs) except AssertionError as e: yield from client.say(message, str(e) or command.error or utils.format_help(command)) except: yield from client.say(message, "An error occurred while executing this command. If the error persists, " "please send a PM to {}.".format(config.creator)) print_exc()
async def execute_command(command: plugins.Command, message: discord.Message, *args, **kwargs): """ Execute a command and send any AttributeError exceptions. """ app_info = await client.application_info() try: await command.function(message, *args, **kwargs) except AssertionError as e: await client.say(message, str(e) or command.error or utils.format_help(command)) except: await client.say(message, "An error occurred while executing this command. If the error persists, " "please send a PM to {}.".format(app_info.owner)) print_exc()
async def execute_command(command: plugins.Command, message: discord.Message, *args, **kwargs): """ Execute a command and send any AttributeError exceptions. """ app_info = await client.application_info() try: await command.function(message, *args, **kwargs) except AssertionError as e: await client.say(message, str(e) or command.error or utils.format_help(command)) except: traceback.print_exc() if utils.is_owner(message.author) and config.owner_error: await client.say(message, utils.format_code(traceback.format_exc())) else: await client.say( message, "An error occurred while executing this command. If the error persists, " "please send a PM to {}.".format(app_info.owner))
async def parse_command(command: plugins.Command, cmd_args: list, message: discord.Message): """ Try finding a command """ command = plugins.get_sub_command(command, *cmd_args[1:]) cmd_args = cmd_args[command.depth:] send_help = False # If the last argument ends with the help argument, skip parsing and display help if cmd_args[-1] in config.help_arg or (command.disabled_pm and message.channel.is_private): complete = False args, kwargs = [], {} send_help = True else: # Parse the command and return the parsed arguments args, kwargs, complete = await parse_command_args( command, cmd_args, message) # If command parsing failed, display help for the command or the error message if not complete: log_message(message) # Log the command if command.disabled_pm and message.channel.is_private: await client.say( message, "This command can not be executed in a private message.") else: if command.error and len(cmd_args) > 1 and not send_help: await client.say(message, command.error) else: if len(cmd_args) == 1: send_help = True await client.say( message, utils.format_help( command, no_subcommand=False if send_help else True)) command = None return command, args, kwargs
def help_(client: discord.Client, message: discord.Message, command: str.lower=None, *args): """ Display commands or their usage and description. """ # Display the specific command if command: if command.startswith(config.command_prefix): command = command[1:] for plugin in plugins.all_values(): cmd = plugins.get_command(plugin, command) if not cmd: continue # Get the specific command with arguments and send the help cmd = plugins.get_sub_command(cmd, args) yield from client.say(message, utils.format_help(cmd)) break # Display every command else: commands = [] for plugin in plugins.all_values(): if getattr(plugin, "__commands", False): # Massive pile of shit that works (so sorry) commands.extend( cmd.name_prefix.split()[0] for cmd in plugin.__commands if not cmd.hidden and (not getattr(getattr(cmd, "function"), "__owner__", False) or utils.is_owner(message.author)) ) commands = ", ".join(sorted(commands)) m = "**Commands**:```{0}```Use `{1}help <command>`, `{1}<command> {2}` or " \ "`{1}<command> {3}` for command specific help.".format( commands, config.command_prefix, *config.help_arg) yield from client.say(message, m)