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

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

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nop 2
dl 0
loc 2
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:
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:
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
        :return: None
77
        """
78
        await utils.make_embed(ctx, title=await utils.check_admin(ctx))
79
80
    @commands.command(name="add-admin", help="Adds <member> to the bot admins table.")
81
    @commands.check(utils.check_admin)
82
    async def add_admin(self, ctx: commands.Context, *, user: discord.Member) -> None:
83
        """Add Admin
84
85
        Adds **member** to the bot admin table.
86
87
        :param ctx: Command context
88
        :param user: Name of the member to add to the bot_admin table
89
        :return: None
90
        """
91
        new_admin = discord.utils.get(ctx.guild.members, name=user)
92
        if new_admin:
93
            log.info("{} added new admin {}".format(ctx.author.display_name, user.display_name))
94
            await utils.insert("bot_admins", [new_admin.name, new_admin.id])
95
            await utils.make_embed(
96
                ctx, color="28b463", title="User: {} is now an admin.".format(new_admin)
97
            )
98
        else:
99
            await utils.error_message(ctx, "User not found.")
100
101
    @commands.command(name="add-admin-channel", help="Marks the channel as an admin channel")
102
    @commands.guild_only()
103
    @commands.check(utils.check_admin)
104
    async def add_admin_channel(self, ctx: commands.Context, log_status: bool = False) -> None:
105
        """Add Admin Channel
106
107
        Adds the channel where the command was run to the admin_channel table.
108
        By default it sets logging as false.
109
110
        :param ctx: Command context
111
        :type ctx: discord.ext.commands.Context
112
        :param log_status: Channel is for logging all users
113
        :type log_status: bool
114
        :return: None
115
        """
116
        log_status = bool(log_status)
117
        await utils.insert("admin_channels", [ctx.channel.name, ctx.channel.id, log_status])
118
        await utils.make_embed(
119
            ctx,
120
            color="28b463",
121
            title="Admin Channel Success",
122
            description="Channel has been added with log status: {}".format(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
        :param extension: Extension to reload
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
        :return: None
146
        """
147
        await utils.update_list(self.bot, True)
148
        self.bot.list_updated = datetime.utcnow()
149
        await ctx.send("List updated")
150
151
152
def setup(bot):
153
    """Needed for extension loading"""
154
    bot.add_cog(AdminCog(bot))
155