使用discord_slash开发Discord机器人遇50027错误求助
- 核心原因:discord_slash是已废弃的第三方库,长期未维护,无法适配Discord最新的Webhook验证机制,导致所有使用该库的斜杠命令触发401令牌无效错误。
修复步骤:
- 升级discord.py到最新版本(2.0+版本已内置原生斜杠命令支持)
pip install -U discord.py
- 将原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("你的机器人令牌")
- 测试新命令:重启机器人后,执行/ping命令即可正常响应,不会再触发401错误。
注意:discord_slash库已停止更新,没有官方修复方案,迁移到discord.py原生支持是唯一长期可行的解决办法。
内容的提问来源于stack exchange,提问作者Akopa
相关产品推荐
相关产品推荐

