-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.py
More file actions
54 lines (42 loc) · 1.7 KB
/
main.py
File metadata and controls
54 lines (42 loc) · 1.7 KB
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
# coding=utf-8
import asyncio
import logging
import os
import sys
import yaml
import discord_client
# ==========================> LOAD YAML FILE <==================================
with open("bot_config.yaml", 'r', encoding='utf-8') as cfg_stream:
try:
print('parsing config file...')
config = yaml.safe_load(cfg_stream)
except yaml.YAMLError as exc:
print(f'While parsing the config file, the following error occurred:\n{exc}')
sys.exit()
# ==============================================================================
if sys.platform not in config['filesystem']['bot_dir'].keys():
print(f'\'{sys.platform}\' is not a supported platform. Add your system in the cache_dir field in config.yaml.')
sys.exit(0)
# resolve directories and create missing ones
script_dir = os.path.dirname(os.path.realpath(__file__))
work_dir = os.path.expandvars(config['filesystem']['bot_dir'][sys.platform])
os.makedirs(work_dir, exist_ok=True)
# initialize logger
logfile = os.path.join(work_dir, config['logging']['logfile'])
print(f'logging events to: {logfile}')
logging.basicConfig(format='[%(asctime)s] [%(levelname)-8s] --- [%(module)-14s]: %(message)s',
level=logging.WARNING,
handlers=[logging.FileHandler(logfile), logging.StreamHandler()])
logger = logging.getLogger(__name__)
logger.level = logging.INFO
# create bot
bot = discord_client.LotrBot(config, script_dir, work_dir)
async def main():
await bot.start(None)
if __name__ == '__main__':
try:
asyncio.run(main())
except KeyboardInterrupt:
logger.info('keyboard interrupt detected, exiting.')
bot.save_caches()
logger.info('saved caches successfully.\n\n')