Add commands for state monitoring

This commit is contained in:
norohind 2022-05-22 22:54:37 +03:00
parent bce9abd10a
commit e041913d5b
Signed by: norohind
GPG Key ID: 01C3BECC26FB59E1
2 changed files with 43 additions and 0 deletions

25
cogs/BotManagment.py Normal file
View File

@ -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))

18
formatting.py Normal file
View File

@ -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