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

Discord Slash命令报错:找不到discord_slash及相关模块

解决Discord机器人Slash命令模块找不到的问题

核心问题本质

ModuleNotFoundError的根源是安装的库和代码导入的模块路径不匹配,不同Slash命令库的包名、导入规则完全不同,盲目安装只会导致更多冲突。

分场景解决方案

场景1:代码依赖旧版discord_slash库

这个库是早期第三方Slash命令实现,现已停止维护,但如果你的代码是基于它编写的,必须安装对应包:

  • 终端执行正确的安装命令:
    pip install discord-py-slash-command
    
    注意:该库的正式包名是discord-py-slash-command,而非代码里的discord_slash模块名,直接搜模块名安装会失败。

场景2:改用官方维护的方案(推荐)

discord.py v2.0+已内置Slash命令支持,无需第三方库,步骤如下:

  1. 先卸载所有冲突的第三方库:
    pip uninstall discord-py-slash-command discord_interactions py-slash-commands -y
    
  2. 安装最新版discord.py:
    pip install -U discord.py
    
  3. 修改代码导入逻辑,替换旧的第三方库导入:
    原代码的from discord_slash import SlashCommand替换为:
    from discord import app_commands
    
    官方Slash命令示例:
    @tree.command(name="hello", description="发送问候")
    async def hello(interaction: discord.Interaction):
        await interaction.response.send_message(f"你好 {interaction.user.mention}!")
    

场景3:代码依赖discord_interactions库

若代码明确基于该库开发,需安装正确的包:

pip install discord-interactions

同时检查代码导入是否匹配:

from discord_interactions import InteractionType, InteractionResponseType

额外排查要点

  • 确认Python环境:如果使用虚拟环境,必须激活后再安装库,避免库装到全局环境但代码运行在虚拟环境中。
  • 验证安装结果:执行pip list,确认目标库出现在列表中,版本符合要求。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 15:48:08