You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Pycord:普通命令转Slash Command时遇Unknown interaction错误

问题描述

我原本有一段可正常运行的Pycord普通命令代码,通过animec库在MAL搜索动漫并发送Embed消息。尝试将其转换为Slash Command后无响应,执行时触发NotFound错误(错误码10062:Unknown interaction)。

原代码

import discord
from discord.ext import commands
from discord import Intents
import animec

intents = Intents.all()
intents.members = True

bot = commands.Bot(command_prefix='$', intents=intents)

@bot.command()
async def anime(ctx, *, query):

    anime = animec.Anime(query)

    embedanime = discord.Embed(title= anime.title_english, url= anime.url, description= f"{anime.description[:1000]}...", color=0x774dea)
    await ctx.send(embed=embedanime)

bot.run(TOKEN)

改写后的Slash Command代码

import discord
from discord.ext import commands
from discord import Intents, Option
import animec


intents = Intents.all()
intents.members = True

bot = commands.Bot(command_prefix='$', intents=intents)

@bot.slash_command(name="anime", description="Search for an anime on MAL", guild=discord.Object(id=824342611774144543))
async def anime(interaction: discord.Interaction, *, search: Option(str, description="What anime you want to search for?", required=True)):

    anime = animec.Anime(search)

    embedanime = discord.Embed(title= anime.title_english, url= anime.url, description= f"{anime.description[:1000]}...", color=0x774dea)
    await interaction.response.send_message(embed=embedanime)


bot.run(TOKEN)

报错信息

Application Command raised an exception:
NotFound: 404 Not Found (error code: 10062):
Unknown interaction
问题原因与解决办法

错误码10062(Unknown interaction)核心是Discord无法识别你的交互请求,大多是响应超时或命令注册异常导致,以下是具体解决步骤:

1. 解决响应超时问题

animec.Anime(search)是同步网络请求,会卡住机器人的事件循环,而Discord要求Slash命令必须在3秒内返回响应,超时后就会触发Unknown interaction。

两种修复方式:

方式一:先发送临时响应再编辑结果

@bot.slash_command(name="anime", description="Search for an anime on MAL", guild=discord.Object(id=824342611774144543))
async def anime(interaction: discord.Interaction, *, search: Option(str, description="What anime you want to search for?", required=True)):
    # 先发送临时回应,告诉Discord我们正在处理
    await interaction.response.defer()
    
    # 执行耗时的搜索操作
    anime = animec.Anime(search)
    
    embedanime = discord.Embed(title=anime.title_english, url=anime.url, description=f"{anime.description[:1000]}...", color=0x774dea)
    # 编辑临时响应,替换成最终的Embed
    await interaction.edit_original_response(embed=embedanime)

方式二:用异步线程包装同步操作

import asyncio

@bot.slash_command(name="anime", description="Search for an anime on MAL", guild=discord.Object(id=824342611774144543))
async def anime(interaction: discord.Interaction, *, search: Option(str, description="What anime you want to search for?", required=True)):
    # 把同步的搜索操作放到线程池执行,不阻塞事件循环
    anime = await asyncio.to_thread(animec.Anime, search)
    
    embedanime = discord.Embed(title=anime.title_english, url=anime.url, description=f"{anime.description[:1000]}...", color=0x774dea)
    await interaction.response.send_message(embed=embedanime)

2. 检查命令注册与权限

  • 确认guild参数里的服务器ID正确,且机器人在该服务器中拥有应用命令权限(在Discord开发者门户的OAuth2设置里,勾选applications.commands权限后重新邀请机器人)。
  • 如果用全局命令,去掉guild参数,但全局命令需要约1小时同步时间,测试阶段优先用服务器专属命令。

3. 验证Intents配置

确保Discord开发者门户中,机器人的Intents开关和代码里的设置一致,开启所有必要的Intents(尤其是message_content,虽然Slash命令不依赖它,但完整配置能避免其他潜在问题)。

内容的提问来源于stack exchange,提问作者Hic

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 01:35:26