Passed
Push — main ( 4d254e...b95c28 )
by Bartosz
02:30 queued 01:14
created

build.cogs.requestcog.setup()   A

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nop 1
dl 0
loc 2
rs 10
c 0
b 0
f 0
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 RoleCog(cogbase.BaseCog):
16
    def __init__(self, base):
17
        super().__init__(base)
18
19
    # Print available roles/commands on monster-request
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 [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=0x00ff00)
35
            await role_ch.send(embed=embed_command)
36
37
        guide_content = "TBA"
38
        embed_guide = discord.Embed(title="Channel Guide", description=guide_content)
39
        await role_ch.send(embed=embed_guide)
40
41
    # Remove normal messages from monster-request
42
    @commands.Cog.listener()
43
    async def on_message(self, ctx):
44
        if ctx.author.id == self.bot.user.id:
45
            return
46
47
        if ctx.channel.id == self.bot.ch_role_request:
48
            if ctx.content.startswith("/"):
49
                await ctx.channel.send(
50
                    f"{ctx.author.mention} For adding or removing role use */role monstername* command",
51
                    delete_after=10.0)
52
                await ctx.delete()
53
            else:
54
                await ctx.delete()
55
56
    # Add or remove monster role to an user
57
    @cog_ext.cog_slash(name="role", guild_ids=cogbase.GUILD_IDS,
58
                       description="Function for adding monster role to user",
59
                       default_permission=True)
60
    async def _role(self, ctx: SlashContext, monster_name: str):
61
        if ctx.channel.id != self.bot.ch_role_request:
62
            await ctx.send(f"Use <#{self.bot.ch_role_request}> to request a role!", hidden=True)
63
            return
64
65
        else:
66
            monster = await self.get_monster(ctx, monster_name)
67
            member = ctx.author
68
            if monster:
69
                role = get(member.guild.roles, name=monster["name"])
70
                if role in member.roles:
71
                    await member.remove_roles(role)
72
                    await ctx.send(f"{role} role removed", delete_after=3.0)
73
                else:
74
                    await member.add_roles(role)
75
                    await ctx.send(f"{role} role added", delete_after=3.0)
76
            else:
77
                await ctx.send(f"Monster role not found", delete_after=3.0)
78
79
80
def setup(bot: commands.Bot):
81
    bot.add_cog(RoleCog(bot))
82