忽略命令中的异常 None: discord.ext.command.errors.CommandNotFound: 找不到命令"play"。这发生在 Python 中



我正试图制作一个类似rythm的机器人,但它给了我这个错误正在忽略命令"无"中的异常:discord.ext.commands.errors.CommandNotFound:命令"播放";未找到这是代码:

music.py:

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):
if ctx.author.voice is None:
await ctx.send("You're not in a voice channel!")
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 disconnect(self,ctx):
await ctx.voice.client.disconnect()
@commands.command()
async def play(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, **FFMPEGOP_OPTIONS)
vc.play(source)

def setup(client):
client.add_cog(music(client))

如果您在music.py文件中设置与音乐相关的命令,我假设您还有一个main.py文件,在该文件中运行bot令牌等。

我的设置方式是将cog从单独的命令文件导入main.py文件。

而不是在音乐中执行这个。py文件:

def setup(client):
client.add_cog(music(client))

试着把它放在main.py文件中:

from music import music
client.add_cog(music(client))

(注意,在上面粘贴的代码中,def __init__中只有一个下划线_,应该是两个(。

在discord.py中命名/使用你的齿轮也是一种很好的做法,比如:

音乐.py

class MusicCog(commands.Cog):
def __init__(self, client):
self.client = client
@commands.command()
# [. . .]

main.py

from music import MusicCog
client.add_cog(MusicCog(client))

正如你原来帖子上的评论所说,缩进确实与你粘贴代码的方式不同。play命令似乎是在disconnect命令中定义的。

不过,另一方面,如果你在cogs中遇到命令问题(尽管它对我有效,但我见过其他几个人遇到了奇怪的问题(,你可以简单地用这种方式定义你的命令:

client.command()
async def disconnect(self, ctx):
await ctx.voice.client.disconnect()
client.command()
async def play(self, ctx, url):
# [. . .]

不过,我个人确实建议在cogs中使用命令。它可以更容易地使用单独的文件,并使代码更有条理。

我希望这能帮助任何遇到这个问题或类似问题的人。

最新更新