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

discord.py 2.1.0混合命令无法显示为斜杠命令求助

解决discord.py 2.1.0 hybrid命令不显示斜杠的问题

核心问题

你的代码缺少命令同步逻辑,还引入了与discord.py 2.x原生交互系统冲突的dislash库,导致斜杠命令无法被Discord服务器识别。

解决步骤

  1. 移除冲突依赖
    卸载dislash库,它和discord.py 2.x的原生应用命令系统不兼容:
pip uninstall dislash

同时删除代码里的from dislash import InteractionClient, SelectMenu, SelectOption这一行。

  1. 添加命令同步逻辑
    discord.py 2.x的hybrid命令必须手动同步到Discord服务器,在机器人就绪事件中加入同步代码:
@bot.event
async def on_ready():
    print(f"Logged in as {bot.user}")
    # 同步指定服务器的命令
    try:
        synced = await bot.tree.sync(guild=discord.Object(id=1000794475683123362))
        print(f"Synced {len(synced)} commands to the guild.")
    except Exception as e:
        print(e)
  1. 检查机器人权限
    确保机器人在目标服务器拥有应用程序命令权限(路径:服务器设置→角色→机器人角色,开启该权限)。

  2. 完整修改后的代码

import sys
import discord
from discord import app_commands
from discord.ext import commands, tasks
import os
import json
from discord.ext.commands import Bot
import random
from random import randint
import datetime
import traceback
import urllib.request, json
import urllib
from dotenv import load_dotenv
from discord.ext.commands import clean_content
from discord.ext.commands.cooldowns import BucketType
from PIL import Image,ImageFont,ImageDraw
from easy_pil import Editor, load_image_async, Font


bot = commands.Bot(command_prefix=["!","?",","],intents=discord.Intents.all())
botcolor = 0xffffff

@bot.hybrid_command(name='test',description='TEST')
@app_commands.guilds(discord.Object(1000794475683123362))
async def test(ctx):
    await ctx.send("Test!")

@bot.event
async def on_ready():
    print(f"已登录为 {bot.user}")
    try:
        synced = await bot.tree.sync(guild=discord.Object(id=1000794475683123362))
        print(f"已同步 {len(synced)} 条命令到服务器")
    except Exception as e:
        print(f"同步命令出错: {e}")

bot.run("你的机器人Token")

额外注意

  • 命令只需同步一次,修改命令内容时再重新同步即可
  • 同步后若未立刻显示,可重启机器人或等待几分钟(Discord存在缓存延迟)
  • 确认服务器ID输入正确,无数字错误

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 17:35:33