M
M
Michal Palych2020-09-29 12:04:43
Python
Michal Palych, 2020-09-29 12:04:43

How to make music play in a circle?

Good afternoon everyone! Is there any way to make the music play in a circle? Just constantly typing /p [link_to_video] is not very convenient. I would like to loop the song. How to implement it?
I am attaching the source code for standard playback:

@Bot.command(aliases = ['p', 'PLAY'])
async def play(ctx, url):
    global voice

    channel = ctx.message.author.voice.channel	
    voice = get(Bot.voice_clients, guild = ctx.guild)	

    song_there = os.path.isfile('song.mp3')

    if voice and voice.is_connected():	
        await voice.move_to(channel)	
    else:	
        voice = await channel.connect()
    try:
        if song_there:
            os.remove('song.mp3')
            print('[logs] Старый файл успешно удалён')
    except PermissionError: 
        print('[logs] Не удалось удалить файл. Неизвестная причина...')

    voice = get(Bot.voice_clients, guild = ctx.guild)
    ydl_opts = {
        'format' : 'bestaudio/best',
        'postprocessors' : [{
            'key' : 'FFmpegExtractAudio',
            'preferredcodec' : 'mp3',
            'preferredquality' : '320'
        }],
    }

    with youtube_dl.YoutubeDL(ydl_opts) as ydl:     
        print ('[logs] Начинаю загрузку музыки...')
        embed = discord.Embed(description = '*Жди - готовлю к воспроизведению твой трек...*')
        await ctx.send(embed = embed)
        ydl.download([url])

    for file in os.listdir('./'):
        if file.endswith('.mp3'):
            name = file
            os.rename(file, 'song.mp3')
    voice.play(discord.FFmpegPCMAudio('song.mp3'), after = lambda e: print(f'{name}, музыка закончила своё проигрывание'))
    nname = name.rsplit('-', maxsplit = 1)

    embed = discord.Embed(description = f' __Сейчас играет:__ **{nname[0]}**', color = 0x428325)
    embed.set_footer(text = "supports by quantprod")
    await ctx.send(embed = embed)

    await Bot.join_voice_channel(channel)

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question