Passed
Branch main (f64b1b)
by Bartosz
01:11
created

build.cogs.requestcog   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 15
eloc 54
dl 0
loc 84
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_ready() 0 23 5
A RequestCog.on_message() 0 13 4
A RequestCog.__init__() 0 2 1
A RequestCog._role() 0 20 4
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
15
class RequestCog(cogbase.BaseCog):
16
    def __init__(self, base):
17
        super().__init__(base)
18
19
    # Print available roles/commands on monster-request channel
20
    @commands.Cog.listener()
21
    async def on_ready(self):
22
        role_ch = self.bot.get_channel(self.bot.ch_role_request)
23
        await role_ch.purge(limit=10)
24
25
        for mon_type in self.bot.config["types"]:
26
            if mon_type["id"] in [2, 3, 4]:  # Pass if common/...
27
                continue
28
29
            aval_commands = []
30
            for command in self.bot.config["commands"]:
31
                if command["type"] == mon_type["id"]:
32
                    aval_commands.append(command["name"])
33
34
            embed_command = discord.Embed(title=mon_type["label"], description='\n'.join(aval_commands), color=0x674ea7)
35
            await role_ch.send(embed=embed_command)
36
37
        guide_content = "**/role monstername** - " \
38
                        "get role with monster name to be notified when the monster is spotted,\n" \
39
                        "use again to remove the role\n\n" \
40
                        "*Check #guides for more info*"
41
        embed_guide = discord.Embed(title="Channel Guide", description=guide_content, color=0x674ea7)
42
        await role_ch.send(embed=embed_guide)
43
44
    # Remove normal messages from monster-request channel
45
    @commands.Cog.listener()
46
    async def on_message(self, ctx):
47
        if ctx.author.id == self.bot.user.id:
48
            return
49
50
        if ctx.channel.id == self.bot.ch_role_request:
51
            if ctx.content.startswith("/"):
52
                await ctx.channel.send(
53
                    f"{ctx.author.mention} For adding or removing role use */role monstername* command",
54
                    delete_after=10.0)
55
                await ctx.delete()
56
            else:
57
                await ctx.delete()
58
59
    # Add or remove monster role to an user
60
    @cog_ext.cog_slash(name="role", guild_ids=cogbase.GUILD_IDS,
61
                       description="Function for adding monster role to user",
62
                       default_permission=True)
63
    async def _role(self, ctx: SlashContext, name: str):
64
        if ctx.channel.id != self.bot.ch_role_request:
65
            await ctx.send(f"Use <#{self.bot.ch_role_request}> to request a role!", hidden=True)
66
67
        else:
68
            monster = self.get_monster(ctx, name)
69
            member = ctx.author
70
            if monster:
71
                role = get(member.guild.roles, name=monster["name"])
72
                if role in member.roles:
73
                    await member.remove_roles(role)
74
                    await ctx.send(f"{role} role removed", delete_after=3.0)
75
                else:
76
                    await member.add_roles(role)
77
                    await ctx.send(f"{role} role added", delete_after=3.0)
78
            else:
79
                await ctx.send(f"Monster role not found", delete_after=3.0)
80
81
82
def setup(bot: commands.Bot):
83
    bot.add_cog(RequestCog(bot))
84