我在下面的代码中得到"This Interaction Failure":

class SimpleView(discord.ui.View):
@discord.ui.button(label="✔️", style=discord.ButtonStyle.success)
async def approve(self, interaction: discord.Interaction, button: discord.ui.Button):
    await interaction.response.send_message(content="Approved!", ephemeral=True)

@discord.ui.button(label="❌", style=discord.ButtonStyle.red)
async def deny(self, interaction: discord.Interaction, button: discord.ui.Button):
    await interaction.response.send_message(content="Denied!")

@bot.event
async def on_member_join(member: discord.Member):
 view = SimpleView()

 role_name = discord.utils.get(member.guild.roles, name="Test")

 approve_channel_id = 1158716789103013898
 approve = member.guild.get_channel(approve_channel_id)

 if approve is not None:
    await member.add_roles(role_name)
    await approve.send(f"{member} joined the server! Do you Approve?", view=view)
    await view.wait()
 else:
    print(f"Channel with ID {approve_channel_id} not found!")

我试图做的是,当某人加入服务器时,他将获得一个特定的角色,并在仅限管理员的通道中使用两个按钮发送消息,一个按钮接受用户在服务器中,另一个按钮拒绝.但是,当我按下一个按钮时,我得到"This Interaction Failure",并且控制台中没有任何错误.

推荐答案

我认为您这里有一个超时错误.

class SimpleView(discord.ui.View):
    def __init__(self):
        super().__init__(timeout = None)

    @discord.ui.button(label="✔️", style=discord.ButtonStyle.green)
    async def approve(self, interaction: discord.Interaction, button: discord.ui.Button,custom_id="accept"):
        await interaction.response.send_message(content="Approved!", ephemeral=True)

    @discord.ui.button(label="❌", style=discord.ButtonStyle.red)
    async def deny(self, interaction: discord.Interaction, button: discord.ui.Button,custom_id="denied"):
        await interaction.response.send_message(content="Denied!")

此外,如果你愿意,你还可以让你的按钮在机器人重启后也能响应:

在您的on_ready()功能添加bot.add_view(SimpleView()),没有任何等待.

@bot.event
async def on_ready():
    bot.add_view(SimpleView())

Python相关问答推荐

使用Beautiful Soup获取第二个srcset属性

如何将带有逗号分隔的数字的字符串解析为int Array?

指示组内的rejected_time是否在creation_timestamp后5分钟内

剧作家Python没有得到回应

如何在Deliveryter笔记本中从同步上下文正确地安排和等待Delivercio代码中的结果?

将DF中的名称与另一DF拆分并匹配并返回匹配的公司

将整组数组拆分为最小值与最大值之和的子数组

在Python中处理大量CSV文件中的数据

Gekko:Spring-Mass系统的参数识别

抓取rotowire MLB球员新闻并使用Python形成表格

. str.替换pandas.series的方法未按预期工作

OR—Tools中CP—SAT求解器的IntVar设置值

如何创建一个缓冲区周围的一行与manim?

SQLAlchemy bindparam在mssql上失败(但在mysql上工作)

Polars asof在下一个可用日期加入

Geopandas未返回正确的缓冲区(单位:米)

交替字符串位置的正则表达式

python sklearn ValueError:使用序列设置数组元素

使用tqdm的进度条

用0填充没有覆盖范围的垃圾箱