fix: refactor bugs

This commit is contained in:
Lukáš Nedbálek 2024-09-28 00:54:40 +02:00
parent 4b3a3a99e0
commit e3e9fd6f6e

36
main.py
View file

@ -41,8 +41,6 @@ async def reply(ctx, message: str):
:param message: The message to reply with. :param message: The message to reply with.
""" """
await ctx.send(message) await ctx.send(message)
else:
logger.error(f'Failed to send the following message: "{message}"')
@bot.event @bot.event
@ -59,16 +57,16 @@ async def grant_role(ctx, study_group_name: str, ukco: int):
:param ukco: The UKCO of the user. :param ukco: The UKCO of the user.
""" """
# Check study group format # Check study group format
if not words[1].startswith('kruh-') or not words[1][5:].isdigit(): if not study_group_name.startswith('kruh-') or not study_group_name[5:].isdigit():
reply_error(ctx, 'Invalid study group format.') await reply_error(ctx, 'Invalid study group format.')
reply(ctx, HELP_MESSAGE) await reply(ctx, HELP_MESSAGE)
return False return
# Check UKCO format # Check UKCO format
if not words[2].isdigit() or not len(words[2]) == 8: if not (1000_0000 < ukco < 9999_9999):
reply_error(ctx, 'Invalid UKCO format.') await reply_error(ctx, 'Invalid UKCO format.')
reply(ctx, HELP_MESSAGE) await reply(ctx, HELP_MESSAGE)
return False return
for guild_id in server_ids: 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 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 continue
await author.add_roles(role) await author.add_roles(role)
await reply(ctx, f'{author.mention} has been granted the {role.mention} role on the [{guild.name}] server.') await reply(ctx, f'{author.mention} has been granted the [{role.name}] 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}].') logger.info(f'Granted role [{role.name}] to [{author.mention}] in [{guild.name}] corresponding to the study group [{study_group_name}].')
return return
await reply_error(ctx, 'Something went wrong. Please check your command and try again.') 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 return
def check_command(message): async def check_command(message):
# Split the message content into words # Split the message content into words
words = message.content.split() words = message.content.split()
@ -105,19 +103,19 @@ def check_command(message):
# Check if the message is a command # Check if the message is a command
if not words[0].startswith('!'): if not words[0].startswith('!'):
reply(message.channel, HELP_MESSAGE) await reply(message.channel, HELP_MESSAGE)
return False return False
# Check if the command is recognized # Check if the command is recognized
if not words[0] == '!grant_role': if not words[0] == '!grant_role':
reply_error(message.channel, 'Unrecognized command.') await reply_error(message.channel, 'Unrecognized command.')
reply(message.channel, HELP_MESSAGE) await reply(message.channel, HELP_MESSAGE)
return False return False
# Check if the number of arguments is correct # Check if the number of arguments is correct
if not len(words) == 3: if not len(words) == 3:
reply_error(message.channel, 'Invalid number of arguments.') await reply_error(message.channel, 'Invalid number of arguments.')
reply(message.channel, HELP_MESSAGE) await reply(message.channel, HELP_MESSAGE)
return False return False
return True return True
@ -129,7 +127,7 @@ async def on_message(message):
if not isinstance(message.channel, discord.DMChannel) or message.author == bot.user: if not isinstance(message.channel, discord.DMChannel) or message.author == bot.user:
return return
is_valid_command = check_command(message) is_valid_command = await check_command(message)
if not is_valid_command: if not is_valid_command:
return return