Passed
Branch main (55bb28)
by Bartosz
01:17
created

build.cogs.cogbase.BaseCog.get_monster()   A

Complexity

Conditions 5

Size

Total Lines 30
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 30
rs 9.1333
c 0
b 0
f 0
cc 5
nop 3
1
import discord
2
from discord.ext import commands
3
from discord_slash.model import SlashCommandPermissionType
4
from discord_slash.utils.manage_commands import create_permission
5
from modules.get_settings import get_settings
6
7
GUILD_IDS = get_settings("guild")
8
MODERATION_IDS = get_settings("MOD_ROLES")
9
PERMISSION_MODS = {
10
    GUILD_IDS[0]: [
11
        create_permission(MODERATION_IDS[0], SlashCommandPermissionType.ROLE, True),
12
        create_permission(MODERATION_IDS[1], SlashCommandPermissionType.ROLE, True)
13
    ]
14
}
15
PERMISSION_ADMINS = {
16
    GUILD_IDS[0]: [
17
        create_permission(get_settings("ADMIN"), SlashCommandPermissionType.USER, True)
18
    ]
19
}
20
21
22
# noinspection PyTypeChecker
23
class BaseCog(commands.Cog):
24
    def __init__(self, bot):
25
        self.bot = bot
26
        print(f"[{self.__class__.__name__}]: Init")
27
28
    def get_bot(self):
29
        return self.bot
30
31
    # Find monster in config
32
    def get_monster(self, ctx, name: str):
33
        """
34
35
        :param ctx:
36
        :type ctx:
37
        :param name:
38
        :type name:
39
        :return:
40
        :rtype:
41
        """
42
        monster = []
43
        name = name.lower()
44
45
        for monsters in self.bot.config["commands"]:
46
            monster = self.find_monster(monsters, name)
47
            if monster:
48
                break
49
50
        if not monster:
51
            print(f"[{self.__class__.__name__}]: Monster not found")
52
            return
53
54
        monster["role"] = discord.utils.get(ctx.guild.roles, name=monster["name"])
55
        if not monster["role"]:
56
            print(f"[{self.__class__.__name__}]: Failed to fetch roleID for monster {monster['name']}")
57
            return
58
59
        else:
60
            monster["role"] = monster["role"].id
61
        return monster
62
63
    @staticmethod
64
    def find_monster(monsters, name):
65
        if monsters["name"].lower() == name:
66
            return monsters
67
        elif name in monsters["triggers"]:
68
            return monsters
69