diff --git a/src/cogs/misc.py b/src/cogs/misc.py index 077767a..4192c1f 100644 --- a/src/cogs/misc.py +++ b/src/cogs/misc.py @@ -1,10 +1,11 @@ import itertools import random -import sys -import traceback +import datetime from operator import attrgetter +from time import time import discord +from discord import Guild from discord.ext.commands import ( Cog, command, @@ -13,13 +14,10 @@ from discord.ext.commands import ( Command, CommandError, Group, - CommandInvokeError, - CommandNotFound, - MissingRole, ) from src.constants import * -from src.errors import UnwantedCommand +from src.utils import has_role class MiscCog(Cog, name="Divers"): @@ -53,6 +51,31 @@ class MiscCog(Cog, name="Divers"): msg = random.choice(jokes) await ctx.send(msg) + @command(name="status") + async def status_cmd(self, ctx: Context): + guild: Guild = ctx.guild + embed = discord.Embed(title="État du serveur", color=EMBED_COLOR) + benevoles = [g for g in guild.members if has_role(g, Role.BENEVOLE)] + participants = [g for g in guild.members if has_role(g, Role.PARTICIPANT)] + no_role = [g for g in guild.members if g.top_role == guild.default_role] + uptime = datetime.timedelta(seconds=round(time() - START_TIME)) + + infos = { + "Bénévoles": len(benevoles), + "Participants": len(participants), + "Sans rôle": len(no_role), + "Total": len(guild.members), + "Bot uptime": uptime, + } + + width = max(map(len, infos)) + txt = "\n".join( + f"`{key.rjust(width)}`: {value}" for key, value in infos.items() + ) + embed.add_field(name="Stats", value=txt) + + await ctx.send(embed=embed) + # ----------------- Help ---------------- # @command(name="help", aliases=["h"]) diff --git a/src/constants.py b/src/constants.py index 5643c39..6436952 100644 --- a/src/constants.py +++ b/src/constants.py @@ -13,8 +13,12 @@ __all__ = [ "TEAMS_CHANNEL_CATEGORY", "DIEGO", "TOURNOIS", + "EMBED_COLOR", + "START_TIME", ] +from time import time + TOKEN = os.environ.get("TFJM_DISCORD_TOKEN") if TOKEN is None: @@ -64,3 +68,6 @@ JOKES_FILE = TOP_LEVEL_DIR / "data" / "jokes" with open(TOP_LEVEL_DIR / "data" / "problems") as f: PROBLEMS = f.read().splitlines() MAX_REFUSE = len(PROBLEMS) - 4 # -5 usually but not in 2020 because of covid-19 + +EMBED_COLOR = 0xFFA500 +START_TIME = time() diff --git a/src/utils.py b/src/utils.py new file mode 100644 index 0000000..06c2096 --- /dev/null +++ b/src/utils.py @@ -0,0 +1,4 @@ +def has_role(member, role: str): + """Return whether the member has a role with this name.""" + + return any(r.name == role for r in member.roles)