Implement defer#770
Open
MattyTheHacker wants to merge 3 commits into
Open
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates the “add user/role to channel/thread” slash commands to defer their interaction responses, aiming to prevent Discord interaction timeouts during slower operations.
Changes:
- Added
ctx.defer()to/add-users-to-channeland/add-role-to-channel. - Switched success responses from
ctx.respond(...)toctx.followup.send(...)to align with deferred interactions. - Simplified channel/thread type checking with a tuple-based
isinstancecall.
Comments suppressed due to low confidence (2)
cogs/add_users_to_threads_and_channels.py:293
await ctx.defer()is called before validation branches that useself.command_send_error(...).command_send_errorultimately callsinteraction.respond(...)(utils/tex_bot_base_cog.py), which will fail once the interaction has already been deferred/responded. In this function that happens in therole_to_add is Nonepath. Move thedefer()call to after all early-return validations (or updatesend_error/command_send_errorto useinteraction.followup.sendwhen the interaction response is already done).
await ctx.defer()
main_guild: discord.Guild = ctx.bot.main_guild
try:
role_id: int = int(role_id_str)
except ValueError:
logger.debug("Role ID: %s is not a valid ID.", role_id_str)
await ctx.respond(content=f"The role: {role_id_str} is not valid.")
return
role_to_add: discord.Role | None = discord.utils.get(main_guild.roles, id=role_id)
if role_to_add is None:
await self.command_send_error(
ctx, message=f"The role: <@{role_id}> is not valid or couldn't be found."
)
return
cogs/add_users_to_threads_and_channels.py:277
await ctx.defer()here should likely beawait ctx.defer(ephemeral=True)since the command’s user-facing responses are sent ephemerally. Otherwise the deferred "thinking" indicator is visible to everyone in the channel/thread.
await ctx.defer()
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> Signed-off-by: Matty Widdop <18513864+MattyTheHacker@users.noreply.github.com>
Signed-off-by: Matty Widdop <18513864+MattyTheHacker@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Unable to reproduce the 403 error but it has since started timing out so added defer bits.