【发布时间】:2021-10-03 06:14:04
【问题描述】:
这是我第一次在这里发帖,如果有什么遗漏,我很抱歉。到目前为止,我已经设置了三个命令(加入、播放、离开)以使其正常工作。我花了几个小时来解决这个机器人的问题,因为它没有接受任何命令。任何帮助,将不胜感激!已经谢谢了。
所以每当有人给机器人一个命令时,我都会收到这个错误:
忽略命令 None 中的异常: discord.ext.commands.errors.CommandNotFound:找不到命令“join”
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("Not in 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 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)
def setup(client):
client.add_cog(music(client))
这是我的主文件:
import discord
from discord.ext import commands
import music
cogs = [music]
client = commands.Bot(command_prefix = '!', intents = discord.Intents.all())
for i in range(len(cogs)):
cogs[i].setup(client)
client.run("*my bot here*")
【问题讨论】:
-
您是否在主文件中正确注册了您的 cog?
-
我想是的。我会在帖子中添加我的主文件。
-
您需要使用
load_extension加载齿轮。我已经发布了一个答案,其中包含一些应该适合您的代码。
标签: discord discord.py