meta.py 文件源码

python
阅读 33 收藏 0 点赞 0 评论 0

项目:Harmonbot 作者: Harmon758 项目源码 文件源码
def repl(self, ctx):
        variables = {"self" : self, "ctx" : ctx, "last" : None}
        await self.bot.embed_reply("Enter code to execute or evaluate\n`exit` or `quit` to exit")
        while True:
            message = await self.bot.wait_for_message(author = ctx.message.author, channel = ctx.message.channel, check = lambda m: m.content.startswith('`'))
            if message.content.startswith("```py") and message.content.endswith("```"):
                code = message.content[5:-3].strip(" \n")
            else:
                code = message.content.strip("` \n")
            if code in ("quit", "exit", "quit()", "exit()"):
                await self.bot.embed_reply('Exiting repl')
                return
            function = exec
            if '\n' not in code:
                try:
                    code = compile(code, "<repl>", "eval")
                except SyntaxError:
                    pass
                else:
                    function = eval
            if function is exec:
                try:
                    code = compile(code, "<repl>", "exec")
                except SyntaxError as e:
                    await self.bot.reply(clients.py_code_block.format("{0.text}{1:>{0.offset}}\n{2}: {0}".format(e, '^', type(e).__name__)))
                    continue
            try:
                result = function(code, variables)
                if inspect.isawaitable(result):
                    result = await result
            except:
                await self.bot.reply(clients.py_code_block.format("\n".join(traceback.format_exc().splitlines()[-2:]).strip()))
            else:
                if function is eval:
                    try:
                        await self.bot.reply(clients.py_code_block.format(result))
                    except Exception as e:
                        await self.bot.reply(clients.py_code_block.format("{}: {}".format(type(e).__name__, e)))
                variables["last"] = result
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号