Defer interaction responses in commands

Call await ctx.defer() for discord.Interaction in play, search and playtop to avoid interaction timeouts during long operations. Also move the MongoDB get_user playlist fetch in check_playlist to after the defer so DB work doesn't occur before the interaction is deferred. (cogs/basic.py, cogs/playlist.py)
This commit is contained in:
Choco
2026-04-05 23:55:50 +08:00
parent 56a6b67470
commit ce6ef5d11e
2 changed files with 11 additions and 2 deletions

View File

@@ -122,6 +122,9 @@ class Basic(commands.Cog):
@commands.dynamic_cooldown(cooldown_check, commands.BucketType.guild)
async def play(self, ctx: commands.Context, *, query: str, start: str = "0", end: str = "0") -> None:
"Loads your input into the queue."
if isinstance(ctx, discord.Interaction) and not ctx.interaction.response.is_done():
await ctx.defer()
player: voicelink.Player = ctx.guild.voice_client
if not player:
player = await voicelink.connect_channel(ctx)
@@ -216,6 +219,9 @@ class Basic(commands.Cog):
@commands.dynamic_cooldown(cooldown_check, commands.BucketType.guild)
async def search(self, ctx: commands.Context, *, query: str, platform: str = Config().search_platform.name):
"Searches your query and displays the results."
if isinstance(ctx, discord.Interaction) and not ctx.interaction.response.is_done():
await ctx.defer()
player: voicelink.Player = ctx.guild.voice_client
if not player:
player = await voicelink.connect_channel(ctx)
@@ -259,6 +265,9 @@ class Basic(commands.Cog):
@commands.dynamic_cooldown(cooldown_check, commands.BucketType.guild)
async def playtop(self, ctx: commands.Context, *, query: str, start: str = "0", end: str = "0"):
"Adds a song with the given url or query on the top of the queue."
if isinstance(ctx, discord.Interaction) and not ctx.interaction.response.is_done():
await ctx.defer()
player: voicelink.Player = ctx.guild.voice_client
if not player:
player = await voicelink.connect_channel(ctx)

View File

@@ -86,11 +86,11 @@ async def check_playlist(
share_perm: Optional[str] = None
) -> dict:
"""Get user's playlist data with various filtering options."""
user_playlists = await MongoDBHandler.get_user(ctx.author.id, d_type='playlist')
if isinstance(ctx, discord.Interaction) and not ctx.interaction.response.is_done():
await ctx.defer()
user_playlists = await MongoDBHandler.get_user(ctx.author.id, d_type='playlist')
if full:
return user_playlists