我正在尝试使用discord.py 创建一个Discord 音乐机器人,我是Python 新手。我不知道如何让Bot自动播放下一首歌曲。我尝试了很多不同的事情。这是我当前播放一首歌曲的代码:
vc.play(discord.FFmpegPCMAudio(executable="C:/FFmpeg/bin/ffmpeg.exe", source=sound + ".mp3"))
await message.channel.send("Spiele nun " + str(sound) +"weiter")
Run Code Online (Sandbox Code Playgroud)
使用上面的代码我没有遇到问题。
您应该使用函数after的参数FFmpegPCMAudio。该参数接受一个可调用函数作为参数,并在当前歌曲结束时执行它。
song_queue = []
Run Code Online (Sandbox Code Playgroud)
play()功能:source = sound + '.mp3'
soung_queue.append(source)
if not vc.is_playing():
vc.play(discord.FFmpegPCMAudio(source=source, after=lambda e: play_next(ctx))
await ctx.send("Now playing...")
else:
await ctx.send('Song queued')
Run Code Online (Sandbox Code Playgroud)
play_next()函数:source = sound + '.mp3'
soung_queue.append(source)
if not vc.is_playing():
vc.play(discord.FFmpegPCMAudio(source=source, after=lambda e: play_next(ctx))
await ctx.send("Now playing...")
else:
await ctx.send('Song queued')
Run Code Online (Sandbox Code Playgroud)
play_next()您可以随意命名该函数。
如果您希望机器人因播放声音时间太长而断开连接,请将函数更改play_next()为:
import asyncio
def play_next(ctx, source):
if len(self.song_queue) >= 1:
del self.song_queue[0]
vc = get(self.bot.voice_clients, guild=ctx.guild)
vc.play(discord.FFmpegPCMAudio(source=source, after=lambda e: play_next(ctx))
asyncio.run_coroutine_threadsafe(ctx.send("No more songs in queue."), self.bot.loop)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8562 次 |
| 最近记录: |