Exemplo n.º 1
0
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 plugins.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 plugins.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
Exemplo n.º 2
0
async def do(message: discord.Message, python_code: Annotate.Code):
    """ Execute python code. """
    code_globals.update(
        dict(message=message,
             client=client,
             author=message.author,
             server=message.server,
             channel=message.channel))

    # Create an async function so that we can await it using the result of eval
    python_code = "async def do_session():\n    " + "\n    ".join(
        line for line in python_code.split("\n"))
    try:
        exec(python_code, code_globals)
    except SyntaxError as e:
        await client.say(message, "```" + utils.format_syntax_error(e) + "```")
        return

    before = datetime.now()
    try:
        result = await eval("do_session()", code_globals)
    except Exception as e:
        await client.say(message, "```" + utils.format_exception(e) + "```")
    else:
        if result:
            await send_result(message.channel, result, datetime.now() - before)
Exemplo n.º 3
0
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
Exemplo n.º 4
0
async def eval_(message: discord.Message, python_code: Annotate.Code):
    """ Evaluate a python expression. Can be any python code on one
    line that returns something. Coroutine generators will by awaited. """
    code_globals.update(dict(message=message, client=client,
                             author=message.author, server=message.server, channel=message.channel))

    try:
        result = eval(python_code, code_globals)
        if inspect.isawaitable(result):
            result = await result
    except SyntaxError as e:
        result = utils.format_syntax_error(e)
    except Exception as e:
        result = utils.format_exception(e)

    await client.say(message, "**Result:** \n```{}\n```".format(result))
Exemplo n.º 5
0
async def eval_(message: discord.Message, python_code: Annotate.Code):
    """ Evaluate a python expression. Can be any python code on one
    line that returns something. Coroutine generators will by awaited. """
    code_globals.update(dict(message=message, client=client,
                             author=message.author, server=message.server, channel=message.channel))

    before = datetime.now()
    try:
        result = eval(python_code, code_globals)
        if inspect.isawaitable(result):
            result = await result
    except SyntaxError as e:
        result = utils.format_syntax_error(e)
    except Exception as e:
        result = utils.format_exception(e)

    await send_result(message.channel, result, datetime.now() - before)
Exemplo n.º 6
0
async def do(message: discord.Message, python_code: Annotate.Code):
    """ Execute python code. """
    code_globals.update(dict(message=message, client=client,
                             author=message.author, server=message.server, channel=message.channel))

    # Create an async function so that we can await it using the result of eval
    python_code = "async def do_session():\n    " + "\n    ".join(line for line in python_code.split("\n"))
    try:
        exec(python_code, code_globals)
    except SyntaxError as e:
        await client.say(message, "```" + utils.format_syntax_error(e) + "```")
        return

    try:
        result = await eval("do_session()", code_globals)
    except Exception as e:
        await client.say(message, "```" + utils.format_exception(e) + "```")
    else:
        if result:
            await client.say(message, "**Result:** \n```{}\n```".format(result))