utils   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 77
dl 0
loc 112
rs 10
c 0
b 0
f 0
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A UtilsCog.__init__() 0 4 1
A UtilsCog.panel_info() 0 43 2
A UtilsCog.purge_command() 0 10 1
A UtilsCog.on_command_error() 0 7 2
A UtilsCog.help_command() 0 22 2

1 Function

Rating   Name   Duplication   Size   Complexity  
A setup() 0 2 1
1
from app.bot import Bot
2
from typing import NoReturn, Optional
3
4
import discord
5
import psutil
6
7
from discord.ext import commands
8
from discord.ext.commands import Context, CommandError
9
10
SALARIED_ROLE_ID: int = 888527962935296091
11
PDG_ROLE_ID: int = 888527789794422784
12
13
14
class UtilsCog(commands.Cog):
15
    """A simple commands cog template."""
16
17
    def __init__(self, client: Bot):
18
        """Link to bot instance."""
19
        self.name = 'Utilitaire'
20
        self.client: Bot = client
21
22
    @commands.command(
23
        name='pan',
24
        description='host stats'
25
    )
26
    @commands.has_any_role(SALARIED_ROLE_ID, PDG_ROLE_ID)
27
    async def panel_info(self, ctx: Context):
28
        await ctx.message.delete()
29
        mb: int = 1024 ** 2
30
31
        vm = psutil.virtual_memory()
32
        cpu_freq = psutil.cpu_freq()
33
        cpu_percent = psutil.cpu_percent()
34
        disk = psutil.disk_usage('.')
35
36
        stats = {
37
            'ram': (
38
                100 * (vm.used / vm.total),
39
                f'{(vm.total / mb) / 1000:,.3f}',
40
                'Gb'
41
            ),
42
            'cpu': (
43
                cpu_percent,
44
                f"{cpu_freq.current / 1000:.1f}`/`{cpu_freq.max / 1000:.1f}",
45
                'Ghz'
46
            ),
47
            'disk': (
48
                100 * (disk.used / disk.total),
49
                f'{disk.total / mb:,.0f}', 'Mb'
50
            )
51
        }
52
53
        panel_embed = discord.Embed(
54
            title="Server Report",
55
            description="The bot is hosted on a private vps."
56
        )
57
58
        for name, (percent, info, unit) in stats.items():
59
            panel_embed.add_field(
60
                name=name.upper(),
61
                value=f"> `{percent:.3f}` **%**\n- `{info}` **{unit}**"
62
            )
63
64
        await ctx.send(embed=panel_embed)
65
66
    @commands.command(
67
        name="help",
68
        description="La commande d'aide",
69
    )
70
    @commands.has_any_role(SALARIED_ROLE_ID, PDG_ROLE_ID)
71
    async def help_command(self, ctx: Context):
72
        await ctx.message.delete()
73
74
        help_embed = discord.Embed(
75
            description="Aide du TéquilaBot"
76
        )
77
78
        for cog in self.client.cogs.values():
79
            help_embed.add_field(
80
                name=f'> {cog.name}',
81
                value='\n'.join(
82
                    f'- `{command.name}`: {command.description}'
83
                    for command in cog.get_commands()
84
                ), inline=False
85
            )
86
87
        await ctx.send(embed=help_embed, delete_after=60)
88
89
    @commands.command(
90
        name="purge",
91
        description="Supprime X messages"
92
    )
93
    @commands.has_any_role(SALARIED_ROLE_ID, PDG_ROLE_ID)
94
    async def purge_command(
95
            self, ctx: Context, limit: Optional[int] = None
96
    ) -> None:
97
        await ctx.channel.purge(limit=limit)
98
        await ctx.send("> Purgé!", delete_after=5)
99
100
    @commands.Cog.listener()
101
    async def on_command_error(self, ctx: Context, exc: CommandError):
102
        print(exc)
103
104
        if isinstance(exc, commands.errors.MissingAnyRole):
105
            await ctx.send(
106
                "> Seul les salariés ont le droit d' exécuter cette commande."
107
            )
108
109
110
def setup(client: Bot) -> NoReturn:
111
    client.add_cog(UtilsCog(client))
112