按下时间已到的按钮时,提示“时间已到,请重复写入的命令”。如何在discord.py上实现它?
我尝试阅读文档,但没有任何帮助,我知道有些机器人有这样的功能
我尝试过这样的
import discord
from discord.ext import commands
class MyView(discord.ui.View):
def __init__(self, timeout=10):
super().__init__(timeout=timeout)
self.timeout_reached = False
async def on_timeout(self):
self.timeout_reached = True
async def on_error(self, error, item, interaction: discord.Interaction):
if self.is_finished():
await interaction.followup.send('Times up!', ephemeral=True)
else:
await interaction.followup.send('An error occurred!', ephemeral=True)
@discord.ui.button(label='Random Button', style=discord.ButtonStyle.primary)
async def random_button(self, interaction: discord.Interaction, button: discord.ui.Button):
if self.is_finished():
await interaction.response.send_message('Times up!', ephemeral=True)
else:
await interaction.response.send_message('The button has been pressed!', ephemeral=True)
class ExampleCog(commands.Cog):
def __init__(self, bot):
self.bot = bot
@commands.command()
async def random_button(self, ctx):
view = MyView()
await ctx.send('Press the button!', view=view)
async def setup(bot):
await bot.add_cog(ExampleCog(bot))
遗憾的是,
on_timeout
方法不需要任何参数。您可以将最新的交互存储在实例变量中,然后使用它来发送消息。
class MyView(discord.ui.View):
def __init__(self, timeout: int = 10) -> None:
super().__init__(timeout=timeout)
self.latest_interaction = None
async def on_timeout(self) -> None:
if self.latest_interaction is not None:
await self.latest_interaction.followup.send("Retry")
async def interaction_check(self, interaction: Interaction) -> bool:
result = await super().interaction_check(interaction)
# store the interaction in our instance variables
self.latest_interaction = interaction
if not result:
await interaction.response.defer()
return result
async def on_error(self, error, item, interaction: discord.Interaction) -> None:
await interaction.followup.send('An error occurred!', ephemeral=True)
@discord.ui.button(label='Random Button', style=discord.ButtonStyle.primary)
async def random_button(self, interaction: discord.Interaction, button: discord.ui.Button) -> None:
...
参考资料: