提问者:小点点

Python不和谐音乐机器人-创建队列-自动播放下一首歌


我正在用python制作一个不和谐音乐机器人。它已经可以完成下载、播放音乐、暂停、恢复、停止等基础工作。

现在我正在尝试排队。所以机器人会自动播放下一首歌,而无需我再次手动告诉他播放一些音乐。它已经创建了一个列表作为歌曲队列,但是…

我的问题:我的机器人不知道歌曲什么时候结束播放下一首歌;没有事件让机器人播放下一首歌。也许有人知道合适的命令或者我如何解决这个问题?我通常使用不和谐中的事件(“on_ready”,“on_message”,“on_error”,…),但我不知道适合队列的事件。

import discord
from discord.ext import commands
import os
import bunchi_commands
import youtube_dl

#intents
intents = discord.Intents.default()
intents.members = True
intents.presences = True

#variabeln
queue = []


#implementation
class MyClient(discord.Client):
    #-------------------------------------------------
    async def on_message(self, message): 
        prefix = "~"
        if message.author != self.user:
            msg = message.content.split()

            voicechannel = message.author.voice.channel
            voiceclient = message.guild.voice_client

        #-------------(voice-commands)--------------------
            elif msg[0] == prefix+"join":
                if voiceclient and voiceclient.is_connected():
                    await voiceclient.move_to(voicechannel)
                else:
                    await voicechannel.connect()

            elif msg[0] == prefix+"dc":         
                if voiceclient.is_connected():
                    await voiceclient.disconnect()

            elif msg[0] == prefix+"play":
                if voiceclient and voiceclient.is_connected():      # just connecting to the voicechannel and voiceclient
                    await voiceclient.move_to(voicechannel)         # 
                else:                                               #  
                    await voicechannel.connect()                    # 

                
                url = str(message.content)[len(msg[0])+1:]          
                dirpath = './downloadsong'                          
                filename = 'song.mp3'                           
                filepath = '{}/{}'.format(dirpath,filename)

                if queue == []:                                      # if queue is empty (=not playing a song right now)
                    bunchi_commands.check_song(filepath)                # delete old song
                    bunchi_commands.download_song(url,filepath)         # download new song

                    voiceclient.play(discord.FFmpegPCMAudio("./downloadsong/song.mp3")) # play song

                bunchi_commands.addqueue(queue,url)
                print(queue)
                            
            elif msg[0] == prefix+"pause":
                if voiceclient.is_playing():
                    voiceclient.pause()

            elif msg[0] == prefix+"resume":
                if voiceclient.is_paused():
                    voiceclient.resume()
            
            elif msg[0] == prefix+"stop":
                voiceclient.stop()

    
#---------------------------------------------------
client = MyClient(intents=intents)
client.run('Token')

共0个答案