From e3e9fd6f6e05fa020dedeacd2d572dd1a6700c0f Mon Sep 17 00:00:00 2001 From: Lukas Nedbalek Date: Sat, 28 Sep 2024 00:54:40 +0200 Subject: [PATCH] fix: refactor bugs --- main.py | 36 +++++++++++++++++------------------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/main.py b/main.py index 34fc48e..87db849 100644 --- a/main.py +++ b/main.py @@ -41,8 +41,6 @@ async def reply(ctx, message: str): :param message: The message to reply with. """ await ctx.send(message) - else: - logger.error(f'Failed to send the following message: "{message}"') @bot.event @@ -59,16 +57,16 @@ async def grant_role(ctx, study_group_name: str, ukco: int): :param ukco: The UKCO of the user. """ # Check study group format - if not words[1].startswith('kruh-') or not words[1][5:].isdigit(): - reply_error(ctx, 'Invalid study group format.') - reply(ctx, HELP_MESSAGE) - return False + if not study_group_name.startswith('kruh-') or not study_group_name[5:].isdigit(): + await reply_error(ctx, 'Invalid study group format.') + await reply(ctx, HELP_MESSAGE) + return # Check UKCO format - if not words[2].isdigit() or not len(words[2]) == 8: - reply_error(ctx, 'Invalid UKCO format.') - reply(ctx, HELP_MESSAGE) - return False + if not (1000_0000 < ukco < 9999_9999): + await reply_error(ctx, 'Invalid UKCO format.') + await reply(ctx, HELP_MESSAGE) + return for guild_id in server_ids: if study_group_name not in study_groups[guild_id]: # Check if the study group is valid for the server @@ -87,8 +85,8 @@ async def grant_role(ctx, study_group_name: str, ukco: int): continue await author.add_roles(role) - await reply(ctx, f'{author.mention} has been granted the {role.mention} role on the [{guild.name}] server.') - logger.info(f'Granted role [{role.mention}] to [{author.mention}] in [{guild.name}] corresponding to the study group [{study_group_name}].') + await reply(ctx, f'{author.mention} has been granted the [{role.name}] role on the [{guild.name}] server.') + logger.info(f'Granted role [{role.name}] to [{author.mention}] in [{guild.name}] corresponding to the study group [{study_group_name}].') return await reply_error(ctx, 'Something went wrong. Please check your command and try again.') @@ -96,7 +94,7 @@ async def grant_role(ctx, study_group_name: str, ukco: int): return -def check_command(message): +async def check_command(message): # Split the message content into words words = message.content.split() @@ -105,19 +103,19 @@ def check_command(message): # Check if the message is a command if not words[0].startswith('!'): - reply(message.channel, HELP_MESSAGE) + await reply(message.channel, HELP_MESSAGE) return False # Check if the command is recognized if not words[0] == '!grant_role': - reply_error(message.channel, 'Unrecognized command.') - reply(message.channel, HELP_MESSAGE) + await reply_error(message.channel, 'Unrecognized command.') + await reply(message.channel, HELP_MESSAGE) return False # Check if the number of arguments is correct if not len(words) == 3: - reply_error(message.channel, 'Invalid number of arguments.') - reply(message.channel, HELP_MESSAGE) + await reply_error(message.channel, 'Invalid number of arguments.') + await reply(message.channel, HELP_MESSAGE) return False return True @@ -129,7 +127,7 @@ async def on_message(message): if not isinstance(message.channel, discord.DMChannel) or message.author == bot.user: return - is_valid_command = check_command(message) + is_valid_command = await check_command(message) if not is_valid_command: return