代码运行良好,直到我单击按钮并给出错误(包含在顶部)。该脚本仍然运行时出现错误,但我需要修复它以用于托管目的。谢谢!
class CollabButton(discord.ui.View):
def __init__(self):
super().__init__(timeout=None)
self.add_item(
discord.ui.Button(label='Accept', style=discord.ButtonStyle.green))
@client.event
async def on_interaction(interaction):
if interaction.type == discord.InteractionType.component:
if is_moderator(interaction.user):
collab_button_view = CollabButton()
if not interaction.response.is_done():
await interaction.response.send_message(
f"Hey {interaction.user.mention}!\nAn admin has accepted the collaboration offer!"
)
else:
if not interaction.response.is_done():
await interaction.response.send_message(
"You do not have permission to use this button.", ephemeral=True)
def is_moderator(user):
#Role name can be changed to anything
return any(role.name == 'Cadly' for role in user.roles)
我尝试添加 if 语句来验证响应是否完成,但我已经没有想法了。任何见解都有帮助!
发生此问题是因为您没有在
on_interaction
事件中过滤所需的交互。因此,此事件也会接收其他组件中处理的交互。
强烈建议您为要在此事件中处理的组件定义一个
custom_id
,并仅过滤由此产生的交互:
class CollabButton(discord.ui.View):
def __init__(self):
super().__init__(timeout=None)
self.add_item(
discord.ui.Button(label='Accept', style=discord.ButtonStyle.green, custom_id="collab_button"))
@client.event
async def on_interaction(interaction):
item_id = interaction.data.get("custom_id")
if item_id == "collab_button":
if is_moderator(interaction.user):
if not interaction.response.is_done():
await interaction.response.send_message(
f"Hey {interaction.user.mention}!\nAn admin has accepted the collaboration offer!"
)
else:
if not interaction.response.is_done():
await interaction.response.send_message(
"You do not have permission to use this button.", ephemeral=True)