From ce6ef5d11ee61f803687f2f4b9ccb92933bf1925 Mon Sep 17 00:00:00 2001 From: Choco Date: Sun, 5 Apr 2026 23:55:50 +0800 Subject: [PATCH] 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) --- cogs/basic.py | 9 +++++++++ cogs/playlist.py | 4 ++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/cogs/basic.py b/cogs/basic.py index 47ac547..dc98a71 100644 --- a/cogs/basic.py +++ b/cogs/basic.py @@ -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) diff --git a/cogs/playlist.py b/cogs/playlist.py index 4ad9614..9c56596 100644 --- a/cogs/playlist.py +++ b/cogs/playlist.py @@ -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