rf: grant_role command function
This commit is contained in:
parent
16f26119aa
commit
4b3a3a99e0
1 changed files with 21 additions and 15 deletions
36
main.py
36
main.py
|
@ -51,7 +51,13 @@ async def on_ready():
|
||||||
|
|
||||||
|
|
||||||
@bot.command()
|
@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
|
# Check study group format
|
||||||
if not words[1].startswith('kruh-') or not words[1][5:].isdigit():
|
if not words[1].startswith('kruh-') or not words[1][5:].isdigit():
|
||||||
reply_error(ctx, 'Invalid study group format.')
|
reply_error(ctx, 'Invalid study group format.')
|
||||||
|
@ -65,28 +71,28 @@ async def grant_role(ctx, study_group_name: str):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
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
|
||||||
|
continue
|
||||||
|
|
||||||
guild = bot.get_guild(guild_id)
|
guild = bot.get_guild(guild_id)
|
||||||
|
if guild is None: # Check if the guild exists
|
||||||
if guild is None:
|
|
||||||
continue
|
continue
|
||||||
|
|
||||||
author = guild.get_member(author_id)
|
role = discord.utils.get(guild.roles, id=study_groups[guild_id][study_group_name])
|
||||||
|
if role is None: # Check if the role exists
|
||||||
if author is None:
|
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if study_group_name in study_groups[guild_id]:
|
author = guild.get_member(ctx.author.id)
|
||||||
role_name = study_groups[guild_id][study_group_name]
|
if author is None: # Check if the author is a member of the guild
|
||||||
role = discord.utils.get(guild.roles, id=role_name)
|
continue
|
||||||
|
|
||||||
if role is not None:
|
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 {study_group_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_id}] in [{guild_id}] 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.')
|
||||||
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
|
return
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue