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

🚧 start team creation

This commit is contained in:
ddorn 2020-04-27 14:46:04 +02:00
parent 45a74e5a71
commit 415fb88f51
3 changed files with 68 additions and 0 deletions

65
src/cogs/teams.py Normal file
View File

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

View File

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

View File

@ -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__":