feat: log

This commit is contained in:
Lukáš Nedbálek 2024-09-24 16:44:14 +02:00
parent cdd68052ca
commit cf22845aa6

12
main.py
View file

@ -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