C
C
CrazyMolecule2021-02-22 13:46:42
Python
CrazyMolecule, 2021-02-22 13:46:42

Discord Music Bot - Queue (discord.py)?

Hello, I need help implementing a queue in a music bot.

At the moment, the queue only works properly when there is one song in it. If there are more songs, then “recursion” begins (after = await serverQueue(voice, message) from def play and await play(queue.pop(0), voice, message) from queue), and all songs are simply skipped.

import discord
    import nacl
    import ffmpeg
    from discord import FFmpegPCMAudio
    from discord.utils import get
    from youtube_dl import YoutubeDL
    
    client = discord.Client()
    
    async def join(message):
        ##Подключение к каналу
        connection = message.author.guild.voice_client
        idUserChannel = message.author.voice.channel.id
        idBotChannel = 0
        if connection:
            idBotChannel = client.voice_clients[0].channel.id
        if (connection) and (idBotChannel != idUserChannel) :
            await message.channel.send('**Перемещение в** ' + str(message.author.voice.channel))
            await connection.move_to(message.author.voice.channel)
        elif (idBotChannel != idUserChannel):
            await message.channel.send('**Подключение к** ' + str(message.author.voice.channel))
            await message.author.voice.channel.connect()
            
    async def play(video_link, voice, message):
        ##Воспроизведение песенок
        ydl_opts = {'format': 'bestaudio', 'noplaylist':'True'}
        FFMPEG_OPTIONS = {'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5', 'options': '-vn'}
        with YoutubeDL(ydl_opts) as ydl:
            info = ydl.extract_info(video_link, download = False)
        print(info.get('title'))
        URL = info['formats'][0]['url']
        print(URL)
        voice.play(FFmpegPCMAudio(URL, **FFMPEG_OPTIONS), after = await serverQueue(voice, message))
        voice.is_playing()
        await message.channel.send('**Сейчас играет** - ' + info.get('title'))
    
    async def skip(voice, message):
        ##Пропуск
        voice.stop()
        await serverQueue(voice, message)
        await message.channel.send('**Успешно пропущено** - ' + info.get('title'))
        
    ##Очередь
    queue = []
    async def serverQueue(voice, message):
        if queue != [] and not voice.is_playing():
            await play(queue.pop(0), voice, message)
            print('Очередь - ' + str(queue))
    
    @client.event
    async def on_message(message):
        if message.author == client.user:
            return
            
        if message.content.startswith('&' + 'join'):
            await join(message)
            
        ##Подключение и проигрывание
        if message.content.startswith('&' + 'play'):
            await join(message)
            voice = get(client.voice_clients, guild = message.channel.guild)
            msg = message.content[1:].split()
            video_link = msg[1]
            if not voice.is_playing():
                await play(video_link, voice, message)
            else:
                await message.channel.send('Успешно добавлено в очередь')
                queue.append(video_link)
    
        ##Пропуск
        if message.content.startswith('&' + 'skip'):
            voice = get(client.voice_clients, guild = message.channel.guild)
            await skip(voice, message)


I tried to get around this in various ways, for example, introducing a second variable, but it did not help. I will be grateful for any help.

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