
在使用discord.py开发机器人时,交互式按钮(discord.ui.button)提供了一种直观的用户体验。然而,开发者常会遇到两种情况导致按钮失效并返回“this interaction failed”错误:
为了解决这些问题,我们需要采取两种策略:一是延长视图的活跃时间,二是使其在机器人重启后依然能够被识别和处理。
要防止按钮因长时间未交互而超时,我们需要在视图类的初始化方法中,将timeout参数设置为None并传递给父类super().__init__()。
示例代码:
import discord
# 定义一个继承自discord.ui.View的自定义视图类
class PersistentMenu(discord.ui.View):
def __init__(self):
# 将timeout设置为None,表示视图永不超时
super().__init__(timeout=None)
self.value = None
@discord.ui.button(label="脚本", style=discord.ButtonStyle.green, emoji="?")
async def script_button(self, interaction: discord.Interaction, button: discord.ui.Button):
# 按钮点击后的响应,设置为临时消息
await interaction.response.send_message("你好,世界!", ephemeral=True)
# 假设client是你的机器人实例
# client = discord.Client(intents=discord.Intents.default())
# tree = discord.app_commands.CommandTree(client)
# 斜杠命令,用于发送包含按钮的消息
@client.tree.command(name="test_button", description="这是一个带有持久化按钮的测试命令")
async def test_button(interaction: discord.Interaction):
# 权限检查(可选)
if not interaction.user.guild_permissions.administrator:
return await interaction.response.send_message("你不是管理员,无法使用此命令。", ephemeral=True)
else:
# 创建视图实例
view = PersistentMenu()
embed = discord.Embed(title="测试按钮", description="点击下方的按钮进行交互。", color=0xfed9ff)
# 发送包含嵌入消息和视图的消息
await interaction.response.send_message(embed=embed, view=view)
注意事项: 确保timeout=None是传递给super().__init__()的参数,而不是直接在视图类实例创建时传入(如view = Menu(timeout=None),这会导致错误,因为Menu类的__init__方法可能没有定义接收timeout参数)。
仅仅设置timeout=None只能防止活跃会话的超时,但不能解决机器人重启后按钮失效的问题。为了让按钮在机器人重启后依然有效,我们需要在机器人启动时,通过bot.add_view()方法重新注册我们的持久化视图。
关键点:
完整示例代码:
import discord
from discord.ext import commands
# 机器人实例的设置
intents = discord.Intents.default()
intents.message_content = True # 如果需要处理消息内容
bot = commands.Bot(command_prefix="!", intents=intents)
# 定义一个持久化视图类
class MyPersistentView(discord.ui.View):
def __init__(self):
# 设置timeout=None,防止会话超时
super().__init__(timeout=None)
# 定义一个按钮,并设置唯一的custom_id
@discord.ui.button(label="点击测试", style=discord.ButtonStyle.green, custom_id="my_unique_test_button")
async def test_button(self, interaction: discord.Interaction, button: discord.ui.Button):
# 按钮点击后的响应
await interaction.response.send_message("你点击了持久化按钮!", ephemeral=True)
# 机器人启动时执行的事件
@bot.event
async def on_ready():
print(f'机器人已上线:{bot.user}')
# 在机器人启动时,添加持久化视图
# 这样,即使机器人重启,它也能识别并响应带有"my_unique_test_button" custom_id的按钮
bot.add_view(MyPersistentView())
# 同步斜杠命令到Discord
await bot.tree.sync()
print("持久化视图已注册,斜杠命令已同步。")
# 斜杠命令,用于发送包含持久化按钮的消息
@bot.tree.command(name='send_persistent_button', description='发送一个包含持久化按钮的消息')
async def send_persistent_button(interaction: discord.Interaction):
# 发送消息时,传入视图实例
await interaction.response.send_message("这是一个持久化按钮:", view=MyPersistentView())
# 运行机器人
# bot.run("YOUR_BOT_TOKEN") # 请替换为你的机器人Token代码解析:
通过正确配置视图的timeout=None以及在机器人启动时利用bot.add_view()方法重新注册带有custom_id的持久化视图,我们可以有效解决Discord.py交互式按钮在长时间后或机器人重启后失效的问题。这两种策略的结合,确保了机器人按钮功能的稳定性和可靠性,为用户提供了更流畅的交互体验。
以上就是Discord.py持久化按钮:解决交互超时与机器人重启后的失效问题的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号