Passed
Push — master ( 58f506...9afc6c )
by Cyb3r
01:28 queued 11s
created

cogs.regions.setup()   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
"""Cog responsible for region management"""
2
import discord
3
from discord.ext import commands
4
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
    def __init__(self, bot):
23
        self.bot = bot
24
25
    async def cog_check(self, ctx):
26
        """cog_check
27
        ---
28
29
        cog_check is set for the whole cog. Which makes all the commands in health admin only.
30
31
        Arguments:
32
        ---
33
            ctx {discord.ext.commands.Context} -- Context of the command.
34
35
        Returns:
36
            bool -- True if the user in the bot admins
37
        """
38
        return await utils.check_admin(ctx)
39
40
    @commands.command(name="add-region",
41
                      help="Adds regions")
42
    async def add_region(self, ctx, *, 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(name=region,
56
                                                       mentionable=True,
57
                                                       reason="Added by {}".format(ctx.author.name))
58
            status = await utils.insert("regions", [region, added_region.id])
59
        else:
60
            status = "error"
61
        if status == "error":
62
            await utils.make_embed(ctx, color="FF0000",
63
                                   title="There was an error creating the region.")
64
        else:
65
            await utils.make_embed(ctx, color="28b463", title="Region has been created.")
66
67
    @commands.command(name="list-regions",
68
                      help="Lists available regions.")
69
    async def list_region(self, ctx):
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 = 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