async def send_raw(ctx: commands.Context, data: dict): paginator = PartitionPaginator(prefix="```json", max_size=1985) to_send = json.dumps(data, indent=4) paginator.add_line(to_send) source = NormalPageSource(paginator.pages) menu = DCMenuPages(source) await menu.start(ctx)
async def jsk_python(self, ctx: SubContext, *, argument: codeblock_converter): arg_dict = get_var_dict_from_ctx(ctx, SCOPE_PREFIX) arg_dict["_"] = self.last_result scope = self.scope try: async with ReplResponseReactor(ctx.message): with self.submit(ctx): executor = AsyncCodeExecutor(argument.content, scope, arg_dict=arg_dict) async for send, result in AsyncSender(executor): if result is None: continue self.last_result = result if isinstance(result, discord.File): send(await ctx.send(file=result)) elif isinstance(result, discord.Embed): send(await ctx.send(embed=result)) elif isinstance(result, PaginatorInterface): send(await result.send_to(ctx)) elif isinstance(result, DCMenuPages): send(await result.start(ctx)) else: if not isinstance(result, str): # repr all non-strings result = repr(result) result = result.replace(self.bot.http.token, "[token omitted]") if result.strip() == "": result = "[empty string]" if len(result) > 2000: paginator = PartitionPaginator(prefix="```py") paginator.add_line(result) source = NormalPageSource(paginator.pages) menu = DCMenuPages(source) send(await menu.start(ctx)) else: send(await ctx.send(f"```py\n{result}```")) finally: scope.clear_intersection(arg_dict)
async def send_to_webhook(self, message): if self.bot.is_closed(): return channel = self.bot.get_channel( int(self.bot.config.discord.logging_channel)) if channel is None: # cache populating await asyncio.sleep(10) channel = self.bot.get_channel( int(self.bot.config.discord.logging_channel)) if channel is None: raise RuntimeError("Config logging_channel id wrong.") webhook: discord.Webhook = discord.utils.get(await channel.webhooks(), name="Logging") if webhook is None: webhook = await channel.create_webhook(name="Logging") pager = PartitionPaginator(prefix=None, suffix=None) pager.add_line(message) # 30 = WARNING if message.record["level"].no >= 30: if self.bot.owner_id: pager.add_line(f"<@!{self.bot.owner_id}>") else: pager.add_line(" ".join( [f"<@!{i}>" for i in self.bot.owner_ids])) if self.bot.owner_id: allowed_users = [discord.Object(id=self.bot.owner_id)] else: allowed_users = [discord.Object(id=i) for i in self.bot.owner_ids] for page in pager.pages: await webhook.send( page, username=str(message.record["module"]), allowed_mentions=discord.AllowedMentions(everyone=False, roles=False, users=allowed_users), )
async def charinfo(self, ctx: commands.Context, *, characters): """ Convert characters to name syntax, or unicode if name isn't found. """ paginator = PartitionPaginator( prefix=None, suffix=None, max_size=300, wrap_on=("}", "\n") ) final = "" for char in characters: try: name = unicodedata.name(char) final += f"\\N{{{name}}}\n" except ValueError: final += f"\\U{ord(char):0>8x}\n" paginator.add_line(final) source = NormalPageSource(paginator.pages) menu = DCMenuPages(source) await menu.start(ctx)
async def jsk_db(self, ctx: commands.Context, *, quarry: str): """ Execute a db quarry """ async with db.get_database() as connection: try: cursor = await connection.execute(quarry) except OperationalError as e: return await ctx.send(str(e)) await connection.commit() quarry_result = await cursor.fetchall() if quarry_result: collums = [coll[0] for coll in cursor.description] final = [collums] for data in quarry_result: final.append(list(data)) table = AsciiTable(final) paginator = PartitionPaginator(prefix="```sql", max_size=1000, wrap_on=("|", "\n")) paginator.add_line(table.table) source = NormalPageSource(paginator.pages) menu = DCMenuPages(source) await menu.start(ctx) else: await ctx.send("[no result]")