Passed
Push — master ( 58f506...9afc6c )
by Cyb3r
01:28 queued 11s
created

CCC_Bot.on_ready()   A

Complexity

Conditions 3

Size

Total Lines 18
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 15
nop 0
dl 0
loc 18
rs 9.65
c 0
b 0
f 0
1
2
"""CCC Bot
3
---
4
Discord Bot for Competitive Cyber Clubs Server.
5
6
All values are loaded from environment variables. Also can be loaded from a .env file.
7
Required Variables:
8
---
9
DISCORD_TOKEN: The token for the bot.
10
OWNER_NAME: The username of the bot owner
11
OWNER_ID: The Discord ID of the bot owner
12
13
Optional:
14
---
15
LOG_LEVEL {DEFAULT: INFO}:
16
"""
17
import os
18
import sys
19
from datetime import datetime
20
from discord.ext import commands
21
import discord
22
from dotenv import load_dotenv
23
import utils
24
25
26
load_dotenv()
27
TOKEN = os.getenv('DISCORD_TOKEN')
28
OWNER_NAME = os.getenv('OWNER_NAME')
29
OWNER_ID = os.getenv('OWNER_ID')
30
log_level = os.getenv('LOG_LEVEL')
31
if not log_level:
32
    log_level = "INFO"
33
34
35
utils.table_create()
36
log = utils.make_logger("bot", log_level)
37
log.info("Starting up")
38
log.debug("Using discord.py version: {} and Python version {}"
39
          .format(discord.__version__, sys.version[0:5]))
40
41
42
initial_extensions = [
43
    'cogs.admin',
44
    'cogs.errors',
45
    'cogs.events',
46
    'cogs.health',
47
    'cogs.misc',
48
    'cogs.rank',
49
    'cogs.regions',
50
    'cogs.schools',
51
    'cogs.search',
52
    'cogs.tasks']
53
54
description = ("The Discord bot that assists with the Competitive Cyber Club Discord\n"
55
               "If you experience any issues then please use the `$report` command.")
56
57
58
class CCC_Bot(commands.Bot):  # pylint: disable=missing-class-docstring
59
    def __init__(self):
60
        super().__init__(command_prefix="$",
61
                         owner_id=OWNER_ID,
62
                         description=description)
63
64
        self.uptime = datetime.utcnow()
65
66
    async def on_ready(self):
67
        "Startup which shows servers it has conencted to"
68
        log.info(
69
            '{} is connected to the following guild: '
70
            '{}'.format(self.user, self.guilds[0].name)
71
        )
72
        await utils.insert("bot_admins", [OWNER_NAME, int(OWNER_ID)])
73
        admin_roles = await utils.select("keys", "value", "key", "admin_role")
74
        for admin_role in admin_roles:
75
            role = discord.utils.get(self.guilds[0].roles,
76
                                     name=admin_role)
77
            for admin in role.members:
78
                await utils.insert("bot_admins", [admin.name, admin.id])
79
        await self.change_presence(activity=discord.Activity(
80
            name='Here to help!', type=discord.ActivityType.playing))
81
        for extension in initial_extensions:
82
            try:
83
                self.load_extension(extension)
84
            except commands.ExtensionError as e:
85
                log.error('Failed to load extension {}. {}'.format(extension, e))
86
87
88
bot = CCC_Bot()
89
bot.run(TOKEN, reconnect=True)
90