From cf22845aa6f68c31166423cf1c97626fd8d14d49 Mon Sep 17 00:00:00 2001 From: Lukas Nedbalek Date: Tue, 24 Sep 2024 16:44:14 +0200 Subject: [PATCH] feat: log --- main.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/main.py b/main.py index 87a07ab..e121f96 100644 --- a/main.py +++ b/main.py @@ -1,9 +1,12 @@ +from configparser import ConfigParser +import logging + import discord from discord.ext import commands -from configparser import ConfigParser TOKEN = 'MTE1NDc0ODQ3MzE4MzMwNTc4OQ.Gz_Q7u.02sb2YNV_QQy7Bs19roXlB62mjoMKA6y8aubHU' # Bot token (replace with your own) DEFAULT_CONFIG_FILE = 'config.ini' # Configuration file with server IDs and study group roles +DEFAULT_LOG_FILE = 'kruhobot.log' # Log file for the bot # Create an instance of the bot @@ -12,6 +15,9 @@ intents.members = True bot = commands.Bot(command_prefix='!', intents=intents) +logging.basicConfig(filename=DEFAULT_LOG_FILE, level=logging.INFO) +logger = logging.getLogger("Kruhobot") + async def reply_error(ctx: commands.Context, message: str): await reply(ctx, f'**Error:** {message}', True) @@ -49,7 +55,7 @@ def init(config): @bot.event async def on_ready(): - print(f'Logged in as {bot.user.name}') + logger.info(f'Logged in as {bot.user.name}') @bot.command() @@ -74,9 +80,11 @@ async def grant_role(ctx, study_group_name: str): if role is not None: await author.add_roles(role) await reply(ctx, f'{author.mention} has been granted the {study_group_name} role on the [{guild.name}] server.') + logger.info(f'Granted role [{role_name}] to [{author_id}] in [{guild_id}] corresponding to the study group [{study_group_name}].') return await reply_error(ctx, 'Something went wrong. Please check your command and try again.') + logger.error(f'Failed to grant a role to [{author_id}] in [{guild_id}] corresponding to the study group [{study_group_name}].') return