1
0
mirror of https://gitlab.com/ddorn/tfjm-discord-bot.git synced 2024-12-25 06:22:21 +00:00

status command

This commit is contained in:
ddorn 2020-04-29 16:27:40 +02:00
parent 8c5b4ec59f
commit 99b9fa091a
3 changed files with 40 additions and 6 deletions

View File

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

View File

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