如何使pycord select菜单在超时时禁用菜单

How to make pycord select menu disable the menu on timeout

我有一个 pycord 下拉菜单,我找不到如何设置超时以及如何使按钮在超时时禁用,我也找不到我应该在哪里添加超时量。

代码:


class Dropdown(discord.ui.Select):
    def __init__(self, bot, timeout=1):
        # For example, you can use self.bot to retrieve a user or perform other functions in the callback.
        self.bot = bot
        self.inter = None

        # Alternatively you can use Interaction.client, so you don't need to pass the bot instance.
        # Set the options that will be presented inside the dropdown
        options = [
            discord.SelectOption(
                label="Red", description="Your favourite colour is red", emoji=""),
            discord.SelectOption(
                label="Green", description="Your favourite colour is green", emoji=""),
            discord.SelectOption(
                label="Blue", description="Your favourite colour is blue", emoji=""),
        ]

        # The placeholder is what will be shown when no option is chosen
        # The min and max values indicate we can only pick one of the three options
        # The options parameter defines the dropdown options. We defined this above
        super().__init__(
            placeholder="Choose your favourite colour...",
            min_values=1,
            max_values=1,
            options=options,


        )

    async def callback(self, interaction: discord.Interaction):
        # Use the interaction object to send a response message containing
        # The user's favourite colour or choice. The self object refers to the
        # Select object, and the values attribute gets a list of the user's
        # selected options. We only want the first one.
        await interaction.response.send_message(f"Your favourite colour is {self.values[0]}")

    async def on_timeout(self):
        print("test")


class DropdownView(discord.ui.View):
    def __init__(self, bot):
        self.bot = bot
        super().__init__()

        # Adds the dropdown to our view object.
        self.add_item(Dropdown(self.bot))


@bot.slash_command(guild_ids=[954586898549059594])
async def button(ctx):
    await ctx.defer()
    view = DropdownView(bot)

    # Sending a message containing our view
    await ctx.followup.send("Pick your favourite colour:", view=view)```

在关于 Select class 的 Pycord 文档中,没有提到超时 属性。

Timeout 是 View class 的 属性,因此您可以使用它的 on_timeout 方法来禁用您的 Select菜单。

通过使用clear_items函数,菜单将在超时时被禁用。

下面的代码不会执行回调。

class Dropdown(discord.ui.Select):
    def __init__(self, bot):
        # For example, you can use self.bot to retrieve a user or perform other functions in the callback.
        self.bot = bot
        self.inter = None

        # Alternatively you can use Interaction.client, so you don't need to pass the bot instance.
        # Set the options that will be presented inside the dropdown
        options = [
            discord.SelectOption(
                label="Red", description="Your favourite colour is red", emoji=""),
            discord.SelectOption(
                label="Green", description="Your favourite colour is green", emoji=""),
            discord.SelectOption(
                label="Blue", description="Your favourite colour is blue", emoji=""),
        ]

        # The placeholder is what will be shown when no option is chosen
        # The min and max values indicate we can only pick one of the three options
        # The options parameter defines the dropdown options. We defined this above
        super().__init__(
            placeholder="Choose your favourite colour...",
            min_values=1,
            max_values=1,
            options=options 


        )

    async def callback(self, interaction: discord.Interaction):
        # Use the interaction object to send a response message containing
        # The user's favourite colour or choice. The self object refers to the
        # Select object, and the values attribute gets a list of the user's
        # selected options. We only want the first one.
        await interaction.response.send_message(f"Your favourite colour is {self.values[0]}")


class DropdownView(discord.ui.View):
    def __init__(self, bot, timeout=1):
        self.bot = bot
        super().__init__(timeout=timeout)

        # Adds the dropdown to our view object.
        self.add_item(Dropdown(self.bot))

    async def on_timeout(self):
        print("test")
        self.clear_items()