Skip to content

Commit

Permalink
[RoleTools] Improve responses in select menus and buttons instead of …
Browse files Browse the repository at this point in the history
…silently ignoring the user on more niche checks
  • Loading branch information
TrustyJAID committed May 11, 2023
1 parent 11b73eb commit b0bc4f5
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
17 changes: 16 additions & 1 deletion roletools/buttons.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import logging
from datetime import datetime, timedelta, timezone
from typing import List, Optional, Union

import discord
Expand Down Expand Up @@ -62,10 +63,24 @@ async def callback(self, interaction: discord.Interaction):
ephemeral=True,
)
return
if await self.view.cog.check_guild_verification(interaction.user, guild):
if wait_time := await self.view.cog.check_guild_verification(interaction.user, guild):
log.debug("Ignoring user due to verification check.")
if wait_time:
wait = datetime.now(timezone.utc) + timedelta(seconds=wait_time)
await interaction.response.send_message(
_(
"I cannot assign roles to you until you have spent more time in this server. Try again {time}."
).format(time=discord.utils.format_dt(wait)),
ephemeral=True,
)
return
if getattr(interaction.user, "pending", False):
await interaction.response.send_message(
_(
"You need to finish your member verification before I can assign you a role."
),
ephemeral=True,
)
return
log.debug(f"Adding role to {interaction.user.name} in {guild}")
response = await self.view.cog.give_roles(interaction.user, [role], _("Button Role"))
Expand Down
25 changes: 23 additions & 2 deletions roletools/select.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import logging
from datetime import datetime, timedelta, timezone
from typing import List, Optional, Union

import discord
Expand Down Expand Up @@ -55,6 +56,7 @@ def __init__(
)
self.name = name
self.disabled_options: List[str] = disabled
self.view: SelectRoleView

async def callback(self, interaction: discord.Interaction):
log.debug("Receiving selection press")
Expand All @@ -81,6 +83,8 @@ async def callback(self, interaction: discord.Interaction):
added_roles = []
removed_roles = []
missing_role = False
pending = False
wait = None
for role_id in role_ids:
role = guild.get_role(role_id)
if role is None:
Expand All @@ -94,12 +98,20 @@ async def callback(self, interaction: discord.Interaction):
config = self.view.cog.config
if role not in interaction.user.roles:
if not await config.role(role).selfassignable():
msg += _(
"{role} Could not be assigned because it is not self assignable."
).format(role=role.mention)
continue

if await self.view.cog.check_guild_verification(interaction.user, guild):
if wait_time := await self.view.cog.check_guild_verification(
interaction.user, guild
):
log.debug("Ignoring user due to verification check.")
if wait_time:
wait = datetime.now(timezone.utc) + timedelta(seconds=wait_time)
continue
if getattr(interaction.user, "pending", False):
pending = True
continue
log.debug(f"Adding role to {interaction.user.name} in {guild}")
response = await self.view.cog.give_roles(
Expand All @@ -110,10 +122,19 @@ async def callback(self, interaction: discord.Interaction):
added_roles.append(role)
elif role in interaction.user.roles:
if not await config.role(role).selfremovable():
msg += _(
"{role} Could not be removed because it is not self assignable."
).format(role=role.mention)
continue
log.debug(f"Removing role from {interaction.user.name} in {guild}")
await self.view.cog.remove_roles(interaction.user, [role], _("Role Selection"))
removed_roles.append(role)
if wait is not None:
msg += _(
"I cannot assign roles to you until you have spent more time in this server. Try again {time}."
).format(time=discord.utils.format_dt(wait))
if pending:
msg += _("You need to finish your member verification before I can assign you a role.")
if missing_role:
msg += _("One or more of the selected roles no longer exists.\n")
if added_roles:
Expand All @@ -134,7 +155,7 @@ async def callback(self, interaction: discord.Interaction):


class SelectRoleView(discord.ui.View):
def __init__(self, cog: commands.Cog):
def __init__(self, cog: RoleToolsMixin):
self.cog = cog
super().__init__(timeout=None)
pass
Expand Down

0 comments on commit b0bc4f5

Please sign in to comment.