Skip to content

Commit

Permalink
[RoleTools] 1.5.2 Fix Select options not applying roles after last co…
Browse files Browse the repository at this point in the history
…mmit

Improve tracking of deleted select options and actually prevent menus from applying the role if the option is deleted. Note: This means if you want to replace an option with the same name and a new role, etc. You will need to reload to fix existing menus using that option name.
  • Loading branch information
TrustyJAID committed May 9, 2023
1 parent d1d38d8 commit 8d89d57
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ TrustyJAID's Cogs for [Red-DiscordBot](https://github.com/Cog-Creators/Red-Disc
| Reddit | 1.2.0 | <details><summary>A cog to post updates from reddit.</summary>Reddit commands for getting updates on specified subreddits.</details> | TrustyJAID |
| Rekt | 1.0.0 | <details><summary>Get REKT</summary>Are you REKT?</details> | TrustyJAID |
| ReTrigger | 2.22.0 | <details><summary>Trigger events via Regular Expressions!</summary>Trigger events based on regex! Check out <https://regex101.com/> and <https://github.com/TrustyJAID/Trusty-cogs/blob/master/retrigger/README.md> for help setting up the cog. Note: This cog can become quite resource heavy. Optional features are available if the requirements are present such as pillow for image resizing and pytesseract to scan images for text (OCR).</details> | TrustyJAID |
| RoleTools | 1.5.1 | <details><summary>Various role related tools.</summary>Various role utility commands. Including Reaction roles, Sticky roles, and Auto role.</details> | TrustyJAID |
| RoleTools | 1.5.2 | <details><summary>Various role related tools.</summary>Various role utility commands. Including Reaction roles, Sticky roles, and Auto role.</details> | TrustyJAID |
| runescape | 1.3.3 | <details><summary>Show your Runescape stats in discord!</summary>A cog to grab Runescape and OSRS stats and profile information.</details> | TrustyJAID |
| ServerStats | 1.7.0 | <details><summary>A plethora of potentially useful commands for any bot owner.</summary>A plethora of potentially useful commands for any bot owner. Includes a way to track the bot joining new servers, find cheaters on global economies, get user avatars and even larger emojis.</details> | TrustyJAID and Preda |
| Spotify | 1.7.0 | <details><summary>Control Spotify through Discord!</summary>This cog allows you to control Spotify via OAuth through the bot on discord. Use `[p]spotify` to see available commands.</details> | TrustyJAID and NeuroAssassin |
Expand Down
2 changes: 1 addition & 1 deletion roletools/roletools.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class RoleTools(
"""

__author__ = ["TrustyJAID"]
__version__ = "1.5.1"
__version__ = "1.5.2"

def __init__(self, bot: Red):
self.bot = bot
Expand Down
40 changes: 29 additions & 11 deletions roletools/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def __init__(
max_values: int,
placeholder: Optional[str],
options: List[SelectRoleOption],
disabled: List[str] = [],
):
super().__init__(
options=options,
Expand All @@ -53,24 +54,30 @@ def __init__(
custom_id=custom_id,
)
self.name = name
self.disabled_options: List[str] = disabled

async def callback(self, interaction: discord.Interaction):
log.debug("Receiving selection press")

role_ids = []
for option in self.values:
if option.startswith("RTSelect"):
for op in self.options:
if op.disabled:
continue
role_ids.append(int(option.split("-")[-1]))
if option.split("-")[1] in self.disabled_options:
continue
role_ids.append(int(option.split("-")[-1]))

if role_ids:
await interaction.response.defer(ephemeral=True, thinking=True)
elif not role_ids or self.disabled:
elif not role_ids and not self.disabled:
await interaction.response.send_message(
_("One or more of the selected roles are no longer available."), ephemeral=True
)
await interaction.message.edit()
return
elif self.disabled:
await interaction.response.send_message(
_("This selection has been disabled from giving roles."), ephemeral=True
)
await interaction.message.edit()
return
guild = interaction.guild
added_roles = []
Expand Down Expand Up @@ -131,6 +138,11 @@ def __init__(self, cog: commands.Cog):
super().__init__(timeout=None)
pass

async def on_error(self, interaction: discord.Interaction, error: Exception, item: SelectRole):
await interaction.response.send_message(
_("An error occured trying to apply a role to you."), ephemeral=True
)


class SelectOptionRoleConverter(discord.app_commands.Transformer):
@classmethod
Expand Down Expand Up @@ -271,6 +283,7 @@ async def initialize_select(self) -> None:
log.debug(f"Adding Option {select_name}")
view = SelectRoleView(self)
options = []
disabled = []
for option_name in select_data["options"]:
try:
option_data = settings["select_options"][option_name]
Expand All @@ -283,14 +296,14 @@ async def initialize_select(self) -> None:
option = SelectRoleOption(
name=option_name,
label=label,
value=f"RT-{option_name}-{role_id}",
value=f"RTSelect-{option_name}-{role_id}",
role_id=role_id,
description=description,
emoji=emoji,
)
options.append(option)
except KeyError:
continue
disabled.append(option_name)

select = SelectRole(
name=select_name,
Expand All @@ -299,6 +312,7 @@ async def initialize_select(self) -> None:
max_values=select_data["max_values"],
placeholder=select_data["placeholder"],
options=options,
disabled=disabled,
)
view.add_item(select)
self.bot.add_view(view)
Expand Down Expand Up @@ -521,11 +535,15 @@ async def delete_select_option(self, ctx: Context, *, name: str) -> None:
async with self.config.guild(ctx.guild).select_options() as select_options:
if name in select_options:
role_id = select_options[name]["role_id"]
custom_id = f"{name.lower()}-{role_id}"
custom_id = f"RTSelect-{name.lower()}-{role_id}"
for view in self.views:
for child in view.children:
if child.custom_id == custom_id:
child.disabled = True
if not isinstance(child, SelectRole):
continue
options = [i.value for i in child.options]
if custom_id in options:
child.disabled_options.append(name.lower())
log.debug(f"Adding {custom_id} to view")
if name in self.settings.get(ctx.guild.id, {}).get("select_options", {}):
del self.settings[ctx.guild.id]["select_options"][name]
del select_options[name]
Expand Down

0 comments on commit 8d89d57

Please sign in to comment.