Skip to content

Commit

Permalink
Move team password command out of /team
Browse files Browse the repository at this point in the history
This allows its permissions to be set independently of `/team`.
  • Loading branch information
raccube committed Aug 28, 2024
1 parent c19b3e7 commit b37d2df
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 30 deletions.
4 changes: 2 additions & 2 deletions src/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@
from src.commands.logs import logs
from src.commands.team import (
Team,
passwd,
new_team,
delete_team,
export_team,
create_voice,
create_team_channel,
)
from src.commands.passwd import passwd


class BotClient(discord.Client):
Expand Down Expand Up @@ -63,8 +63,8 @@ def __init__(
team.add_command(create_voice)
team.add_command(create_team_channel)
team.add_command(export_team)
team.add_command(passwd)
self.tree.add_command(team, guild=self.guild)
self.tree.add_command(passwd, guild=self.guild)
self.tree.add_command(join, guild=self.guild)
self.tree.add_command(logs, guild=self.guild)
self.load_passwords()
Expand Down
38 changes: 38 additions & 0 deletions src/commands/passwd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from typing import TYPE_CHECKING

import discord
from discord import app_commands

if TYPE_CHECKING:
from src.bot import BotClient

@app_commands.command( # type:ignore[arg-type]
name='passwd',
description='Outputs or changes team passwords',
)
@app_commands.describe(
tla='Three Letter Acronym (e.g. SRZ)',
new_password='New password',
)
async def passwd(
interaction: discord.interactions.Interaction["BotClient"],
tla: str | None = None,
new_password: str | None = None,
) -> None:
if tla is None:
await interaction.response.send_message(
'\n'.join([f"**{team}:** {password}" for team, password in interaction.client.passwords.items()]),
ephemeral=True,
)
if new_password is not None:
if not interaction.user.guild_permissions.administrator:
await interaction.response.send_message(
"You do not have permission to change team passwords.",
ephemeral=True
)
return
interaction.client.set_password(tla, new_password)
await interaction.response.send_message(f"The password for {tla.upper()} has been changed.", ephemeral=True)
else:
password = interaction.client.passwords[tla]
await interaction.response.send_message(f"The password for {tla.upper()} is `{password}`", ephemeral=True)
30 changes: 2 additions & 28 deletions src/commands/team.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
TEAM_LEADER_ROLE,
TEAM_CATEGORY_NAME,
TEAM_CHANNEL_PREFIX,
PASSWORDS_CHANNEL_NAME,
TEAM_VOICE_CATEGORY_NAME,
)

Expand All @@ -23,7 +22,8 @@
@app_commands.guild_only()
@app_commands.default_permissions()
class Team(app_commands.Group):
pass
def __init__(self):
super().__init__(description="Manage teams")


group = Team()
Expand Down Expand Up @@ -251,29 +251,3 @@ async def export_team(
output = output + await _export_team(tla, only_teams, guild, interaction)
output = output + "\n```"
await interaction.followup.send(content=output, ephemeral=True)


@group.command( # type:ignore[arg-type]
name='passwd',
description='Outputs or changes team passwords',
)
@app_commands.describe(
tla='Three Letter Acronym (e.g. SRZ)',
new_password='New password',
)
async def passwd(
interaction: discord.interactions.Interaction["BotClient"],
tla: str | None = None,
new_password: str | None = None,
) -> None:
if tla is None:
await interaction.response.send_message(
'\n'.join([f"**{team}:** {password}" for team, password in interaction.client.passwords.items()]),
ephemeral=True,
)
elif new_password:
interaction.client.set_password(tla, new_password)
await interaction.response.send_message(f"The password for {tla.upper()} has been changed.", ephemeral=True)
else:
password = interaction.client.passwords[tla]
await interaction.response.send_message(f"The password for {tla.upper()} is `{password}`", ephemeral=True)

0 comments on commit b37d2df

Please sign in to comment.