我正在尝试创建一个音乐机器人。加入语音频道,但不播放频道中的音乐。我已经下载了包括 ffmpeg 在内的所有软件包。
我也检查了两次,但没有发现任何错误。任何人都可以帮助更正我的代码吗?
@client.command(name='join', help='This command makes the bot join the voice channel')
async def join(ctx):
if not ctx.message.author.voice:
await ctx.send("You are not connected to a voice channel")
return
else:
channel = ctx.message.author.voice.channel
await channel.connect()
@client.command(name='remove', help='This command removes an item from the list')
async def remove(ctx, number):
global queue
try:
del (queue[int(number)])
await ctx.send(f'Your queue is now `{queue}!`')
except:
await ctx.send('Your queue is either **empty** or the index is **out of range**')
@client.command(name='play', help='This command plays songs')
async def play(ctx):
global queue
server = ctx.message.guild
voice_channel = server.voice_client
async with ctx.typing():
player = await YTDLSource.from_url(queue[0], loop=client.loop)
voice_channel.play(player, after=lambda e: print('Player error: %s' % e) if e else None)
await ctx.send(f'**Now playing:** {player.title} !')
del (queue[0])
@client.command(name='pause', help='This command pauses the song')
async def pause(ctx):
server = ctx.message.guild
voice_channel = server.voice_client
voice_channel.pause()
@client.command(name='resume', help='This command resumes the song!')
async def resume(ctx):
server = ctx.message.guild
voice_channel = server.voice_client
voice_channel.resume()
@client.command(name='leave', help='This command stops makes the bot leave the voice channel')
async def leave(ctx):
voice_client = ctx.message.guild.voice_client
await voice_client.disconnect()
@client.command(name='stop', help='This command stops the song!')
async def stop(ctx):
server = ctx.message.guild
voice_channel = server.voice_client
voice_channel.stop()
答案 0 :(得分:0)
这对我有用:
@bot.command()
async def play(ctx, url):
voice_client = bot.voice_clients[0]
ytdl = youtube_dl.YoutubeDL(YTDL_OPTS)
info = ytdl.extract_info(url, download=False)
asrc = discord.FFmpegOpusAudio(info['formats'][0]['url'], before_options="-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5")
voice_client.play(asrc)
确保您拥有最新版本的 Youtube-dl 和其他软件包。