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

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