Passed
Push — master ( 95e0ba...ca4927 )
by Cyb3r
01:11
created

admin.AdminCog.list_admins()   A

Complexity

Conditions 2

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

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