我有一个软线下拉菜单,我找不到如何设置超时和如何使按钮禁用超时,我无法找到我应该在哪里添加超时量。
代码:
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)```发布于 2022-03-31 13:09:43
在关于选择类的Pycord文档中,没有提到超时值属性。
超时值是视图类的一个属性,因此可以使用其on_timeout方法禁用选择菜单。
通过使用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()https://stackoverflow.com/questions/71655432
复制相似问题