我正在制作一个机器人,它通过选择菜单中的交互滚动,有时代码中断并在终端显示错误。
但这很奇怪,因为我看到三件事发生了:
- 机器人添加了选定的位置,但它出现在不和谐中:"此交互失败">
- 机器人没有给出任务并且"此交互失败"在终端 中出现错误
- 工作完美,没有任何错误。
我的代码:
import discord
from discord.ext import commands
from discord.ui import View, Select
intents = discord.Intents.default()
intents.members = True
intents.message_content = True
bot = commands.Bot(command_prefix='!', intents=intents)
@bot.command()
async def select(ctx):
embed = discord.Embed(title="",
description="Choose your color:n🔴 - Redn🟢 - Greenn🔵 - Blue",
color=discord.Color.orange())
async def select_callback(interaction):
user = interaction.user
r = discord.utils.get(ctx.guild.roles, name="Vermelho")
g = discord.utils.get(ctx.guild.roles, name="Verde")
b = discord.utils.get(ctx.guild.roles, name="Azul")
await user.remove_roles(r, g, b)
role = discord.utils.get(ctx.guild.roles, name=str(select.values[0]))
await user.add_roles(role)
await interaction.response.defer()
select = Select(
min_values=1,
max_values=1,
placeholder="Choose your color here",
options={discord.SelectOption(emoji="🔴", label="Vermelho", description="Color red"),
discord.SelectOption(emoji="🟢", label="Verde", description="Color green"),
discord.SelectOption(emoji="🔵", label="Azul", description="Color blue"),}
)
select.callback = select_callback
view = View(timeout=None)
view.add_item(select)
await ctx.send(embed=embed, view=view)
@bot.event
async def on_ready():
print("Running")
bot.run('TOKEN')
错误:
ERROR discord.ui.view Ignoring exception in view <View timeout=None children=1> for item <Select placeholder='Choose your color here' min_values=1 max_values=1 disabled=False options={<SelectOption label='Verde' value='Verde' description='Color green' emoji=<PartialEmoji animated=False name='🟢' id=No ne> default=False>, <SelectOption label='Azul' value='Azul' description='Color blue' emoji=<PartialEmoji animated=False name=
'🔵' id=None> default=False>, <SelectOption label='Vermelho' value='Vermelho' description='Color red' emoji=<PartialEmoji animated=False name='🔴' id=None> default=False>}>
Traceback (most recent call last):
File "C:UsersMiguelDesktopProgramaçãoProjetosPythonBotsCosk Botvenvlibsite-packagesdiscorduiview.py", line 425, in _scheduled_task
await item.callback(interaction)
File "c:UsersMiguelDesktopProgramaçãoProjetosPythonBotsCosk Botaaaa.py", line 28, in select_callback
await interaction.response.defer()
File "C:UsersMiguelDesktopProgramaçãoProjetosPythonBotsCosk Botvenvlibsite-packagesdiscordinteractions.py", line 636, in defer
await adapter.create_interaction_response(
File "C:UsersMiguelDesktopProgramaçãoProjetosPythonBotsCosk Botvenvlibsite-packagesdiscordwebhookasync_.py", line 218, in request
raise NotFound(response, data)
discord.errors.NotFound: 404 Not Found (error code: 10062): Unknown interaction
我认为发生这种情况是因为您试图在回调的最后延迟交互。解决这个问题的方法是在回调的最顶部添加defer,像这样
async def select_callback(interaction):
await interaction.response.defer()
...
我注意到的另一件事是,你在延迟后没有向用户发送任何响应。这将在15分钟后引起Discord方面的Interaction Failed
错误,因为您正在发送任何内容。我建议您在完成代码后至少向用户发送一条确认消息。