首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使pycord菜单在超时时禁用菜单

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

Stack Overflow用户
提问于 2022-03-29 01:25:45
回答 1查看 1.8K关注 0票数 0

我有一个软线下拉菜单,我找不到如何设置超时和如何使按钮禁用超时,我无法找到我应该在哪里添加超时量。

代码:

代码语言:javascript
复制
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)```
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-03-31 13:09:43

在关于选择类的Pycord文档中,没有提到超时值属性。

超时值是视图类的一个属性,因此可以使用其on_timeout方法禁用选择菜单。

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

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

代码语言:javascript
复制
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()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71655432

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档