Discord.py随机表情包命令开发遇404错误求助
Discord.py 随机表情包命令触发Unknown Webhook错误的解决办法
你在开发Discord.py的随机表情包命令时,两种实现方式都触发了相同错误:
discord.app_commands.errors.CommandInvokeError: Command 'test' raised an exception: NotFound: 404 Not Found (error code: 10015): Unknown Webhook
你的两段命令代码:
async def meme(interaction: discord.Interaction): content = requests.get("https://meme-api.com/gimme").text data = json.loads(content,) meme = discord.Embed(title=f"{data['title']}") await interaction.followup.send(embed=meme)
和
@bot.tree.command() async def test(interaction: discord.Interaction): embed = discord.Embed(title='Meme', description=None) async with aiohttp.ClientSession() as cs: async with cs.get('https://www.reddit.com/r/wholesomememes/new.json?sort=hot') as r: res = await r.json() embed.set_image(url=res['data']['children'] [random.randint(0, 25)]['data']['url']) await interaction.followup.send(embed=embed, content=None)
错误原因
问题出在你直接调用了interaction.followup.send(),但没有先对初始交互发送响应。Discord要求Slash命令必须在3秒内完成初始响应(比如发送确认消息或延迟通知),否则后续的followup请求会找不到对应的Webhook会话,从而触发404错误。
修正方案
给两段代码都加上初始响应逻辑即可解决:
第一段代码修正版
async def meme(interaction: discord.Interaction): # 先发送延迟响应,告知Discord命令正在处理 await interaction.response.defer() # 建议替换同步requests为异步aiohttp,避免阻塞事件循环 async with aiohttp.ClientSession() as cs: async with cs.get("https://meme-api.com/gimme") as r: data = await r.json() meme = discord.Embed(title=f"{data['title']}") await interaction.followup.send(embed=meme)
第二段代码修正版
@bot.tree.command() async def test(interaction: discord.Interaction): # 先发送延迟响应 await interaction.response.defer() embed = discord.Embed(title='Meme') async with aiohttp.ClientSession() as cs: async with cs.get('https://www.reddit.com/r/wholesomememes/new.json?sort=hot') as r: res = await r.json() embed.set_image(url=res['data']['children'][random.randint(0, 25)]['data']['url']) await interaction.followup.send(embed=embed)
额外提示
- 如果你的命令能在3秒内完成处理,也可以直接用
await interaction.response.send_message()发送最终结果,无需使用followup - 务必确保初始响应(
defer()或send_message())在任何耗时操作(比如网络请求)之前执行,避免超过Discord的3秒响应超时限制
内容的提问来源于stack exchange,提问作者Fraser
相关产品推荐
相关产品推荐

