Skip to content

Commit a623078

Browse files
committed
feat: autocomplete for club existing in the discord guild
1 parent aa6a118 commit a623078

2 files changed

Lines changed: 133 additions & 118 deletions

File tree

src/commands/club.py

Lines changed: 128 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -1,117 +1,128 @@
1-
from __future__ import annotations
2-
3-
from typing import TYPE_CHECKING
4-
5-
from discord import Interaction, Member, app_commands
6-
from discord.app_commands import Choice, Transform, Transformer
7-
from discord.ext import commands
8-
from discord.ext.commands import BadArgument
9-
10-
from src.client import ClubSchema # noqa TC001
11-
from src.services.club import ClubService, DiscordClub
12-
from src.settings import Settings
13-
14-
if TYPE_CHECKING:
15-
from src.client import SithClient
16-
from src.main import AeBot
17-
18-
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-
29-
class ClubCog(commands.GroupCog, group_name="club"):
30-
def __init__(self, client: SithClient, bot: AeBot):
31-
self.club_service = ClubService(client, bot)
32-
self.settings = Settings()
33-
34-
async def autocomplete_club(
35-
self, _interaction: Interaction, current: str
36-
) -> list[Choice]:
37-
"""Autocompletion for clubs."""
38-
clubs = await self.club_service.search_club(current)
39-
clubs = clubs[:25] # discord autocomplete can have at most 25 items
40-
return [Choice(name=club.name, value=str(club.id)) for club in clubs]
41-
42-
@app_commands.command(name="infos")
43-
@app_commands.autocomplete(club=autocomplete_club)
44-
async def club_infos(
45-
self, interaction: Interaction, club: Transform[ClubSchema, ClubTransformer]
46-
):
47-
await interaction.response.defer(thinking=True)
48-
await interaction.followup.send(embed=self.club_service.embed(club))
49-
50-
@app_commands.command(name="remove_member")
51-
@app_commands.autocomplete(club=autocomplete_club)
52-
async def remove_club_member(
53-
self,
54-
interaction: Interaction,
55-
club: Transform[ClubSchema, ClubTransformer],
56-
member: Member,
57-
):
58-
await interaction.response.defer(thinking=True)
59-
discord_club = DiscordClub.load(club.id)
60-
if not discord_club:
61-
await interaction.followup.send(f"Le club : {club.name} n'existe pas")
62-
return
63-
if (
64-
not interaction.user.guild_permissions.manage_roles
65-
and not interaction.user.get_role(discord_club.president_role_id)
66-
):
67-
await interaction.followup.send(
68-
"Seul le président du club et les admins peuvent retirer un membre"
69-
)
70-
return
71-
if member.id not in discord_club.members:
72-
await interaction.followup.send("Cet utilisateur n'est pas dans le club")
73-
return
74-
await self.club_service.remove_member(discord_club, member)
75-
await interaction.followup.send(
76-
f"{member.name} a été retiré du club :thumbs_up:"
77-
)
78-
79-
@app_commands.command(name="add_member")
80-
@app_commands.autocomplete(club=autocomplete_club)
81-
async def add_club_member(
82-
self,
83-
interaction: Interaction,
84-
club: Transform[ClubSchema, ClubTransformer],
85-
member: Member,
86-
):
87-
await interaction.response.defer(thinking=True)
88-
discord_club = DiscordClub.load(club.id)
89-
if not discord_club:
90-
await interaction.followup.send(f"Le club : {club.name} n'existe pas")
91-
return
92-
if (
93-
not interaction.user.guild_permissions.manage_roles
94-
and not interaction.user.get_role(discord_club.president_role_id)
95-
):
96-
await interaction.followup.send(
97-
"Seul le président du club et les admins peuvent ajouter un membre"
98-
)
99-
return
100-
await self.club_service.add_member(discord_club, member)
101-
await interaction.followup.send(
102-
f"{member.name} a été ajouté au club :thumbs_up:"
103-
)
104-
105-
@app_commands.command(name="create")
106-
@app_commands.autocomplete(club=autocomplete_club)
107-
@app_commands.checks.has_permissions(manage_guild=True)
108-
async def create_club(
109-
self, interaction: Interaction, club: Transform[ClubSchema, ClubTransformer]
110-
):
111-
await interaction.response.defer(thinking=True)
112-
discord_club = DiscordClub.load(club.id)
113-
if discord_club is not None:
114-
await interaction.followup.send(f"Le club : {club.name} existe déjà...")
115-
else:
116-
await self.club_service.create_club(club, interaction.guild)
117-
await interaction.followup.send(f"Le club : {club.name} à été créé")
1+
from __future__ import annotations
2+
3+
from typing import TYPE_CHECKING
4+
5+
from discord import Interaction, Member, app_commands
6+
from discord.app_commands import Choice, Transform, Transformer
7+
from discord.ext import commands
8+
from discord.ext.commands import BadArgument
9+
10+
from src.client import ClubSchema # noqa TC001
11+
from src.services.club import ClubService, DiscordClub
12+
from src.settings import Settings
13+
14+
if TYPE_CHECKING:
15+
from src.client import SithClient
16+
from src.main import AeBot
17+
18+
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+
29+
class ClubCog(commands.GroupCog, group_name="club"):
30+
def __init__(self, client: SithClient, bot: AeBot):
31+
self.club_service = ClubService(client, bot)
32+
self.settings = Settings()
33+
34+
async def autocomplete_club(
35+
self, _interaction: Interaction, current: str
36+
) -> list[Choice]:
37+
"""Autocompletion for clubs."""
38+
clubs = await self.club_service.search_club(current, only_existing=False)
39+
clubs = clubs[:25] # discord autocomplete can have at most 25 items
40+
return [Choice(name=club.name, value=str(club.id)) for club in clubs]
41+
42+
async def autocomplete_existing_club(
43+
self, _interaction: Interaction, current: str
44+
) -> list[Choice]:
45+
"""Autocompletion for clubs that have a channel in the guild"""
46+
clubs = await self.club_service.search_club(current, only_existing=True)
47+
clubs = clubs[:25] # discord autocomplete can have at most 25 items
48+
return [Choice(name=club.name, value=str(club.id)) for club in clubs]
49+
50+
@app_commands.command(name="infos")
51+
@app_commands.autocomplete(club=autocomplete_club)
52+
async def club_infos(
53+
self, interaction: Interaction, club: Transform[ClubSchema, ClubTransformer]
54+
):
55+
await interaction.response.defer(thinking=True)
56+
await interaction.followup.send(embed=self.club_service.embed(club))
57+
58+
@app_commands.command(name="remove_member")
59+
@app_commands.autocomplete(club=autocomplete_existing_club)
60+
async def remove_club_member(
61+
self,
62+
interaction: Interaction,
63+
club: Transform[ClubSchema, ClubTransformer],
64+
member: Member,
65+
):
66+
await interaction.response.defer(thinking=True)
67+
discord_club = DiscordClub.load(club.id)
68+
if not discord_club:
69+
await interaction.followup.send(f"Le club : {club.name} n'existe pas")
70+
return
71+
if (
72+
not interaction.user.guild_permissions.manage_roles
73+
and not interaction.user.get_role(discord_club.president_role_id)
74+
):
75+
await interaction.followup.send(
76+
"Seul le président du club et les admins peuvent retirer un membre"
77+
)
78+
return
79+
if member.id not in discord_club.members:
80+
await interaction.followup.send("Cet utilisateur n'est pas dans le club")
81+
return
82+
await self.club_service.remove_member(discord_club, member)
83+
await interaction.followup.send(
84+
f"{member.name} a été retiré du club :thumbs_up:"
85+
)
86+
87+
@app_commands.command(name="add_member")
88+
@app_commands.autocomplete(club=autocomplete_existing_club)
89+
async def add_club_member(
90+
self,
91+
interaction: Interaction,
92+
club: Transform[ClubSchema, ClubTransformer],
93+
member: Member,
94+
):
95+
await interaction.response.defer(thinking=True)
96+
discord_club = DiscordClub.load(club.id)
97+
if not discord_club:
98+
await interaction.followup.send(f"Le club : {club.name} n'existe pas")
99+
return
100+
if (
101+
not interaction.user.guild_permissions.manage_roles
102+
and not interaction.user.get_role(discord_club.president_role_id)
103+
):
104+
await interaction.followup.send(
105+
"Seul le président du club et les admins peuvent ajouter un membre"
106+
)
107+
return
108+
if member.id in discord_club.members:
109+
await interaction.followup.send("Cet utilisateur est déjà dans le club")
110+
return
111+
await self.club_service.add_member(discord_club, member)
112+
await interaction.followup.send(
113+
f"{member.name} a été ajouté au club :thumbs_up:"
114+
)
115+
116+
@app_commands.command(name="create")
117+
@app_commands.autocomplete(club=autocomplete_club)
118+
@app_commands.checks.has_permissions(manage_guild=True)
119+
async def create_club(
120+
self, interaction: Interaction, club: Transform[ClubSchema, ClubTransformer]
121+
):
122+
await interaction.response.defer(thinking=True)
123+
discord_club = DiscordClub.load(club.id)
124+
if discord_club is not None:
125+
await interaction.followup.send(f"Le club : {club.name} existe déjà...")
126+
else:
127+
await self.club_service.create_club(club, interaction.guild)
128+
await interaction.followup.send(f"Le club : {club.name} à été créé")

src/services/club.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,12 @@ def __init__(self, client: SithClient, bot: AeBot):
5858
self._club_cache = {}
5959
self._bot = bot
6060

61-
async def search_club(self, current: str) -> list[SimpleClubSchema]:
61+
async def search_club(
62+
self, current: str, *, only_existing: bool
63+
) -> list[SimpleClubSchema]:
6264
clubs = await self._client.search_clubs(current)
65+
if clubs and only_existing:
66+
clubs = [c for c in clubs if str(c.id) in DiscordClub.load_all()]
6367
return clubs if clubs is not None else []
6468

6569
async def get_club(self, club_id: int) -> ClubSchema | None:

0 commit comments

Comments
 (0)