Skip to content

Commit 1796101

Browse files
authored
Merge pull request #1 from ae-utbm/attribute-president-role
Attribute president role
2 parents 9f94f8b + 5674ff1 commit 1796101

5 files changed

Lines changed: 68 additions & 19 deletions

File tree

bot.toml.example

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,22 @@ log_level = "INFO"
44

55
[guild]
66
id = <the id of the server here>
7-
club_ids = [
8-
1, # AE
9-
29, # Club Welcome
10-
38, # Troll Penché
11-
43, # Reflex
12-
79, # Cook'UT
13-
]
7+
8+
[guild.club.1] # AE
9+
president_role_id = <role id>
10+
member_role_id = <role id>
11+
[guild.club.29] # Club Welcome
12+
president_role_id = <role id>
13+
member_role_id = <role id>
14+
[guild.club.38] # Troll Penché
15+
president_role_id = <role id>
16+
member_role_id = <role id>
17+
[guild.club.43] # Reflex
18+
president_role_id = <role id>
19+
member_role_id = <role id>
20+
[guild.club.79] # Cook'UT
21+
president_role_id = <role id>
22+
member_role_id = <role id>
1423

1524
[sith_api]
1625
api_key = "<your sith api token here>"

src/commands/club.py

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,32 @@
22

33
from typing import TYPE_CHECKING
44

5-
from discord import Interaction, app_commands
6-
from discord.app_commands import Choice
5+
from discord import Interaction, Member, app_commands
6+
from discord.app_commands import Choice, Transform, Transformer
77
from discord.ext import commands
8+
from discord.ext.commands import BadArgument
89

10+
from src.client import ClubSchema # noqa TC001
911
from src.services.club import ClubService
1012
from src.settings import Settings
1113

1214
if TYPE_CHECKING:
13-
from src.client import ClubSchema, SithClient
15+
from src.client import SithClient
1416
from src.main import AeBot
1517

1618

19+
class ClubTransformer(Transformer):
20+
async def transform(
21+
self, interaction: Interaction[AeBot], value: int
22+
) -> ClubSchema:
23+
club = await interaction.client.client.get_club(value)
24+
if not club:
25+
raise BadArgument("Ce club n'existe pas")
26+
return club
27+
28+
1729
class ClubCog(commands.GroupCog, group_name="club"):
18-
def __init__(self, client: SithClient, bot: "AeBot"):
30+
def __init__(self, client: SithClient, bot: AeBot):
1931
self.club_service = ClubService(client, bot)
2032
self.settings = Settings()
2133

@@ -30,10 +42,31 @@ async def autocomplete_club(
3042

3143
@app_commands.command(name="infos")
3244
@app_commands.autocomplete(club=autocomplete_club)
33-
async def club_infos(self, interaction: Interaction, club: int):
45+
async def club_infos(
46+
self, interaction: Interaction, club: Transform[ClubSchema, ClubTransformer]
47+
):
3448
await interaction.response.defer(thinking=True)
35-
club: ClubSchema = await self.club_service.get_club(club)
36-
if club is None:
37-
await interaction.followup.send("Ce club n'a pas été trouvé.")
38-
return
3949
await interaction.followup.send(embed=self.club_service.embed(club))
50+
51+
@app_commands.command(name="add_member")
52+
@app_commands.autocomplete(club=autocomplete_club)
53+
async def add_club_member(
54+
self,
55+
interaction: Interaction,
56+
club: int,
57+
member: Member,
58+
):
59+
await interaction.response.defer(thinking=True)
60+
club = next(
61+
(c for _id, c in self.settings.guild.clubs.items() if _id == club), None
62+
)
63+
if not club:
64+
await interaction.followup.send("Ce club n'a pas été trouvé")
65+
return
66+
if not interaction.user.get_role(club.president_role_id):
67+
await interaction.followup.send(
68+
"Seul le président du club peut utiliser cette commande"
69+
)
70+
return
71+
await member.add_roles(interaction.guild.get_role(club.member_role_id))
72+
await interaction.followup.send("Rôle attribué :thumbs_up:")

src/main.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ def __init__(self, client: SithClient):
1919
self.settings = Settings()
2020
self.logger = logging.getLogger("discord")
2121
self.client = client
22-
super().__init__(command_prefix="?", intents=Intents.all())
22+
super().__init__(
23+
command_prefix=self.settings.bot.command_prefix, intents=Intents.all()
24+
)
2325

2426
async def setup_hook(self):
2527
await self.add_cog(ClubCog(self.client, self))

src/services/club.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ async def search_club(self, current: str) -> list[SimpleClubSchema]:
2525
clubs = await self._client.search_clubs(current)
2626
if clubs is None:
2727
return []
28-
return [club for club in clubs if club.id in self._config.guild.club_ids]
28+
return [club for club in clubs if club.id in self._config.guild.clubs]
2929

3030
async def get_club(self, club_id: int) -> ClubSchema | None:
3131
if club_id not in self._club_cache:

src/settings.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,14 @@ class ApiConfig(BaseModel):
1717
api_key: SecretStr
1818

1919

20+
class ClubConfig(BaseModel):
21+
president_role_id: int
22+
member_role_id: int
23+
24+
2025
class GuildConfig(BaseModel):
2126
id: int
22-
club_ids: list[int]
27+
clubs: dict[int, ClubConfig]
2328

2429

2530
class BotConfig(BaseModel):

0 commit comments

Comments
 (0)