コード例 #1
0
    async def _eval(self, ctx, *, code):
        code = clean_code(code)

        local_variables = {
            "discord": discord,
            "commands": commands,
            "bot": self.bot,
            "ctx": ctx,
            "channel": ctx.channel,
            "author": ctx.author,
            "guild": ctx.guild,
            "message": ctx.message
        }

        stdout = io.StringIO()

        try:
            with contextlib.redirect_stdout(stdout):
                exec(f"async def func():\n{textwrap.indent(code, '    ')}",
                     local_variables)

                obj = await local_variables["func"]()
                result = f"{stdout.getvalue()}\n-- {obj}\n"
        except Exception as e:
            result = "".join(format_exception(e, e, e.__traceback__))

        pager = Pag(
            timeout=100,
            entries=[result[i:i + 2000] for i in range(0, len(result), 2000)],
            length=1,
            prefix="```py\n",
            suffix="```")

        await pager.start(ctx)
コード例 #2
0
    async def start(self, ctx):
        # sort the questions by ID, in this context, ID is the order
        # of the questions.
        correct_answers = dict()
        code_ques = sorted(await ctx.bot.code.get_all(),
                           key=lambda d: d["_id"])

        for ques in code_ques:
            ques_id = ques["_id"]

            await ctx.send(ques["question"])
            async with aiohttp.ClientSession() as session:
                async with session.get(ques["bin"]) as resp:
                    code = await resp.text()
                    code = code.replace("\t", "    ").replace("\r\n", "\n")

            try:
                msg = await ctx.bot.wait_for(
                    "message",
                    check=lambda m: m.channel.id == ctx.channel.id and m.author
                    .id == ctx.author.id,
                    timeout=self.timeout,
                )

                content = clean_code(msg.content)
                # Replace tabs with spaces.
                content = content.replace("\t", "    ").replace("\r\n", "\n")
                # This line is supposed to clear empty lines in the
                # User's code, this is to give as much lee-way as
                # Possible with code formatting.
                # Of course, because of that, the code in the
                # database must not have any blank lines.
                content = "\n".join(
                    [text for text in content.split("\n") if text])

                if not content == code:
                    self.logger.info("\n".join([
                        li for li in difflib.ndiff(content, code)
                        if li[0] != " "
                    ]))
                    await ctx.send(
                        "Sorry, but that isn't what we're looking for! Remember that Python and the way we're "
                        "detecting code needs you to be accurate with your capitalization as well! "
                        "Make sure you're using correct python conventions and grammar. "
                        "Let's see the next one!")
                    correct_answers[ques_id] = False
                    continue
                else:
                    await ctx.send(
                        "Great! That looks good! Let's see the next one...")
                    correct_answers[ques_id] = True
                    continue
            except asyncio.TimeoutError:
                await ctx.send("Whoops! You ran out of time.")
                correct_answers[ques_id] = False
                return
        await ctx.send("Whoa! You're all done!")
        return correct_answers