drinks   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 87
dl 0
loc 127
rs 10
c 0
b 0
f 0
wmc 13

8 Methods

Rating   Name   Duplication   Size   Complexity  
A DrinksCog.remove_command() 0 12 1
A DrinksCog.on_command_error() 0 13 4
A DrinksCog.delete_command() 0 12 1
A DrinksCog.on_ready() 0 6 1
A DrinksCog.set_drink_list_description() 0 19 2
A DrinksCog.__init__() 0 6 1
A DrinksCog.create_command() 0 12 1
A DrinksCog.add_command() 0 12 1

1 Function

Rating   Name   Duplication   Size   Complexity  
A setup() 0 2 1
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.classes.drink_list import DrinkList
8
from app.exceptions import DrinkAlreadyExists, DrinkNotFound
9
from app.utils import SALARIED_ROLE_ID, PDG_ROLE_ID
10
11
CHANNEL_ID = 888531559861321738
12
MESSAGE_ID = 888535656542928906
13
14
15
class DrinksCog(commands.Cog):
16
    """A simple commands cog template."""
17
18
    def __init__(self, client):
19
        """Link to bot instance."""
20
        self.name = 'Gestion Boisson'
21
        self.client = client
22
        self.channel: Optional[TextChannel] = None
23
        self.drink_list: Optional[DrinkList] = None
24
25
    @commands.Cog.listener()
26
    async def on_ready(self):
27
        self.channel = self.client.guild.get_channel(CHANNEL_ID)
28
29
        self.drink_list = DrinkList(
30
            await self.channel.fetch_message(MESSAGE_ID)
31
        )
32
33
    @commands.command(
34
        name="create",
35
        description="Créer une nouvelle boisson",
36
    )
37
    @commands.has_any_role(SALARIED_ROLE_ID, PDG_ROLE_ID)
38
    async def create_command(self, ctx: Context, drink) -> None:
39
        await ctx.message.delete()
40
        await self.drink_list.create(drink)
41
        await ctx.send(
42
            f"Boisson ajouté!\n"
43
            f">>> {self.drink_list.flatten()}",
44
            delete_after=3
45
        )
46
47
    @commands.command(
48
        name="add",
49
        description="Incrémente le compteur d' une boisson"
50
    )
51
    @commands.has_any_role(SALARIED_ROLE_ID, PDG_ROLE_ID)
52
    async def add_command(self, ctx: Context, drink, n=1) -> None:
53
        await ctx.message.delete()
54
        await self.drink_list.add(drink, n)
55
        await ctx.send(
56
            f"`{drink}`x`{n}` Ajouté!\n"
57
            f">>> {self.drink_list.flatten()}",
58
            delete_after=3
59
        )
60
61
    @commands.command(
62
        name="delete",
63
        description="Retire un boisson de la liste"
64
    )
65
    @commands.has_any_role(SALARIED_ROLE_ID, PDG_ROLE_ID)
66
    async def delete_command(self, ctx: Context, drink) -> None:
67
        await ctx.message.delete()
68
        await self.drink_list.delete(drink)
69
        await ctx.send(
70
            f"`{drink}` Supprimé!\n"
71
            f">>> {self.drink_list.flatten()}",
72
            delete_after=3
73
        )
74
75
    @commands.command(
76
        name="remove",
77
        description="Décrémente le compteur d' une boisson"
78
    )
79
    @commands.has_any_role(SALARIED_ROLE_ID, PDG_ROLE_ID)
80
    async def remove_command(self, ctx: Context, drink, n=1):
81
        await ctx.message.delete()
82
        await self.drink_list.remove(drink, n)
83
        await ctx.send(
84
            f"`{drink}` `x`{n} Retiré!\n"
85
            f">>> {self.drink_list.flatten()}",
86
            delete_after=3
87
        )
88
89
    @commands.command(
90
        name="desc-1",
91
        description="Met à jour la description de la liste de boissons."
92
    )
93
    @commands.has_any_role(SALARIED_ROLE_ID, PDG_ROLE_ID)
94
    async def set_drink_list_description(self, ctx, *, message):
95
        await ctx.message.delete()
96
97
        with open(
98
            "assets/drinklist_description.txt",
99
            'w', encoding='utf-8'
100
        ) as f:
101
            f.write(message)
102
103
        await ctx.send(
104
            f"Description mise à jour\n>>> {message}", delete_after=5
105
        )
106
107
        await self.drink_list.update()
108
109
    @commands.Cog.listener()
110
    async def on_command_error(self, ctx: Context, exc: CommandError):
111
        if not isinstance(exc, commands.errors.CommandInvokeError):
112
            return
113
114
        if isinstance(exc.original, DrinkNotFound):
115
            await ctx.send(
116
                f"La boisson {exc.original.drink_name} n' existe pas !"
117
            )
118
119
        if isinstance(exc.original, DrinkAlreadyExists):
120
            await ctx.send(
121
                f"La boisson {exc.original.drink_name} est déjà enregistrée !"
122
            )
123
124
125
def setup(client) -> NoReturn:
126
    client.add_cog(DrinksCog(client))
127