build.cogs.requestcog   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 17
eloc 68
dl 0
loc 99
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A RequestCog.__init__() 0 2 1
B RequestCog.on_ready() 0 36 5
B RequestCog.on_message() 0 13 6
A RequestCog._role() 0 20 4

1 Function

Rating   Name   Duplication   Size   Complexity  
A setup() 0 2 1
1
"""
2
Cog with role related commands available in the Bot.
3
4
Current commands:
5
/role
6
7
"""
8
import discord
9
from discord.ext import commands
10
from discord.utils import get
11
from discord_slash import cog_ext, SlashContext
12
13
import cogs.cogbase as cogbase
14
from modules.utils import get_dominant_color
15
16
17
class RequestCog(cogbase.BaseCog):
18
    def __init__(self, base):
19
        super().__init__(base)
20
21
    # Print available roles/commands on monster-request channel
22
    @commands.Cog.listener()
23
    async def on_ready(self) -> None:
24
        role_ch = self.bot.get_channel(self.bot.ch_role_request)
25
        await role_ch.purge(limit=10)
26
        bot = get(self.bot.get_all_members(), id=self.bot.user.id)
27
        bot_color = get_dominant_color(bot.avatar_url)
28
29
        for mon_type in self.bot.config["types"]:
30
            if mon_type["id"] in [2, 3, 4]:  # Pass if common/...
31
                continue
32
33
            aval_commands = [
34
                command["name"]
35
                for command in self.bot.config["commands"]
36
                if command["type"] == mon_type["id"]
37
            ]
38
39
            hex_to_int = "%02x%02x%02x"
40
            if mon_type["id"] == 1:
41
                embed_color = int(hex_to_int % (163, 140, 21), 16)
42
            elif mon_type["id"] == 0:
43
                embed_color = int(hex_to_int % (17, 93, 178), 16)
44
            else:
45
                embed_color = bot_color
46
47
            embed_command = discord.Embed(title=mon_type["label"],
48
                                          description='\n'.join(aval_commands),
49
                                          color=embed_color)
50
            await role_ch.send(embed=embed_command)
51
52
        guide_content = "**/role monstername** - " \
53
                        "get role with monster name to be notified when the monster is spotted, " \
54
                        "use again to remove the role"
55
        embed_guide = discord.Embed(title="Channel Guide", description=guide_content, color=bot_color)
56
        embed_guide.set_footer(text="Check #guides for more info")
57
        await role_ch.send(embed=embed_guide)
58
59
    # Remove normal messages from monster-request channel
60
    @commands.Cog.listener()
61
    async def on_message(self, ctx) -> None:
62
        if ctx.author.id == self.bot.user.id or isinstance(ctx.channel, discord.channel.DMChannel):
63
            return
64
        if ctx.channel.id == self.bot.ch_role_request:
65
            if ctx.content.startswith("/"):
66
                await ctx.channel.send(
67
                    f"{ctx.author.mention} For adding or removing role use */role monstername* command",
68
                    delete_after=10.0)
69
            try:
70
                await ctx.delete()
71
            except discord.errors.NotFound:
72
                pass
73
74
    # Add or remove monster role to an user
75
    @cog_ext.cog_slash(name="role", guild_ids=cogbase.GUILD_IDS,
76
                       description="Get or remove role with monster name to be pinged when the monster is spotted",
77
                       default_permission=True)
78
    async def _role(self, ctx: SlashContext, name) -> None:
79
        if ctx.channel.id != self.bot.ch_role_request:
80
            await ctx.send(f"Use <#{self.bot.ch_role_request}> to request a role!", hidden=True)
81
82
        else:
83
            monster = self.get_monster(ctx, name)
84
            member = ctx.author
85
            if monster:
86
                role = get(member.guild.roles, name=monster["name"])
87
                if role in member.roles:
88
                    await member.remove_roles(role)
89
                    await ctx.send(f"{role} role removed", hidden=True)
90
                else:
91
                    await member.add_roles(role)
92
                    await ctx.send(f"{role} role added", hidden=True)
93
            else:
94
                await ctx.send("Monster role not found", hidden=True)
95
96
97
def setup(bot: commands.Bot) -> None:
98
    bot.add_cog(RequestCog(bot))
99