Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Makes role self more dynamic #1199

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 41 additions & 17 deletions techsupport_bot/commands/role.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,6 @@ async def setup(bot: bot.TechSupportBot) -> None:
bot (bot.TechSupportBot): The bot object
"""
config = extensionconfig.ExtensionConfig()
config.add(
key="self_assignable_roles",
datatype="list",
title="All roles people can assign themselves",
description="The list of roles by name that people can assign themselves",
default=[],
)
config.add(
key="allow_self_assign",
datatype="list",
Expand All @@ -49,6 +42,13 @@ async def setup(bot: bot.TechSupportBot) -> None:
description="The list of roles that are allowed to assign others roles",
default=[],
)
config.add(
key="self_assign_map",
datatype="dict",
title="Map of all the roles allowed in self assign",
description="A map in the format target: [use, use]",
default={},
)
await bot.add_cog(RoleGiver(bot=bot))
bot.add_extension_config("role", config)

Expand Down Expand Up @@ -92,13 +92,37 @@ async def self_role(self: Self, interaction: discord.Interaction) -> None:
# Pull config
config = self.bot.guild_configs[str(interaction.guild.id)]

# Interaction user roles
current_roles = getattr(interaction.user, "roles", [])

# Get the roles map
roles_map = config.extensions.role.self_assign_map.value

allowed_roles_list = []

# Build the allowed roles list
for assignable_role in roles_map:
for allowed_role in roles_map[assignable_role]:
discord_allowed_role = discord.utils.get(
interaction.guild.roles, id=int(allowed_role)
)
if discord_allowed_role in current_roles:
allowed_roles_list.append(assignable_role)
break

if len(allowed_roles_list) == 0:
embed = auxiliary.prepare_deny_embed(
message="You cannot assign yourself any roles right now"
)
await interaction.response.send_message(embed=embed, ephemeral=True)
return

# Get needed config items
roles = config.extensions.role.self_assignable_roles.value
allowed_to_execute = config.extensions.role.allow_self_assign.value

# Call the base function
await self.role_command_base(
interaction, roles, allowed_to_execute, interaction.user
interaction, allowed_roles_list, allowed_to_execute, interaction.user
)

@role_group.command(
Expand Down Expand Up @@ -149,7 +173,7 @@ async def role_command_base(

Args:
interaction (discord.Interaction): The interaction that called this command
assignable_roles (list[str]): A list of roles that are assignabled for this command
assignable_roles (list[str]): A list of role IDs that are assignabled for this command
allowed_roles (list[str]): A list of roles that are allowed to execute this command
member (discord.Member): The member to assign roles to
"""
Expand Down Expand Up @@ -245,18 +269,18 @@ def generate_options(
Args:
user (discord.Member): The user that will be getting the roles applied
guild (discord.Guild): The guild that the roles are from
roles (list[str]): A list of roles by name to add to the options
roles (list[str]): A list of roles by ID to add to the options

Returns:
list[discord.SelectOption]: A list of SelectOption with defaults set
"""
options = []

for role_name in roles:
for role_id in roles:
default = False

# First, get the role
role = discord.utils.get(guild.roles, name=role_name)
role = discord.utils.get(guild.roles, id=int(role_id))
if not role:
continue

Expand All @@ -265,7 +289,7 @@ def generate_options(
default = True

# Third, the option to the list with relevant default
options.append(discord.SelectOption(label=role_name, default=default))
options.append(discord.SelectOption(label=role.name, default=default))
return options

async def modify_roles(
Expand All @@ -280,7 +304,7 @@ async def modify_roles(
"""Modifies a set of roles based on an input and reference list

Args:
config_roles (list[str]): The list of roles allowed to be modified
config_roles (list[str]): The list of roles by ID allowed to be modified
new_roles (list[str]): The list of roles from the config_roles that should be assigned
to the user. Any roles not on this list will be removed
guild (discord.Guild): The guild to assign the roles in
Expand All @@ -291,8 +315,8 @@ async def modify_roles(
added_roles = []
removed_roles = []

for role_name in config_roles:
real_role = discord.utils.get(guild.roles, name=role_name)
for role_id in config_roles:
real_role = discord.utils.get(guild.roles, id=int(role_id))
if not real_role:
continue

Expand Down
Loading