Passed
Push — master ( dcd1eb...4f7f97 )
by Cyb3r
02:51 queued 01:44
created

bot.cogs.admin.AdminCog.check_admin()   A

Complexity

Conditions 1

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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