@CrazyMolecule

Discord музыкальный бот — Очередь (discord.py)?

Здравствуйте, мне нужна помощь с реализацией очереди в музыкальном боте.

На данный момент очередь нормально работает только тогда, когда в ней одна песня. Если песен больше, то начинается «рекурсия» (after = await serverQueue(voice, message) из def play и await play(queue.pop(0), voice, message) из queue), и все песни просто пропускаются.

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)


Пробовал обойти это разными способами, например, вводил вторую переменную, но это не помогло. Буду благодарен любой помощи.
  • Вопрос задан
  • 735 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы