22
33from 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
77from discord .ext import commands
8+ from discord .ext .commands import BadArgument
89
10+ from src .client import ClubSchema # noqa TC001
911from src .services .club import ClubService
1012from src .settings import Settings
1113
1214if 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+
1729class 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:" )
0 commit comments