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

bot.cogs.regions.RegionCog.__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 responsible for region management"""
2
import discord
3
from discord.ext import commands
4
from bot import utils
5
6
7
class RegionCog(commands.Cog, name="Regions"):
8
    """RegionCog
9
    ---
10
11
    Cog that holds the region commands
12
13
    Commands:
14
    ---
15
        `add-region`: Command that adds a region and its role to to the regions database.
16
        `list-regions`: Commands that list all the regions available to join.
17
18
    Arguments:
19
    ---
20
        bot {discord.commands.Bot} -- The bot
21
    """
22
23
    def __init__(self, bot):
24
        self.bot = bot
25
26
    async def cog_check(self, ctx: commands.Context):
27
        """cog_check
28
        ---
29
30
        cog_check is set for the whole cog. Which makes all the commands in health admin only.
31
32
        Arguments:
33
        ---
34
            ctx {discord.ext.commands.Context} -- Context of the command.
35
36
        Returns:
37
            bool -- True if the user in the bot admins
38
        """
39
        return await utils.check_admin(ctx)
40
41
    @commands.command(name="add-region", help="Adds regions")
42
    async def add_region(self, ctx: commands.Context, *, region: str):
43
        """Add_region
44
        ---
45
46
        Allows admins to add regions.
47
48
        Arguments:
49
        ---
50
            ctx {discord.ext.commands.Context} -- Context of the command.
51
            region {str} -- Name of region to add.
52
        """
53
        is_role = discord.utils.get(ctx.guild.roles, name=region)
54
        if not is_role:
55
            added_region = await ctx.guild.create_role(
56
                name=region,
57
                mentionable=True,
58
                reason=f"Added by {ctx.author.name}",
59
            )
60
            status = await utils.insert("regions", [region, added_region.id])
61
        else:
62
            status = "error"
63
        if status == "error":
64
            await utils.error_message(ctx, "Error creating the region.")
65
        else:
66
            await utils.make_embed(ctx, color="28b463", title="Region has been created.")
67
68
    @commands.command(name="list-regions", help="Lists available regions.")
69
    async def list_region(self, ctx: commands.Context):
70
        """list-regions
71
        ---
72
73
        Admin command to lists the regions. Only returns a list.
74
75
        Arguments:
76
        ---
77
            ctx {discord.ext.commands.Context} -- Context of the command.
78
        """
79
        regions = sorted(await utils.fetch("regions", "name"))
80
        formatted = ""
81
        for region in regions:
82
            formatted += " - {} \n".format(region)
83
        await utils.make_embed(ctx, title="Available Regions:", description=formatted)
84
85
86
def setup(bot):
87
    """Needed for extension loading"""
88
    bot.add_cog(RegionCog(bot))
89