Failed Conditions
Branch BonHowi (9d60b2)
by Bartosz
03:20
created

build.cogs.requestcog.RequestCog.on_message()   B

Complexity

Conditions 6

Size

Total Lines 13
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 13
rs 8.6666
c 0
b 0
f 0
cc 6
nop 2
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) -> 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
            for command in self.bot.config["commands"]:
35
                if command["type"] == mon_type["id"]:
36
                    aval_commands.append(command["name"])
37
38
            hex_to_int = "%02x%02x%02x"
39
            if mon_type["id"] == 1:
40
                embed_color = int(hex_to_int % (163, 140, 21), 16)
41
            elif mon_type["id"] == 0:
42
                embed_color = int(hex_to_int % (17, 93, 178), 16)
43
            else:
44
                embed_color = bot_color
45
46
            embed_command = discord.Embed(title=mon_type["label"],
47
                                          description='\n'.join(aval_commands),
48
                                          color=embed_color)
49
            await role_ch.send(embed=embed_command)
50
51
        guide_content = "**/role monstername** - " \
52
                        "get role with monster name to be notified when the monster is spotted, " \
53
                        "use again to remove the role"
54
        embed_guide = discord.Embed(title="Channel Guide", description=guide_content, color=bot_color)
55
        embed_guide.set_footer(text="Check #guides for more info")
56
        await role_ch.send(embed=embed_guide)
57
58
    # Remove normal messages from monster-request channel
59
    @commands.Cog.listener()
60
    async def on_message(self, ctx) -> None:
61
        if ctx.author.id == self.bot.user.id or isinstance(ctx.channel, discord.channel.DMChannel):
62
            return
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
            try:
69
                await ctx.delete()
70
            except discord.errors.NotFound:
71
                pass
72
73
    # Add or remove monster role to an user
74
    @cog_ext.cog_slash(name="role", guild_ids=cogbase.GUILD_IDS,
75
                       description="Get or remove role with monster name to be pinged when the monster is spotted",
76
                       default_permission=True)
77
    async def _role(self, ctx: SlashContext, name) -> None:
78
        if ctx.channel.id != self.bot.ch_role_request:
79
            await ctx.send(f"Use <#{self.bot.ch_role_request}> to request a role!", hidden=True)
80
81
        else:
82
            monster = self.get_monster(ctx, name)
83
            member = ctx.author
84
            if monster:
85
                role = get(member.guild.roles, name=monster["name"])
86
                if role in member.roles:
87
                    await member.remove_roles(role)
88
                    await ctx.send(f"{role} role removed", hidden=True)
89
                else:
90
                    await member.add_roles(role)
91
                    await ctx.send(f"{role} role added", hidden=True)
92
            else:
93
                await ctx.send(f"Monster role not found", hidden=True)
94
95
96
def setup(bot: commands.Bot) -> None:
97
    bot.add_cog(RequestCog(bot))
98