admin.AdminCog.check_admin()   A
last analyzed

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