运行此代码后,我得到这个错误=Ignoring exception in command None: discord.ext.commands.errors.CommandNotFound: Command "join" is not found
我得到这个错误,即使代码已经运行了几个星期没有任何错误,我没有改变任何东西。
我不知道如何解决它,我已经尝试将commands.command()中的命令替换为bot.command(),但它不起作用。
感觉我什么都试过了,但我已经没有希望了。
请多多指教。
import discord
from discord.ext import commands
import youtube_dl
class music(commands.Cog):
def __init__(self, client):
self.client = client
@command.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)
@command.command()
async def dc(self,ctx):
await ctx.voice_client.disconnect()
await ctx.send("X")
@command.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)
@command.command()
async def pause(self,ctx):
await ctx.send("x")
await ctx.voice_client.pause()
@command.command()
async def resume(self,ctx):
await ctx.send("x")
await ctx.voice_client.resume()
def setup(client):
client.add_cog(music(client))
这是我的主文件
的代码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
cogs=[music]
intents = discord.Intents.all()
client = commands.Bot(command_prefix='?', intents = intents, case_insensitive=True)
for i in range(len(cogs)):
cogs[i].setup(client)
@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)))
token = ""
with open("toke.txt") as file:
token = file.read()
keepalive()
client.run(token)
首先你需要缩进一个类中的每个命令1级缩进,其次,它是@commands.command()
而不是@command.command()
。
这是cogs的文档:
https://discordpy.readthedocs.io/en/master/ext/commands/cogs.html?突出=齿轮
编辑:我已经写在我的评论,这是一个例子如何加载齿轮:
主要文件:
import discord
from discord.ext import commands
intents = discord.Intents.all()
client = commands.Bot(command_prefix='?', intents = intents, case_insensitive=True)
cogs = ["music"]
for i in cogs:
client.load_extension(f"cogs.{i}")
with open("token.txt") as file:
token = file.read()
client.run(token)
这里假设music.py文件存储在名为cogs
的文件夹中,如果不是这样,编辑client.load_extension(f"cogs.{i}")
行并将cogs
替换为您的文件夹名称(或者如果文件存储在与main.py相同的文件夹中则删除它)。
然后在音乐文件中:
import discord
from discord.ext import commands
class music(commands.Cog):
def __init__(self, client):
self.client = client
@commands.command()
async def play(self, ctx):
print("test")
def setup(client):
client.add_cog(music(client))