我正在创建播放音频的不和谐机器人,但我得到了这个错误"discord.ext.commands.errors. commandnotfound: Command "join"未找到">
这里是我的代码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("Et ole puhelussa vitun apina!")
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()
FNPEG_OPTIONS = {'before_optopms': '-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,**FNPEG_OPTIONS)
vc.play(source)
@commands.command()
async def pause(self, ctx):
await ctx.voice_client.pause()
await ctx.send("MUSIIKKI PYSÄYTETTY")
@commands.command()
async def resume(self, ctx):
await ctx.voice_client.resume()
await ctx.send("MUSIIKKI JATKUU")
def setup(client):
client.add_cog(music(client))
run.py
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("MTA0ODUzNjcyMjY3NTMzNTE3OA.G4CK62.BwAK0qKYvuOYU_tm7-cVNCctL4RnnSDtIHmfyc")
我尝试命令,但他们不工作,它只说"不一致。ext.commands.errors. commandnotfound:命令"命令i was trying"未找到">
在discord.py 2中,add_cog方法已成为async function
,因此您需要将其await
。如果您使用来自其他文件的cog,建议使用load_extension来加载它。例如:
齿轮/music.py
class Music(commands.Cog):
...
# as of discord.py 2, this function needs to be an async function
async def setup(bot):
await bot.add_cog(Music(bot))
main.py
# subclass the bot to override the "setup_hook" method
class MyBot(commands.Bot):
async def setup_hook(self):
cogs_to_load = ("cogs.music",) # tuple of paths to the cogs you wanted to load
# use "load_extension" to load all the cogs
for cog in cogs_to_load:
await self.load_extension(cog)
bot = MyBot()
...
这是一个扩展的例子,这是一个齿轮的例子。