regions.RegionCog.list_region()   A
last analyzed

Complexity

Conditions 2

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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