Skip to content

ctx.defer() Throws error under certain conditions. #1973

@supopur

Description

@supopur

Summary

await ctx.defer() Throws a unknown interaction error after certain time of no usage of the bot.

Reproduction Steps

The possible reproduction steps:

  1. Have a main.py file that loads cogs
  2. Have a cog with a simple slash command
  3. Use await ctx.defer()
  4. Launch the bot and wait
  5. After 30 ish seconds of waiting do next step
  6. Launch said simple slash command and see the console
  7. After launching the command the the command again and it will work
  8. If you do a command after 10s or less of running another command it will work all fine
  9. If you wait another 30s and run the command again it will throw a error.

Minimal Reproducible Code

Main.py:

import discord
from discord.ext import commands

intents = discord.Intents.default()
intents.members = True

bot = commands.Bot(command_prefix='!', intents=intents)

bot.load_extension("cogs.example")

@bot.slash_command(guild_ids=["823188238260633600"])
async def stop(ctx):
    await ctx.defer()
    if ctx.author.guild_permissions.administrator:
        print("Terminating the bot...")
        try:
            await ctx.respond("https://media4.giphy.com/media/CC5MVO9Jx4RqMQRfvT/giphy.gif")
        except:
            pass
        sys.exit("Terminated.")
    else:
        await ctx.respond("https://media.tenor.com/Iv6oKRuAhVEAAAAC/hal9000-im-sorry-dave.gif")

if __name__ == "__main__":
    bot.run(token)

cogs/example.py:

import discord
from discord.ext import commands

class Greetings(commands.Cog):
    def __init__(self, bot):
        self.bot = bot


    @commands.slash_command(guild_ids=["823188238260633600"])
    async def hello(self, ctx):
        self.bot.api.logger("inf", "hello from the cog!")
        await ctx.send(f'Hello {ctx.author}')

    @commands.slash_command(guild_ids=["823188238260633600"])
    async def uptime(self, ctx):
        #Extends the 3s limit to reply so we dont get a unknown interation error.
        await ctx.defer()
        await ctx.followup.send(f"This is a example and i cannot be bothered to do a uptime command :)")

def setup(bot):
    bot.add_cog(Greetings(bot))

Expected Results

Have the ctx.defer() send a thinking state to discord to extend the response
Then have the bot reply

Actual Results

After certain time of inactivity from the bot when i run a command it gives a Unknown Interaction error when it tries do to ctx.defer()
But if you use a command in 10s or less after running a command it will work just fine as expected. This is basically some hibernation thing wich is strange.

Screenshot from the log: Here
Discord screenshot

Intents

intents = discord.Intents.default() intents.members = True

System Information

From my test it doesnt depend on python version/hardware yes i tried running it on a VPS same resoult.

Checklist

  • I have searched the open issues for duplicates.
  • I have shown the entire traceback, if possible.
  • I have removed my token from display, if visible.

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    unconfirmed bugA bug report that needs triaging

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions