我试图编码一个不和谐机器人,但我甚至不能得到命令的工作。使用replit.com作为IDE并运行bot。
这是main.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("token")
这是bot的代码:
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("join 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, **FFMPEG_OPTIONS)
vc.play(source)
@commands.command()
async def pause(self,ctx):
await ctx.voice_client.pause()
await ctx.send("pause")
@commands.command()
async def resume(self,ctx):
await ctx.voice_client.resume()
await ctx.send("resume")
@commands.command()
async def test(self,ctx):
await ctx.send("test!")
def setup(client):
client.add_cog(music(client))
它运行没有错误,但如果我尝试使用?test或任何其他命令,它会给我这个错误:
Ignoring exception in command None:
discord.ext.commands.errors.CommandNotFound: Command "test" is not found
我看到其他用户有相同的问题,但是,我不能找到解决它。
我对Python只有基本的经验,对Discord机器人没有任何经验,所以这可能是我犯的一些愚蠢的错误。
感谢您的宝贵时间!正如@TheFungusAmongUs指出的,你的缩进是关闭的。您的测试函数,以及music
类下的所有其他函数,应该与__init__()
处于相同的缩进级别。
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("join 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, **FFMPEG_OPTIONS)
vc.play(source)
@commands.command()
async def pause(self,ctx):
await ctx.voice_client.pause()
await ctx.send("pause")
@commands.command()
async def resume(self,ctx):
await ctx.voice_client.resume()
await ctx.send("resume")
@commands.command()
async def test(self,ctx):
await ctx.send("test!")
def setup(client):
client.add_cog(music(client))