Defxult / reactionmenu

A library to create a discord.py 2.0+ paginator (reaction menu/buttons menu). Supports pagination with buttons, reactions, and category selection using selects.
MIT License
115 stars 12 forks source link

What's a way to `defer` the ViewMenu? #48

Closed Dynamixus closed 1 year ago

Dynamixus commented 1 year ago

Describe the bug

Title.

I keep getting this error: discord.ext.commands.errors.HybridCommandError: Hybrid command raised an error: Command 'char' raised an exception: NotFound: 404 Not Found (error code: 10062): Unknown interaction when using the command as a slash command. Command works fine when I use the prefix command.

And upon googling I found that it may be because I used web requests frequently in this specific command. A solution I found requires me to defer my response via:

await interaction.response.defer()
# later
await interaction.followup.send(...)

But looking through the documentation I don't see an implementation of the sort of this. Is there a workaround for this?

Minimal Reproducible Code

from reactionmenu import ViewMenu, ViewSelect, Page, ViewButton
import discord
from discord import app_commands
from discord.ext import commands
from discord.ext.commands import Context
import json
import requests
from bs4 import BeautifulSoup
from urllib.request import urlopen

class General(commands.Cog, name="general"):
    def __init__(self, bot):
        self.bot = bot

        @commands.hybrid_command(name = "uwu", description = "...")
    @checks.not_blacklisted()
    async def uwu(self, context: Context) -> None:

        menu = ViewMenu(context, menu_type = ViewMenu.TypeEmbed, disable_items_on_timeout = False, remove_items_on_timeout = True, timeout = 600.0)

        page1 = discord.Embed(
            title="uwu1",
            description= webrequests
            color=0x752C18,
            timestamp=datetime.now(),
            )

        page2 = discord.Embed(
            title="uwu2",
            description=webrequests
            )

        menu.add_page(page1)
        menu.add_page(page2)

        menu.add_button(ViewButton(style=discord.ButtonStyle.primary, emoji='👑', label='x', custom_id = ViewButton.ID_GO_TO_FIRST_PAGE))
        menu.add_button(ViewButton(style=discord.ButtonStyle.primary, emoji='💎', label='y', custom_id = ViewButton.ID_GO_TO_LAST_PAGE))
        menu.add_button(ViewButton(style=discord.ButtonStyle.danger, label = "Close", custom_id = ViewButton.ID_END_SESSION))

        await menu.start()

Traceback

discord.ext.commands.errors.HybridCommandError: Hybrid command raised an error: Command 'char' raised an exception: NotFound: 404 Not Found (error code: 10062): Unknown interaction

Library Version

Required Checklist

Additional Comments

No response

Defxult commented 1 year ago

No such error would occur if i copy/paste that code. If you want to defer, just use interaction.response.defer(). Buttons still respond after a interaction has been defered.

Dynamixus commented 1 year ago

I really just used a boiler code, supposing that I do web requests for the descriptions of the embeds that will take at least 3 seconds or more to get the data. At which point, the command will send a The application did not respond ephemeral message on Discord like so:

image

And the console will show this traceback:

Traceback (most recent call last):
  File "C:\Users\Xer\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\client.py", line 441, in _run_event
    await coro(*args, **kwargs)
  File "C:\Users\Xer\source\repos\RenGenBreadCloneStable\bot.py", line 350, in on_command_error
    raise error
  File "C:\Users\Xer\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\ext\commands\hybrid.py", line 438, in _invoke_with_namespace
    value = await self._do_call(ctx, ctx.kwargs)  # type: ignore
  File "C:\Users\Xer\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\app_commands\commands.py", line 846, in _do_call
    raise CommandInvokeError(self, e) from e
discord.ext.commands.errors.HybridCommandError: Hybrid command raised an error: Command 'char' raised an exception: NotFound: 404 Not Found (error code: 10062): Unknown interaction

I tried interaction.response.defer() but it still doesn't work.

Dynamixus commented 1 year ago

~I tried interaction.response.defer() but it still doesn't work.~

Sorry about that, I just learned that I should defer before I do stuff that takes a long time. Thank you for the time!