bot.main   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 53
dl 0
loc 97
rs 10
c 0
b 0
f 0
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
B CCC_Bot.on_ready() 0 34 7
A CCC_Bot.__init__() 0 11 1
1
"""CCC Bot
2
---
3
Discord Bot for Competitive Cyber Clubs Server.
4
5
All values are loaded from environment variables. Also can be loaded from a .env file.
6
Required Variables:
7
---
8
DISCORD_TOKEN: The token for the bot.
9
10
11
Optional:
12
---
13
LOG_LEVEL {DEFAULT: INFO}:
14
"""
15
import os
16
import sys
17
from datetime import datetime
18
from discord.ext import commands
19
import discord
20
from bot import utils
21
22
23
TOKEN = os.environ["DISCORD_TOKEN"]
24
25
VERSION = "v0.2.1"
26
27
log = utils.make_logger("bot", os.getenv("LOG_LEVEL", "INFO"))
28
log.info("Starting up")
29
log.debug(f"Using discord.py version: {discord.__version__} and Python version {sys.version[0:5]}")
30
31
32
COGS_DIR = "./bot/cogs"
33
34
description = (
35
    "This Discord bot that assists with the Competitive Cyber Club Discord\n"
36
    "If you experience any issues then please use the ?report feature.\n"
37
    f"Version: {VERSION}"
38
)
39
40
41
class CCC_Bot(commands.Bot):
42
    """Main class for running bot"""
43
44
    def __init__(self):
45
        super().__init__(
46
            command_prefix=("?", "$"), intents=discord.Intents.all(), description=description
47
        )
48
        self.log = log
49
        self.uptime = datetime.utcnow()
50
        self.list_updated, self.school_list = "", None
51
        self.__version__ = VERSION
52
        self.daily_reporting = True
53
        self.description = (
54
            "This Discord bot that assists with the Competitive Cyber Club Discord\n"
55
            "If you experience any issues then please use the ?report feature.\n"
56
            f"Version: {self.__version__}"
57
        )
58
59
    async def on_ready(self):
60
        """
61
        On Ready
62
63
        Startup function which shows servers it has connected to
64
        """
65
        await utils.update_list(self, not os.path.exists("school_list.csv"))
66
        app_info = await self.application_info()
67
        await utils.insert("bot_admins", [app_info.owner.name, app_info.owner.id])
68
        admin_roles = await utils.select("keys", "value", "key", "admin_role")
69
        for admin_role in admin_roles:
70
            for guild in self.guilds:
71
                role = discord.utils.get(guild.roles, name=admin_role)
72
                if role is not None:
73
                    for admin in role.members:
74
                        await utils.insert("bot_admins", [admin.name, admin.id])
75
        await self.change_presence(
76
            activity=discord.Activity(name="?help", type=discord.ActivityType.playing)
77
        )
78
        # Load all python files in the cogs directory
79
        for extension in [
80
            f.replace(".py", "")
81
            for f in os.listdir(COGS_DIR)
82
            if os.path.isfile(os.path.join(COGS_DIR, f))
83
        ]:
84
            try:
85
                log.debug(f"Loading cog: cogs.{extension}")
86
                await self.load_extension(f"cogs.{extension}")
87
            except commands.ExtensionError as e:
88
                log.error(f"Failed to load extension {extension}. {e}")
89
        await self.change_presence(
90
            activity=discord.Activity(name="?help", type=discord.ActivityType.playing)
91
        )
92
        log.info("Bot is ready to go")
93
94
95
bot = CCC_Bot()
96
bot.run(TOKEN, reconnect=True)
97