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

使用discord_slash开发Discord机器人遇50027错误求助

解决discord_slash库斜杠命令401 Unauthorized错误
  • 核心原因:discord_slash是已废弃的第三方库,长期未维护,无法适配Discord最新的Webhook验证机制,导致所有使用该库的斜杠命令触发401令牌无效错误。

修复步骤:

  1. 升级discord.py到最新版本(2.0+版本已内置原生斜杠命令支持)
pip install -U discord.py
  1. 将原discord_slash的代码替换为discord.py原生斜杠命令写法,以你的ping命令为例:
import discord
from discord import app_commands

bot = discord.Bot(intents=discord.Intents.default())

# 启动时同步斜杠命令到Discord服务器
@bot.event
async def on_ready():
    await bot.tree.sync()
    print(f"已登录为 {bot.user}")

# 原生斜杠命令定义
@bot.tree.command(name="ping", description="Check the bots time response")
async def ping(interaction: discord.Interaction):
    embed = discord.Embed(title="Pong.", description=f"**:chicken: `{int(bot.latency * 1000)}` ms**")
    await interaction.response.send_message(embed=embed)

bot.run("你的机器人令牌")
  1. 测试新命令:重启机器人后,执行/ping命令即可正常响应,不会再触发401错误。

注意:discord_slash库已停止更新,没有官方修复方案,迁移到discord.py原生支持是唯一长期可行的解决办法。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.25 17:33:25