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

regions.RegionCog.cog_check()   A

Complexity

Conditions 1

Size

Total Lines 11
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nop 2
dl 0
loc 11
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
        :type ctx: discord.ext.commands.Context
29
        :return: User is bot admin
30
        :rtype: bool
31
        """
32
        return await utils.check_admin(ctx)
33
34
    @commands.command(name="add-region", help="Adds regions")
35
    async def add_region(self, ctx: commands.Context, *, region: str) -> None:
36
        """Add region
37
38
        :param ctx: Command context
39
        :type ctx: discord.ext.commands.Context
40
        :param region: Name of region to add
41
        :type region: str
42
        :return: None
43
        """
44
        is_role = discord.utils.get(ctx.guild.roles, name=region)
45
        if not is_role:
46
            added_region = await ctx.guild.create_role(
47
                name=region,
48
                mentionable=True,
49
                reason=f"Added by {ctx.author.name}",
50
            )
51
            status = await utils.insert("regions", [region, added_region.id])
52
        else:
53
            status = "error"
54
        if status == "error":
55
            await utils.error_message(ctx, "Error creating the region.")
56
        else:
57
            await utils.make_embed(ctx, color="28b463", title="Region has been created.")
58
59
    @commands.command(name="list-regions", help="Lists available regions.")
60
    async def list_region(self, ctx: commands.Context) -> None:
61
        """List regions
62
63
        Admin command to lists the regions. Only returns a list.
64
65
        :param ctx: Command context
66
        :type ctx: discord.ext.commands.Context
67
        :return: None
68
        """
69
        regions = sorted(await utils.fetch("regions", "name"))
70
        formatted = ""
71
        for region in regions:
72
            formatted += f" - {region} \n"
73
        await utils.make_embed(ctx, title="Available Regions:", description=formatted)
74
75
76
def setup(bot):
77
    """Needed for extension loading"""
78
    bot.add_cog(RegionCog(bot))
79