Discord.py 按钮响应交互在一段时间后失败

2023-12-26

我有一个非常基本的脚本,它会弹出一条消息,其中包含带有命令 ?place 的按钮

单击此按钮后,机器人会向单击它的用户回复“嗨”。

如果按钮没有交互超过大约 3 分钟,它就会开始返回“交互失败”。

之后按钮就变得毫无用处。我假设有某种内部超时,我在文档中找不到。无论使用discord.py (2.0) 还是pycord,该按钮都会执行相同的操作。控制台没有任何反应。就好像按钮点击没有被拾取一样。

偶尔该按钮会再次开始工作,控制台上会出现大量以下错误:

discord.errors.NotFound: 404 Not Found (error code: 10062): Unknown interaction
Ignoring exception in view <View timeout=180.0 children=1> for item <Button style=<ButtonStyle.success: 3> url=None disabled=False label='click me' emoji=None row=None>:

我认为超时 = 180 是此问题的原因,但有人知道如何停止此超时以及为什么会发生这种情况吗?我在文档中看不到任何关于不和谐按钮只能使用 3 分钟的内容。

import discord

from discord.ext import commands
intents = discord.Intents.default()
intents.members = True
intents.message_content = True
bot = commands.Bot(command_prefix="?", intents=intents)


embed1=discord.Embed(title="Test", description = f"TESTING",color=0xffffff)   
print("bot connected")
 
@ bot.command(name='place')
async def hello(ctx):
    view = discord.ui.View()
    buttonSign = discord.ui.Button(label = "click me", style= discord.ButtonStyle.green)


    async def buttonSign_callback(interaction):
        userName = interaction.user.id
        embedText = f"test test test"
        embed=discord.Embed(title="Test", description = embedText,color=0xffffff)
        await interaction.response.send_message(f"Hi <@{userName}>")

       

    buttonSign.callback = buttonSign_callback
    view.add_item(item=buttonSign)
    await ctx.send(embed = embed1,view = view)

bot.run(TOKEN)


解释

默认情况下,ViewDiscord.py 2.0 中的超时时间为 180 秒(3 分钟)。您可以通过传入来修复此错误None作为创建视图时的超时。

Code

@bot.command(name='place')
async def hello(ctx):
    view = discord.ui.View(timeout=None)

参考

discord.ui.View.timeout https://discordpy.readthedocs.io/en/latest/interactions/api.html?highlight=view#discord.ui.View.timeout

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Discord.py 按钮响应交互在一段时间后失败 的相关文章

随机推荐