コード例 #1
0
 async def reload(self, ctx, *modules):
     done = {}
     for mod in modules:
         if mod in self.bot.extensions:
             try:
                 self.bot.reload_extension(mod)
                 done[mod] = True
             except commands.NoEntryPointError:
                 done[mod] = False
             except Exception as exc:
                 done[mod] = format_exc(getattr(exc, '__cause__', exc) or exc)
         else:
             try:
                 self.bot.load_extension(mod)
                 done[mod] = True
             except commands.NoEntryPointError:
                 done[mod] = False
             except Exception as exc:
                 done[mod] = format_exc(getattr(exc, '__cause__', exc) or exc)
     if all(z is True for z in done.values()):
         return await ctx.message.add_reaction(self.bot.tick_yes)
     fmt = []
     for module, success in done.items():
         if success is True:
             fmt.append(f"{self.bot.tick_yes} {module}\n")
         elif success is False:
             fmt.append(f'\U0001f504 {module}\n')
         else:
             fmt.append(f"{self.bot.tick_no} {module}\n```py\n{success}\n```\n")
     await ctx.send_as_paginator('\n'.join(fmt))
コード例 #2
0
 async def try_(self, ctx, *, command):
     nmsg = copy.copy(ctx.message)
     nmsg.content = ctx.prefix+command
     nctx = await self.bot.get_context(nmsg)
     try:
         await self.bot.invoke(nctx)
     except Exception as exc:
         await ctx.send_as_paginator(format_exc(exc), codeblock=True)
コード例 #3
0
 async def redis(self, ctx, cmd, *args):
     args = [int(a) if a.isdigit() else a.format(ctx=ctx) for a in args]
     try:
         func = getattr(self.bot.redis, cmd)
         ret = await func(*args)
     except Exception as exc:
         await ctx.message.add_reaction(self.bot.tick_no)
         await ctx.send_as_paginator(format_exc(exc), codeblock=True)
     else:
         await ctx.message.add_reaction(self.bot.tick_yes)
         if not ret:
             return
         ret = recursive_decode(ret)
         await ctx.send_as_paginator(pformat(ret), codeblock=True)
コード例 #4
0
    async def src(self, ctx, *, command):
        cmd = self.bot.get_command(command)
        if not cmd:
            try:
                obj = import_expression.eval(command, {})
            except Exception as exc:
                await ctx.message.add_reaction(self.bot.tick_no)
                return await ctx.send_as_paginator(format_exc(exc), codeblock=True, destination=ctx.author)
        else:
            obj = cmd.callback

        lines, firstlno = inspect.getsourcelines(obj)
        paginator = BetterPaginator('```py\n', '```')
        for lno, line in enumerate(lines, start=firstlno):
            paginator.add_line(f'{lno}\t{line.rstrip()}'.replace('``', '`\u200b`'))
        await PaginationHandler(self.bot, paginator, no_help=True).start(ctx)
コード例 #5
0
    async def eval(self, ctx, *, code_string):
        if not self._env:
            self._env.update({"discord": discord,
                              "commands": commands, '_': None,
                              import_expression.constants.IMPORTER: importlib.import_module})
        self._env['ctx'] = ctx
        try:
            expr = import_expression.compile(code_string)
            ret = eval(expr, self._env)
        except SyntaxError:
            pass
        except Exception as exc:
            await ctx.message.add_reaction(self.bot.tick_no)
            return await ctx.send_as_paginator(format_exc(exc), codeblock=True)
        else:
            if inspect.isawaitable(ret):
                fut = asyncio.ensure_future(ret, loop=self.bot.loop)
                self._evals.append(fut)
                try:
                    with Timer(ctx.message):
                        ret = await asyncio.wait_for(fut, timeout=self._timeout)
                except Exception as exc:
                    await ctx.message.add_reaction(self.bot.tick_no)
                    return await ctx.send_as_paginator(format_exc(exc), codeblock=True)
                finally:
                    self._evals.remove(fut)
            await ctx.message.add_reaction(self.bot.tick_yes)
            if ret is None:
                return
            self._env['_'] = ret
            if isinstance(ret, discord.Embed):
                return await ctx.send(embed=ret)
            if not isinstance(ret, str):
                ret = repr(ret)
            return await ctx.send_as_paginator(ret, codeblock=self._send_in_codeblocks)
        code = f"""async def __func__():
    try:
{textwrap.indent(code_string, '        ')}
    finally:
        globals().update(locals())"""
        try:
            import_expression.exec(code, self._env)
        except Exception as exc:
            await ctx.message.add_reaction(self.bot.tick_no)
            return await ctx.send_as_paginator(format_exc(exc), codeblock=True)

        func = self._env.pop('__func__')
        fut = asyncio.ensure_future(func(), loop=self.bot.loop)
        self._evals.append(fut)
        try:
            with Timer(ctx.message):
                await asyncio.wait_for(fut, timeout=self._timeout)
        except asyncio.CancelledError:
            await ctx.message.add_reaction('\U0001f6d1')
            return
        except asyncio.TimeoutError:
            await ctx.message.add_reaction('\u23f0')
            return
        except Exception as exc:
            await ctx.message.add_reaction(self.bot.tick_no)
            return await ctx.send_as_paginator(format_exc(exc), codeblock=True)
        else:
            ret = fut.result()
        finally:
            self._evals.remove(fut)

        await ctx.message.add_reaction(self.bot.tick_yes)

        if ret is None:
            return

        self._env['_'] = ret

        if isinstance(ret, discord.Embed):
            return await ctx.send(embed=ret)

        if not isinstance(ret, str):
            ret = repr(ret)

        return await ctx.send_as_paginator(ret, codeblock=self._send_in_codeblocks)