From 89045362a514a7915d3b5c13e8ae4756b0ed2fa4 Mon Sep 17 00:00:00 2001 From: "Karina J. Kwiatek" Date: Thu, 15 Aug 2024 10:07:08 +0200 Subject: [PATCH] Add command for exporting teams --- src/bot.py | 3 ++- src/commands/team.py | 55 +++++++++++++++++++++++++++++++++++++++++++- src/constants.py | 1 + 3 files changed, 57 insertions(+), 2 deletions(-) diff --git a/src/bot.py b/src/bot.py index 4d21cbf..25b156f 100644 --- a/src/bot.py +++ b/src/bot.py @@ -17,7 +17,7 @@ PASSWORDS_CHANNEL_NAME, ) from src.commands.join import join -from src.commands.team import Team, new_team, delete_team, create_voice, create_team_channel +from src.commands.team import Team, new_team, delete_team, create_voice, create_team_channel, export_team class BotClient(discord.Client): @@ -50,6 +50,7 @@ def __init__( team.add_command(delete_team) team.add_command(create_voice) team.add_command(create_team_channel) + team.add_command(export_team) self.tree.add_command(team, guild=self.guild) self.tree.add_command(join, guild=self.guild) diff --git a/src/commands/team.py b/src/commands/team.py index 344fd02..85c740b 100644 --- a/src/commands/team.py +++ b/src/commands/team.py @@ -12,7 +12,7 @@ ROLE_PREFIX, TEAM_CATEGORY_NAME, PASSWORDS_CHANNEL_NAME, - TEAM_VOICE_CATEGORY_NAME, + TEAM_VOICE_CATEGORY_NAME, TEAM_LEADER_ROLE, ) TEAM_CREATED_REASON = "Created via command by " @@ -186,3 +186,56 @@ async def create_team_channel( reason=TEAM_CREATED_REASON ) await interaction.response.send_message(f"{new_channel.mention} created!", ephemeral=True) + + +@group.command( + name='export', + description='Outputs all commands needed to create a team (or all teams)', +) +@app_commands.describe( + tla='Three Letter Acronym (e.g. SRZ)', + only_teams="Only creates teams without extra channels", +) +async def export_team( + interaction: discord.interactions.Interaction["BotClient"], + tla: str | None = None, + only_teams: bool = False, +) -> None: + guild: discord.Guild | None = interaction.guild + if guild is None: + raise app_commands.NoPrivateMessage() + + await interaction.response.defer(thinking=True, ephemeral=True) + + async def _find_password(team_tla: str) -> str: + async for team_name, password in interaction.client.load_passwords(): + if team_name == team_tla: + return password + + async def _export_team(team_tla: str) -> str: + main_channel = discord.utils.get(guild.text_channels, name=f"team-{team_tla.lower()}") + password = await _find_password(team_tla) + commands = [f"/team new tla:{team_tla} name:{main_channel.topic} password:{password}"] + + if not only_teams: + channels = filter(lambda c: c.name.startswith(f"team-{team_tla.lower()}-"), guild.text_channels) + for channel in channels: + suffix = channel.name.removeprefix(f"team-{team_tla.lower()}-") + commands.append(f"/team channel tla:{team_tla} suffix:{suffix}") + + has_voice: bool = discord.utils.get(guild.voice_channels, name=f"team-{team_tla.lower()}") is not None + if has_voice: + commands.append(f"/team voice tla:{team_tla}") + + return "\n".join(commands) + "\n" + + output = "```\n" + + if tla is None: + for team_role in guild.roles: + if team_role.name.startswith(ROLE_PREFIX) and team_role.name != TEAM_LEADER_ROLE: + output = output + await _export_team(team_role.name.removeprefix(ROLE_PREFIX)) + else: + output = output + await _export_team(tla) + output = output + "\n```" + await interaction.followup.send(content=output, ephemeral=True) diff --git a/src/constants.py b/src/constants.py index 5415fcc..d129ec4 100644 --- a/src/constants.py +++ b/src/constants.py @@ -22,3 +22,4 @@ TEAM_CATEGORY_NAME = "Team Channels" TEAM_VOICE_CATEGORY_NAME = "Team Voice Channels" +TEAM_LEADER_ROLE = "Team Supervisor"