コード例 #1
0
ファイル: builtin.py プロジェクト: likewhoa/PCBOT
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)
コード例 #2
0
async def on_message(message: discord.Message):
    # wait until ready
    await client.wait_until_ready()

    # don't care for bots
    if message.author.bot:
        return

    command_prefix = "."  # TODO hardkodet prefix
    content = message.content[len(command_prefix):]

    # make sure this is a command
    if not message.content.startswith(
            command_prefix) or not content or content.startswith(" "):
        return

    content_words = content.split()
    cmd_name, given_args = content_words[0], content_words[1:]

    command = plugins.get_command(cmd_name)
    if not command:
        log.info("not a command: {}".format(cmd_name))
        return

    cmd_args, complete = parse_given_args(command, message, given_args)
    if not complete:
        log.info("missing arguments: {}".format(cmd_args))
        return

    client.loop.create_task(run_command(command, message, *cmd_args))
コード例 #3
0
ファイル: bot.py プロジェクト: likewhoa/PCBOT
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))
コード例 #4
0
ファイル: bot.py プロジェクト: PcBoy111/PCBOT
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 not message.channel.is_private 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
    cmd = ""
    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):]

    # Handle commands
    for plugin in plugins.all_values():
        # If there was a command and the bot can send messages in the channel, parse the command
        if not cmd:
            continue
        command = plugins.get_command(plugin, cmd)

        if command:
            parsed_command, args, kwargs = await parse_command(command, cmd_args, message)

            if parsed_command:
                log_message(message)  # Log the command
                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))
コード例 #5
0
async def help_(message: discord.Message, command: str.lower = None, *args):
    """ Display commands or their usage and description. """
    command_prefix = config.server_command_prefix(message.server)

    # Display the specific command
    if command:
        if command.startswith(command_prefix):
            command = command[len(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, plugins.format_help(cmd, message.server))

    # Display every command
    else:
        commands = []

        for plugin in plugins.all_values():
            # Only go through plugins with actual commands
            if not getattr(plugin, "__commands", False):
                continue

            # Add all commands that the user can use
            for cmd in plugin.__commands:
                if not cmd.hidden and plugins.can_use_command(
                        cmd, message.author, message.channel):
                    commands.append(cmd.name_prefix(message.server).split()[0])

        commands = ", ".join(sorted(commands))

        m = "**Commands**: ```{0}```Use `{1}help <command>`, `{1}<command> {2}` or " \
            "`{1}<command> {3}` for command specific help.".format(
            commands, command_prefix, *config.help_arg)
        await client.say(message, m)
コード例 #6
0
ファイル: builtin.py プロジェクト: xZwop/PCBOT
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)
コード例 #7
0
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.utcnow()

    # Make a local copy of the message since some attributes are changed and they shouldn't be overridden
    # in plugin based on_message events
    original_message = message
    message = copy(message)

    # 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

    # Find server specific settings
    command_prefix = config.server_command_prefix(message.server)
    case_sensitive = config.server_case_sensitive_commands(message.server)

    # Check that the message is a command
    if not message.content.startswith(command_prefix):
        return

    # Remove the prefix and make sure that a command was actually specified
    message.content = message.content[len(command_prefix):]
    if not message.content or message.content.startswith(" "):
        return

    # Split content into arguments by space (surround with quotes for spaces)
    cmd_args = utils.split(message.content)

    # Try finding a command object using the command name (first argument)
    command = plugins.get_command(cmd_args[0], case_sensitive=case_sensitive)
    if not command:
        return

    try:
        # Find the subcommand if there is one
        command = plugins.get_sub_command(command,
                                          *cmd_args[1:],
                                          case_sensitive=case_sensitive)

        # Check that the author is allowed to use the command
        if not plugins.can_use_command(command, message.author,
                                       message.channel):
            return

        # Parse the command with the user's arguments
        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 plugins.format_help(
                command, message.server, no_subcommand=True))
        log_message(message)
        return

    if not parsed_command:
        return

    # Log the command executed and execute said command
    log_message(original_message)
    client.loop.create_task(
        execute_command(parsed_command, original_message, *args, **kwargs))

    # Manually dispatch an event for when commands are requested
    client.dispatch("command_requested", message, parsed_command, *args,
                    **kwargs)

    # Log time spent parsing the command
    stop_time = datetime.utcnow()
    time_elapsed = (stop_time - start_time).total_seconds() * 1000
    logging.debug("Time spent parsing command: {elapsed:.6f}ms".format(
        elapsed=time_elapsed))