diff --git a/cogs/BotManagment.py b/cogs/BotManagment.py new file mode 100644 index 0000000..caf23b6 --- /dev/null +++ b/cogs/BotManagment.py @@ -0,0 +1,25 @@ +# -*- coding: utf-8 -*- +from formatting import format_table +from discord.ext import commands +from discord.ext.commands import Context + + +class BotManagement(commands.Cog): + def __init__(self, bot: commands.Bot): + self.bot = bot + + @commands.is_owner() + @commands.command('listServers') + async def list_servers(self, ctx: Context): + text_table = format_table(((server.name, str(server.id)) for server in self.bot.guilds), ('name', 'id')) + await ctx.channel.send(text_table) + + @commands.is_owner() + @commands.command('listVoice') + async def list_voice_connections(self, ctx: Context): + text_table = format_table(((it.guild.name,) for it in self.bot.voice_clients)) + await ctx.channel.send(text_table) + + +async def setup(bot): + await bot.add_cog(BotManagement(bot)) diff --git a/formatting.py b/formatting.py new file mode 100644 index 0000000..6a0e3c3 --- /dev/null +++ b/formatting.py @@ -0,0 +1,18 @@ +from typing import Iterable + + +class MISSING: + pass + + +def format_table(data: Iterable[Iterable[str]], header: Iterable[str] = MISSING) -> str: + if header != MISSING: + data = (header, *data) + + result = '```\n' + for row in data: + row = [item.replace('`', '\\`') for item in row] + result += '\t'.join(row) + '\n' + + result += '```' + return result