paper-dynasty-discord/paperdynasty.py
Cal Corum bfd72ae0f5 Update logging to RotatingFileHandler
Add auto game end
Calculate stats and decisions
Support raising instantiated exceptions
2024-11-09 23:14:54 -06:00

91 lines
2.5 KiB
Python

import discord
import datetime
import logging
from logging.handlers import RotatingFileHandler
import asyncio
import os
from discord.ext import commands
from in_game.gameplay_models import create_db_and_tables
raw_log_level = os.getenv('LOG_LEVEL')
if raw_log_level == 'DEBUG':
log_level = logging.DEBUG
elif raw_log_level == 'INFO':
log_level = logging.INFO
elif raw_log_level == 'WARN':
log_level = logging.WARNING
else:
log_level = logging.ERROR
# date = f'{datetime.datetime.now().year}-{datetime.datetime.now().month}-{datetime.datetime.now().day}'
# logger.basicConfig(
# filename=f'logs/{date}.log',
# format='%(asctime)s - %(levelname)s - %(message)s',
# level=log_level
# )
# logger.getLogger('discord.http').setLevel(logger.INFO)
logger = logging.getLogger('discord_app')
logger.setLevel(log_level)
handler = RotatingFileHandler(
filename='logs/discord.log',
# encoding='utf-8',
maxBytes=32 * 1024 * 1024, # 32 MiB
backupCount=5, # Rotate through 5 files
)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
# dt_fmt = '%Y-%m-%d %H:%M:%S'
# formatter = logger.Formatter('[{asctime}] [{levelname:<8}] {name}: {message}', dt_fmt, style='{')
# handler.setFormatter(formatter)
logger.addHandler(handler)
COGS = [
'cogs.owner',
'cogs.admins',
'cogs.economy',
'cogs.players',
'cogs.gameplay',
]
intents = discord.Intents.default()
intents.members = True
intents.message_content = True
bot = commands.Bot(command_prefix='.',
intents=intents,
# help_command=None,
description='The Paper Dynasty Bot\nIf you have questions, feel free to contact Cal.',
case_insensitive=True,
owner_id=258104532423147520)
@bot.event
async def on_ready():
logger.info('Logged in as:')
logger.info(bot.user.name)
logger.info(bot.user.id)
# @bot.tree.error
# async def on_error(interaction, error):
# await interaction.channel.send(f'{error}')
async def main():
create_db_and_tables()
for c in COGS:
try:
await bot.load_extension(c)
logger.info(f'Loaded cog: {c}')
except Exception as e:
logger.error(f'Failed to load cog: {c}')
logger.error(f'{e}')
async with bot:
await bot.start(os.environ.get('BOT_TOKEN'))
asyncio.run(main())