示例#1
0
文件: builtin.py 项目: xZwop/PCBOT
def on_message(client: discord.Client, message: discord.Message):
    """ Perform lambda commands. """
    args = utils.split(message.content)

    # Check if the command is a lambda command and is not disabled (in the blacklist)
    if args[0] in lambdas.data and args[0] not in lambda_config.data["blacklist"]:
        def say(msg, m=message):
            asyncio.async(client.say(m, msg))

        def arg(i, default=0):
            if len(args) > i:
                return args[i]
            else:
                return default

        code_globals.update(dict(arg=arg, say=say, args=args, message=message, client=client))

        # Execute the command
        try:
            exec(lambdas.data[args[0]], code_globals)
        except Exception as e:
            if utils.is_owner(message.author):
                say("```" + utils.format_exception(e) + "```")
            else:
                logging.warn("An exception occurred when parsing lambda command:"
                             "\n{}".format(utils.format_exception(e)))

        return True
示例#2
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)
示例#3
0
文件: builtin.py 项目: likewhoa/PCBOT
async def on_message(message: discord.Message):
    """ Perform lambda commands. """
    args = utils.split(message.content)

    # Check if the command is a lambda command and is not disabled (in the blacklist)
    if args[0] in lambdas.data and args[0] not in lambda_config.data["blacklist"]:
        def arg(i, default=0):
            if len(args) > i:
                return args[i]
            else:
                return default

        code_globals.update(dict(arg=arg, args=args, message=message, client=client,
                                 author=message.author, server=message.server, channel=message.channel))
        python_code = lambdas.data[args[0]]

        # Create an async function so that we can await it using the result of eval
        python_code = "async def lambda_session():\n    " + "\n    ".join(line for line in python_code.split("\n"))
        try:
            exec(python_code, code_globals)
        except SyntaxError as e:
            if utils.is_owner(message.author):
                await client.say(message, "```" + utils.format_syntax_error(e) + "```")
            else:
                logging.warning("An exception occurred when parsing lambda command:"
                                "\n{}".format(utils.format_syntax_error(e)))
            return True

        # Execute the command
        try:
            await eval("lambda_session()", code_globals)
        except AssertionError as e:  # Send assertion errors to the core module
            raise AssertionError(e)
        except Exception as e:
            if utils.is_owner(message.author):
                await client.say(message, "```" + utils.format_exception(e) + "```")
            else:
                logging.warning("An exception occurred when parsing lambda command:"
                                "\n{}".format(utils.format_exception(e)))

        return True
示例#4
0
文件: builtin.py 项目: PcBoy111/PCBOT
async def on_message(message: discord.Message):
    """ Perform lambda commands. """
    args = utils.split(message.content)

    # Check if the command is a lambda command and is not disabled (in the blacklist)
    if args[0] in lambdas.data and args[0] not in lambda_config.data["blacklist"]:
        def arg(i, default=0):
            if len(args) > i:
                return args[i]
            else:
                return default

        code_globals.update(dict(arg=arg, args=args, message=message, client=client,
                                 author=message.author, server=message.server, channel=message.channel))
        python_code = lambdas.data[args[0]]

        # Create an async function so that we can await it using the result of eval
        python_code = "async def lambda_session():\n    " + "\n    ".join(line for line in python_code.split("\n"))
        try:
            exec(python_code, code_globals)
        except SyntaxError as e:
            if utils.is_owner(message.author):
                await client.say(message, "```" + utils.format_syntax_error(e) + "```")
            else:
                logging.warn("An exception occurred when parsing lambda command:"
                             "\n{}".format(utils.format_syntax_error(e)))
            return True

        # Execute the command
        try:
            await eval("lambda_session()", code_globals)
        except AssertionError as e:  # Send assertion errors to the core module
            raise AssertionError(e)
        except Exception as e:
            if utils.is_owner(message.author):
                await client.say(message, "```" + utils.format_exception(e) + "```")
            else:
                logging.warn("An exception occurred when parsing lambda command:"
                             "\n{}".format(utils.format_exception(e)))
        finally:
            return True
示例#5
0
文件: osu.py 项目: PcBoy111/PCBOT
async def unlink(message: discord.Message, member: Annotate.Member=Annotate.Self):
    """ Unlink your osu! account or unlink the member specified (**Owner only**). """
    # The message author is allowed to unlink himself
    # If a member is specified and the member is not the owner, set member to the author
    if not utils.is_owner(message.author):
        member = message.author

    # The member might not be linked to any profile
    assert member.id in osu_config.data["profiles"], "No osu! profile assigned to **{}**!".format(member.name)

    # Unlink the given member (usually the message author)
    del osu_config.data["profiles"][member.id]
    osu_config.save()
    await client.say(message, "Unlinked **{}'s** osu! profile.".format(member.name))
示例#6
0
文件: osu.py 项目: likewhoa/PCBOT
async def unlink(message: discord.Message,
                 member: Annotate.Member = Annotate.Self):
    """ Unlink your osu! account or unlink the member specified (**Owner only**). """
    # The message author is allowed to unlink himself
    # If a member is specified and the member is not the owner, set member to the author
    if not utils.is_owner(message.author):
        member = message.author

    # The member might not be linked to any profile
    assert member.id in osu_config.data[
        "profiles"], "No osu! profile assigned to **{}**!".format(member.name)

    # Unlink the given member (usually the message author)
    del osu_config.data["profiles"][member.id]
    osu_config.save()
    await client.say(message,
                     "Unlinked **{}'s** osu! profile.".format(member.name))
示例#7
0
文件: bot.py 项目: likewhoa/PCBOT
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))
示例#8
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)