Passed
Pull Request — master (#50)
by Cyb3r
01:40
created

bot.cogs.admin   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 168
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 62
dl 0
loc 168
rs 10
c 0
b 0
f 0
wmc 11

8 Methods

Rating   Name   Duplication   Size   Complexity  
A AdminCog.reload_extension() 0 13 1
A AdminCog.add_admin_channel() 0 25 1
A AdminCog.add_admin() 0 22 2
A AdminCog.__init__() 0 2 1
A AdminCog.cog_check() 0 14 1
A AdminCog.check_admin() 0 16 1
A AdminCog.list_admins() 0 21 2
A AdminCog.refresh_list() 0 11 1

1 Function

Rating   Name   Duplication   Size   Complexity  
A setup() 0 3 1
1
"""Cog that has the admin features"""
2
import logging
3
from datetime import datetime
4
import discord.utils
5
from discord.ext import commands
6
from bot import utils
7
8
log = logging.getLogger("bot")
9
10
11
class AdminCog(commands.Cog, name="Admin"):
12
    """AdminCog
13
    ---
14
15
    Cog that contains the commands for admin functions.
16
17
    Commands:
18
    ---
19
        `list-admins`: Sends a message that contain a list of the bot admin users.
20
        `am-admin`: Returns true of false depending on if the user is in the bot_admins table.
21
        `add-admin`: Add a user to the bot admin table.
22
        `add-admin-channel`: Add the channel that it was called in to the admin_channel table.
23
        `reload-extension`: Reloads the extensions names.
24
        `update-list`: Updates the list of schools
25
26
    Arguments:
27
    ---
28
        bot {discord.commands.Bot} -- The bot
29
    """
30
31
    def __init__(self, bot):
32
        self.bot = bot
33
34
    async def cog_check(self, ctx: commands.Context):
35
        """cog_check
36
        ---
37
38
        cog_check is set for the whole cog. Which makes all the commands in health admin only.
39
40
        Arguments:
41
        ---
42
            ctx {discord.ext.commands.Context} -- Context of the command.
43
44
        Returns:
45
            bool -- True if the user in the bot admins
46
        """
47
        return await utils.check_admin(ctx)
48
49
    @commands.command(
50
        name="list-admins",
51
        aliases=["ladmins", "ladmin"],
52
        help="List users that are bot admins",
53
    )
54
    async def list_admins(self, ctx: commands.Context):
55
        """List-admins
56
        ---
57
        Command that returns a list of the users that are in the bot_admin table.
58
59
        Arguments:
60
        ---
61
            ctx {discord.ext.commands.Context} -- Context of the command.
62
        """
63
        fetched = [x for x in await utils.fetch("bot_admins", "name") if x != "CCC-Dev-Bot"]
64
        embed = await utils.make_embed(ctx, send=False, title="Bot Admins:")
65
        admins = ""
66
        for admin in fetched:
67
            admins += "- {} \n".format(admin)
68
        embed.add_field(name="Admins", value=admins, inline=False)
69
        await ctx.send(embed=embed)
70
71
    @commands.command(
72
        name="check-admin",
73
        aliases=["cadmin", "am-admin"],
74
        help="Tells you if you are a bot admin",
75
    )
76
    async def check_admin(self, ctx: commands.Context):
77
        """Check Admin
78
        ---
79
80
        Tells the user if they are in the bot admin table. Only return true or false.
81
82
        Arguments:
83
        ---
84
            ctx {discord.ext.commands.Context} -- Context of the command.
85
        """
86
        await utils.make_embed(ctx, title=await utils.check_admin(ctx))
87
88
    @commands.command(name="add-admin", help="Adds <user> to the bot admins table.")
89
    @commands.check(utils.check_admin)
90
    async def add_admin(self, ctx: commands.Context, *, user: discord.Member):
91
        """Add-Admin
92
        ---
93
94
        Adds `user` to the bot admin table.
95
96
        Arguments:
97
        ---
98
            ctx {discord.ext.commands.Context} -- Context of the command.
99
            user {str} -- The name of the user that you want to add to the bot_admin table
100
        """
101
        new_admin = discord.utils.get(ctx.guild.members, name=user)
102
        if new_admin:
103
            log.info("{} added new admin {}".format(ctx.author.display_name, user.display_name))
104
            await utils.insert("bot_admins", [new_admin.name, new_admin.id])
105
            await utils.make_embed(
106
                ctx, color="28b463", title="User: {} is now an admin.".format(new_admin)
107
            )
108
        else:
109
            await utils.error_message(ctx, "User not found.")
110
111
    @commands.command(name="add-admin-channel", help="Marks the channel as an admin channel")
112
    @commands.guild_only()
113
    @commands.check(utils.check_admin)
114
    async def add_admin_channel(self, ctx: commands.Context, log_status=False):
115
        """add-admin-channel
116
        ---
117
118
        Adds the channel where the command was run to the admin_channel table.
119
        By default it sets logging as false.
120
121
        Arguments:
122
        ---
123
            ctx {discord.ext.commands.Context} -- Context of the command.
124
125
        Keyword Arguments:
126
        ---
127
            log_status {bool} -- [If the channel is for logging all users] (default: {False})
128
        """
129
        log_status = bool(log_status)
130
        await utils.insert("admin_channels", [ctx.channel.name, ctx.channel.id, log_status])
131
        await utils.make_embed(
132
            ctx,
133
            color="28b463",
134
            title="Admin Channel Success",
135
            description="Channel has been added with log status: {}".format(log_status),
136
        )
137
138
    @commands.command(name="reload-extension", help="reloads <extension>")
139
    async def reload_extension(self, ctx: commands.Context, extension: str):
140
        """reload_extension
141
        ---
142
143
        Command that reloads an extension.
144
145
        Arguments:
146
            ctx {discord.ext.commands.Context} -- Context of the command.
147
            extension {str} -- extension to reload
148
        """
149
        self.bot.reload_extension(extension)
150
        await utils.make_embed(ctx, color="28b463", title="Reloaded", description=extension)
151
152
    @commands.command(name="update-list", help="Updates the school_list.csv")
153
    async def refresh_list(self, ctx: commands.Context):
154
        """refresh_list
155
        ---
156
157
        Arguments:
158
            ctx {discord.ext.commands.Context} -- Context of the command.
159
        """
160
        await utils.update_list(self.bot, True)
161
        self.bot.list_updated = datetime.utcnow()
162
        await ctx.send("List updated")
163
164
165
def setup(bot):
166
    """Needed for extension loading"""
167
    bot.add_cog(AdminCog(bot))
168