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

Discord音乐机器人无代码修改触发CommandNotFound命令无法识别问题

问题原因与修复方案

你的报错核心是Cog中的命令没有被正确注册,存在两个致命语法错误,外加版本兼容问题:

  • 错误1:装饰器使用错误:你代码中写的@command.command()不存在对应变量,Cog内的命令应该使用@commands.command()装饰器
  • 错误2:缩进错误:所有命令函数(join、dc、p、pause、resume)都没有缩进在music类内部,不属于Cog的类方法,自然不会被注册到机器人中
  • 版本兼容问题:如果运行环境近期自动更新了discord.py到2.x版本,旧的同步Cog加载逻辑会失效,需要适配异步加载规则

修复后的音乐Cog代码

import discord
from discord.ext import commands
import youtube_dl

class music(commands.Cog):
  def __init__(self, client):
    self.client = client

  # 修正装饰器,且缩进调整到类内部
  @commands.command()
  async def join(self, ctx):
      await ctx.send("X")
      if ctx.author.voice is None: 
          await ctx.send("X")
      voice_channel = ctx.author.voice.channel
      if ctx.voice_client is None: 
          await voice_channel.connect()
      else:
          await ctx.voice_client.move_to(voice_channel)

  @commands.command()
  async def dc(self,ctx):
      await ctx.voice_client.disconnect()
      await ctx.send("X")

  @commands.command()
  async def p(self,ctx,url):
      ctx.voice_client.stop()
      FFMPEG_OPTIONS = {'before_options': '-reconnect 1 -reconnect_streamed 1   -reconnect_delay_max 5', 'options': '-vn'}
      YDL_OPTIONS ={'format':"bestaudio"}
      vc = ctx.voice_client

      with youtube_dl.YoutubeDL(YDL_OPTIONS) as ydl:
          info = ydl.extract_info(url, download=False)
          url2 = info['formats'][0]['url']
          source = await discord.FFmpegOpusAudio.from_probe(url2,**FFMPEG_OPTIONS)
          vc.play(source)

  @commands.command()
  async def pause(self,ctx):
      await ctx.send("x") 
      await ctx.voice_client.pause()

  @commands.command()
  async def resume(self,ctx):
      await ctx.send("x")
      await ctx.voice_client.resume() 

# discord.py 2.x版本需要将setup改为异步函数,add_cog前加await
async def setup(client):
  await client.add_cog(music(client))

主文件适配修改(discord.py 2.x版本必须修改)

删除原来的Cog加载逻辑和client.run(token)代码,替换为异步加载逻辑:

import discord
from discord.ext import commands
from discord.ext import  tasks
from discord.voice_client import VoiceClient
from random import choice
import music
import comments
from replit import db
from keepalive import keepalive
import asyncio

cogs=[music]
intents = discord.Intents.all()
client = commands.Bot(command_prefix='?', intents = intents, case_insensitive=True)

@client.command(name='ping', help='.')
async def ping(ctx):
    await ctx.send(f'Ping-pong: {round(client.latency * 1000)}ms')

status = ['X']

@client.event
async def on_ready():
    change_status.start()
    print('X')

@tasks.loop(seconds=20)
async def change_status():
    await client.change_presence(activity=discord.Game(choice(status)))

# 新增异步加载Cog逻辑
async def load_cogs():
    for cog in cogs:
        await cog.setup(client)

async def main():
    async with client:
        await load_cogs()
        token = ""
        with open("toke.txt") as file:
            token = file.read()
        keepalive()
        await client.start(token)

asyncio.run(main())

额外验证点

  • 确认Discord开发者后台的机器人配置页已开启Message Content Intent权限,discord.py 2.x版本需要该权限才能读取消息内容识别命令
  • 确认输入命令时使用了正确的前缀?,格式为?join
  • youtube_dl已停止维护,长期使用建议替换为yt-dlp避免链接解析失败问题

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.30 18:15:04