如何使Discord机器人重复播放歌曲?

时间:2020-08-29 08:23:38

标签: python python-3.x discord.py discord.py-rewrite

我想制作一个不和谐的机器人来重复播放歌曲。假设变量计数包含我想要歌曲重复的次数,如何使该命令voice.play(discord.FFmpegPCMAudio(audio))重复n次,确保每次重复仅在歌曲完全播放之后发生? / p>

1 个答案:

答案 0 :(得分:0)

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:此代码中没有错误管理,您必须自己做。