mirror of
				https://gitlab.com/ddorn/tfjm-discord-bot.git
				synced 2025-11-04 03:42:12 +01:00 
			
		
		
		
	✨ status command
This commit is contained in:
		@@ -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"])
 | 
			
		||||
 
 | 
			
		||||
@@ -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()
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										4
									
								
								src/utils.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										4
									
								
								src/utils.py
									
									
									
									
									
										Normal file
									
								
							@@ -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)
 | 
			
		||||
		Reference in New Issue
	
	Block a user