rf: grant_role command function

This commit is contained in:
Lukáš Nedbálek 2024-09-28 00:44:14 +02:00
parent 16f26119aa
commit 4b3a3a99e0

36
main.py
View file

@ -51,7 +51,13 @@ async def on_ready():
@bot.command()
async def grant_role(ctx, study_group_name: str):
async def grant_role(ctx, study_group_name: str, ukco: int):
"""
Grant a role to the user based on the study group and UKCO.
:param ctx: The context of the command.
:param study_group_name: The name of the study group.
: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.')
@ -65,28 +71,28 @@ async def grant_role(ctx, study_group_name: str):
return False
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
continue
guild = bot.get_guild(guild_id)
if guild is None:
if guild is None: # Check if the guild exists
continue
author = guild.get_member(author_id)
if author is None:
role = discord.utils.get(guild.roles, id=study_groups[guild_id][study_group_name])
if role is None: # Check if the role exists
continue
if study_group_name in study_groups[guild_id]:
role_name = study_groups[guild_id][study_group_name]
role = discord.utils.get(guild.roles, id=role_name)
author = guild.get_member(ctx.author.id)
if author is None: # Check if the author is a member of the guild
continue
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 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}].')
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}].')
logger.error(f'Failed to grant a role to [{author.mention}] corresponding to the study group [{study_group_name}].')
return