如何让 discord bot 重复播放歌曲?

How to make the discord bot repeat the song playback?

我想制作一个不和谐的机器人来重复播放歌曲。假设变量 count 包含我想要歌曲重复的次数,我如何让这个命令 voice.play(discord.FFmpegPCMAudio(audio)) 重复 n 次,确保每次重复只在歌曲播放完后才发生?

VoiceClient.play() 有一个 after 参数,您可以使用它再次播放音频:

from discord.ext import commands
from asyncio import run_coroutine_threadsafe as rct

bot = commands.Bot(prefix='your_prefix')

def play_next(ctx, audio, msg, n):
    if n:
        voice = get(bot.voice_clients, guild=ctx.guild)
        rct(msg.edit(content='Finished playing the song, {n} more to go.'), bot.loop)
        voice.play(FFmpegPCMAudio(audio), after=lambda e: play_next(ctx, audio, msg, n-1))
        voice.is_playing()
    else:
        rct(msg.delete())

@bot.command()
repeat(ctx, n):
    audio = 'your_audio_source'
    voice = get(bot.voice_clients, guild=ctx.guild)

    msg = await ctx.send(f'Started playing video {n} times')
    voice.play(FFmpegPCMAudio(audio), after=lambda e: play_next(ctx, audio, msg, n-1))
    voice.is_playing()

bot.run('your_token')

PS: 此代码中没有错误管理,您必须自己做。