forked from Aurorastation/BOREALISbot2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
89 lines (68 loc) · 2.3 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import logging.config
import os
import yaml
from core import *
def setup_logging(
default_path='logging.yml',
default_level=logging.INFO,
env_key='LOG_CFG'
):
"""Setup logging configuration
"""
path = default_path
value = os.getenv(env_key, None)
if value:
path = value
if os.path.exists(path):
with open(path, 'rt') as f:
config = yaml.safe_load(f.read())
logging.config.dictConfig(config)
else:
logging.basicConfig(level=default_level)
## LOGGER
setup_logging()
logger = logging.getLogger(__name__)
## GLOBALS
config = None
api = None
scheduler = None
INIT_EXT = {"cogs.owner"}
## CONFIG INIT
try:
config = subsystems.Config("config.yml")
config.setup()
except ConfigError as err:
logger.exception("Error initializing Config object.")
raise RuntimeError("Stopping now.")
INIT_EXT = INIT_EXT.union(set(config.bot["autoload_cogs"]))
## API INIT
try:
api = subsystems.API(config)
except ApiError as err:
logger.exception("Error initializing API object.")
raise RuntimeError("Stopping now.")
## BOT INIT
bot = Borealis(config.bot["prefix"], config, api,
description="Borealis version 3.5.0, here to assist in any SS13 related matters!")
try:
scheduler = subsystems.TaskScheduler(bot, config.scheduler["interval"])
scheduler.add_task(43200, bot.UserRepo().update_auths, "update_users", init_now=True,
is_coro=True)
scheduler.add_task(43200, config.update_channels, "update_channels", init_now=True,
args=[api], is_coro=True)
scheduler.add_task(1800, bot.process_temporary_bans, "process_bans", init_now=True, is_coro=True)
except SchedulerError as err:
logger.exception("Error initializing scheduler object.")
raise RuntimeError("Stopping now.")
@bot.event
async def on_ready():
logger.info("Bot ready. Logged in as: %s - %s", bot.user.name, bot.user.id)
if __name__ == '__main__':
for ext in INIT_EXT:
try:
bot.load_extension(ext)
except Exception as e:
logger.error("MAIN: Failed to load extension: %s.", ext, exc_info=True)
logger.info("Bot up and running.")
bot.loop.create_task(scheduler.run_loop())
bot.run(config.bot["token"], bot=True, reconnect=True)