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

bot.cogs.regions   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 31
dl 0
loc 79
rs 10
c 0
b 0
f 0
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A regions.RegionCog.add_region() 0 24 3
A regions.RegionCog.list_region() 0 15 2
A regions.RegionCog.__init__() 0 2 1
A regions.RegionCog.cog_check() 0 11 1

1 Function

Rating   Name   Duplication   Size   Complexity  
A regions.setup() 0 3 1
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