From 415fb88f5148179384bda980b58dc141aa47957a Mon Sep 17 00:00:00 2001 From: ddorn Date: Mon, 27 Apr 2020 14:46:04 +0200 Subject: [PATCH] :construction: start team creation --- src/cogs/teams.py | 65 +++++++++++++++++++++++++++++++++++++++++ src/constants.py | 2 ++ src/tfjm_discord_bot.py | 1 + 3 files changed, 68 insertions(+) create mode 100644 src/cogs/teams.py diff --git a/src/cogs/teams.py b/src/cogs/teams.py new file mode 100644 index 0000000..d681998 --- /dev/null +++ b/src/cogs/teams.py @@ -0,0 +1,65 @@ +from collections import namedtuple + +import discord +from discord.ext import commands +from discord.ext.commands import Cog, Bot, group, Context +from discord.utils import get, find + +from src.constants import * + + +Team = namedtuple("Team", ["name", "trigram", "tournoi", "secret", "status"]) + + +class TeamsCog(Cog, name="Teams"): + def __init__(self): + self.teams = self.load_teams() + + def load_teams(self): + with open(TEAMS_FILE) as f: + # first line is header + lines = f.read().splitlines()[1:] + teams = [ + Team(*[field.strip('"') for field in line.split(";")]) for line in lines + ] + return teams + + @group(name="team") + async def team(self, ctx): + """Groupe de commandes pour la gestion des équipes.""" + + @team.command(name="create") + async def create_team(self, ctx: Context, trigram, team_secret): + await ctx.message.delete() + + team: Team = get(self.teams, trigram=trigram) + role: discord.Role = get(ctx.guild.roles, name=trigram) + captain_role = get(ctx.guild.roles, name=Role.CAPTAIN) + + if team is None: + await ctx.send( + f"{ctx.author.mention}: le trigram `{trigram}` " + f"n'est pas valide. Es-tu sûr d'avoir le bon ?" + ) + elif role is not None: + # Team exists + captain = find(lambda m: captain_role in m.roles, role.members) + await ctx.send( + f"{ctx.author.mention}: l'équipe {trigram} " + f"existe déjà. Tu peux demander a ton capitaine " + f"{captain.mention} de t'ajouter à l'équipe avec " + f"`!team add {ctx.author.mention}`" + ) + elif team_secret != team.secret: + await ctx.send( + f"{ctx.author.mention}: ton secret n'est pas valide, " + f"Tu peux le trouver sur https://inscription.tfjm.org/mon-equipe." + ) + else: + # Team creation ! + + await ctx.send(f"Creation de l'équipe {trigram} avec {team_secret} !") + + +def setup(bot: Bot): + bot.add_cog(TeamsCog()) diff --git a/src/constants.py b/src/constants.py index 9ccc487..75459ea 100644 --- a/src/constants.py +++ b/src/constants.py @@ -8,6 +8,7 @@ __all__ = [ "MAX_REFUSE", "ROUND_NAMES", "TIRAGES_FILE", + "TEAMS_FILE", ] TOKEN = os.environ.get("TFJM_DISCORD_TOKEN") @@ -36,6 +37,7 @@ ROUND_NAMES = ["premier tour", "deuxième tour"] TOP_LEVEL_DIR = Path(__file__).parent.parent TIRAGES_FILE = TOP_LEVEL_DIR / "data" / "tirages.yaml" +TEAMS_FILE = TOP_LEVEL_DIR / "data" / "teams" with open(TOP_LEVEL_DIR / "data" / "problems") as f: PROBLEMS = f.read().splitlines() diff --git a/src/tfjm_discord_bot.py b/src/tfjm_discord_bot.py index 676d493..8d9be98 100644 --- a/src/tfjm_discord_bot.py +++ b/src/tfjm_discord_bot.py @@ -98,6 +98,7 @@ async def on_command_error(ctx: Context, error, *args, **kwargs): bot.load_extension("src.cogs.tirages") +bot.load_extension("src.cogs.teams") if __name__ == "__main__":