1 | from typing import NoReturn, Optional |
||
2 | |||
3 | from discord import TextChannel |
||
4 | from discord.ext import commands |
||
5 | from discord.ext.commands import Context, CommandError |
||
6 | |||
7 | from app.bot import Bot |
||
8 | from app.classes.members_list import MemberList |
||
9 | from app.exceptions import MemberAlreadyExists, MemberNotFound |
||
10 | from app.utils import SALARIED_ROLE_ID, PDG_ROLE_ID |
||
11 | |||
12 | CHANNEL_ID: int = 888563799701991435 |
||
13 | MESSAGE_ID: int = 888770216480366632 |
||
14 | |||
15 | |||
16 | class MembersCog(commands.Cog): |
||
17 | """A simple commands cog template.""" |
||
18 | |||
19 | def __init__(self, client: Bot): |
||
20 | """Link to bot instance.""" |
||
21 | self.name = 'Gestion Members' |
||
22 | self.client: Bot = client |
||
23 | self.channel: Optional[TextChannel] = None |
||
24 | self.manager = None |
||
25 | |||
26 | @commands.Cog.listener() |
||
27 | async def on_ready(self): |
||
28 | self.channel = self.client.guild.get_channel(CHANNEL_ID) |
||
29 | |||
30 | self.manager = MemberList( |
||
31 | await self.channel.fetch_message(MESSAGE_ID), |
||
32 | ) |
||
33 | |||
34 | @commands.command( |
||
35 | name='addpeople', |
||
36 | description="Ajoute une personne dans la liste des adhérents" |
||
37 | ) |
||
38 | @commands.has_any_role(SALARIED_ROLE_ID, PDG_ROLE_ID) |
||
39 | async def add_people_command(self, ctx: Context, person_name: str): |
||
40 | await ctx.message.delete() |
||
41 | await self.manager.add(person_name, 'people') |
||
42 | await ctx.send('Ajouté!', delete_after=2) |
||
43 | |||
44 | @commands.command( |
||
45 | name='addcomp', |
||
46 | description="Ajoute une entreprise dans la liste des adhérents" |
||
47 | ) |
||
48 | @commands.has_any_role(SALARIED_ROLE_ID, PDG_ROLE_ID) |
||
49 | async def add_company_command(self, ctx: Context, company_name: str): |
||
50 | await ctx.message.delete() |
||
51 | await self.manager.add(company_name, 'company') |
||
52 | await ctx.send('Ajouté!', delete_after=2) |
||
53 | |||
54 | @commands.command( |
||
55 | name='removepeople', |
||
56 | description="Retire la personne donnée de la liste des adhérents" |
||
57 | ) |
||
58 | @commands.has_any_role(SALARIED_ROLE_ID, PDG_ROLE_ID) |
||
59 | async def remove_people_command(self, ctx: Context, company_name: str): |
||
60 | await ctx.message.delete() |
||
61 | await self.manager.remove(company_name, 'people') |
||
62 | await ctx.send('Retiré!', delete_after=2) |
||
63 | |||
64 | @commands.command( |
||
65 | name='removecomp', |
||
66 | description="Retire l'entreprise donnée de la liste des adhérents" |
||
67 | ) |
||
68 | @commands.has_any_role(SALARIED_ROLE_ID, PDG_ROLE_ID) |
||
69 | async def remove_company_command(self, ctx: Context, company_name: str): |
||
70 | await ctx.message.delete() |
||
71 | await self.manager.remove(company_name, 'company') |
||
72 | await ctx.send('Retiré!', delete_after=2) |
||
73 | |||
74 | View Code Duplication | @commands.command( |
|
0 ignored issues
–
show
Duplication
introduced
by
![]() |
|||
75 | name="desc-2", |
||
76 | description="Met à jour la description de la liste des adherents." |
||
77 | ) |
||
78 | @commands.has_any_role(SALARIED_ROLE_ID, PDG_ROLE_ID) |
||
79 | async def set_drink_list_description(self, ctx: Context, *, message: str): |
||
80 | await ctx.message.delete() |
||
81 | |||
82 | with open( |
||
83 | "assets/members_description.txt", |
||
84 | 'w', encoding='utf-8' |
||
85 | ) as f: |
||
86 | f.write(message) |
||
87 | |||
88 | await ctx.send( |
||
89 | f"Description mise à jour\n>>> {message}", delete_after=5 |
||
90 | ) |
||
91 | |||
92 | await self.manager.update() |
||
93 | |||
94 | @commands.Cog.listener() |
||
95 | async def on_command_error(self, ctx: Context, exc: CommandError): |
||
96 | if not isinstance(exc, commands.errors.CommandInvokeError): |
||
97 | return |
||
98 | |||
99 | if isinstance(exc.original, MemberAlreadyExists): |
||
100 | await ctx.send( |
||
101 | f"Le membre `{exc.original.member_name}`" |
||
102 | f"est déjà enregistrée dans `{exc.original.member_type}`" |
||
103 | ) |
||
104 | |||
105 | if isinstance(exc.original, MemberNotFound): |
||
106 | await ctx.send( |
||
107 | f"Le membre `{exc.original.member_name}`" |
||
108 | f"n'est pas dans `{exc.original.member_type}`" |
||
109 | ) |
||
110 | |||
111 | |||
112 | def setup(client) -> NoReturn: |
||
113 | client.add_cog(MembersCog(client)) |
||
114 |