Passed
Push — main ( 8897ef...d87f8f )
by Bartosz
02:19 queued 01:07
created

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

Complexity

Conditions 2

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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