Passed
Push — main ( d12a1e...942891 )
by Bartosz
02:45 queued 01:23
created

build.cogs.requestcog   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 17
eloc 65
dl 0
loc 97
rs 10
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A setup() 0 2 1

4 Methods

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