This repository has been archived by the owner on Jun 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
bot.py
100 lines (76 loc) · 3.02 KB
/
bot.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
90
91
92
93
94
95
96
97
98
99
100
import aiohttp
import datetime
import logging
import subprocess
import yaml
from pathlib import Path
from discord import __version__ as discver, Activity, ActivityType
from discord.ext.commands import Bot
from utils.helpers import get_prefix
from utils.database.db_functions import cache_prefixes
CONFIG_FILE = Path('config.yaml')
LOGDIR = Path('logs')
# Set up logging
def setup_logger() -> logging.Logger:
"""Create and return the root Logger object for the bot."""
LOGDIR.mkdir(exist_ok=True)
timestamp = datetime.datetime.now().strftime('%Y-%m-%d %H-%M-%S')
logfile = LOGDIR / f'{timestamp}.log'
logger = logging.getLogger('bot') # the actual logger instance
logger.setLevel(logging.DEBUG) # capture all log levels
console_log = logging.StreamHandler()
console_log.setLevel(logging.DEBUG) # log levels to be shown at the console
file_log = logging.FileHandler(logfile)
file_log.setLevel(logging.DEBUG) # log levels to be written to file
formatter = logging.Formatter('{asctime} - {name} - {levelname} - {message}', style='{')
console_log.setFormatter(formatter)
file_log.setFormatter(formatter)
logger.addHandler(console_log)
logger.addHandler(file_log)
# additionally, do some of the same configuration for the discord.py logger
discord_logger = logging.getLogger('discord') # the discord.py logging instance
discord_logger.setLevel(logging.INFO) # DEBUG has far too much info
discord_logger.addHandler(console_log)
discord_logger.addHandler(file_log)
return logger
log = setup_logger()
# Run db_structure.py to make sure all tables are created.
async def update_database_tables():
log.info("Running db_structure.py ...")
subprocess.run(["python", "utils/database/db_structure.py"])
log.info("Done running db_structure.py")
# Load configuration
with open(CONFIG_FILE, 'r') as yaml_file:
config = yaml.safe_load(yaml_file)
# Use configuration to start the bot
# TODO: dynamic per-server prefixes using utils.helpers.prefix
bot = Bot(
activity=Activity(
name=f'{config["prefix"]}help | D&D 5e',
type=ActivityType.watching
),
command_prefix=get_prefix,
pm_help=True
)
bot.config = config # assign configuration to a bot attribute for access from cogs
bot.remove_command('help')
bot.start_time = datetime.datetime.now()
@bot.event
async def on_connect():
bot.aiohttp_session = aiohttp.ClientSession() # assign separate ClientSession object for outside requests
await cache_prefixes()
@bot.event
async def on_ready():
log.info(f"Connected as {bot.user}, using discord.py {discver}")
await update_database_tables()
def main():
"""Load cogs, configuration, and start the bot."""
for extension in bot.config['load_extensions']:
try:
log.debug(f'Loading extension: {extension}')
bot.load_extension(extension)
except: # noqa: E722
log.exception(f'Failed to load extension: {extension}')
bot.run(config['token'])
if __name__ == '__main__':
main()