Sathvik K S
Sathvik K S

Reputation: 119

How to make the discord bot repeat the song playback?

I want to make a discord bot to repeat the song playback. Assuming the variable count contains the number of times I want times I want the song to repeat, how do I make this command voice.play(discord.FFmpegPCMAudio(audio)) repeat for n times, making sure that each repetition occurs only after the song is played completely?

Upvotes: 0

Views: 4962

Answers (1)

MrSpaar
MrSpaar

Reputation: 3994

VoiceClient.play() has a after argument that you can use to play again audio:

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: There is no error management in this code, you'll have to do your own.

Upvotes: 1

Related Questions