音乐命令。Cog 帮助 (nextcord.py)

问题描述 投票:0回答:1

我已经花了好几个小时了,我很茫然,我尝试的一切都没有成功,我很接近,我知道,我正在尝试一切,但我需要的东西我就是无法破解。我有自己的音乐齿轮: 下面的词典包含播放列表,

{
    "0": {
        "SID": 681262466650734596,
        "djs": [],
        "playlist": [
            "Pantera ~ Walk (2010 Remaster), 315, https://www.youtube.com/watch?v=XNjKN0_wx3s",
            "Pantera ~ 10's (2016 Remaster), 290, https://www.youtube.com/watch?v=3ZRiMcdGhqE"
        ]
    }
}

我试图弄清楚如何在第一首歌曲完成或跳过后播放下一首歌曲,我的跳过命令如下:

@commands.command(aliases=['ff'])
    async def skip(self, ctx):
        voice_client = self.voice_clients.get(ctx.guild.id)
        try:
            if ctx.guild.voice_client:
                ctx.guild.voice_client.stop()
                next_song = PLDetails(ctx.guild.id, 'a')
                if next_song:
                    yl = next_song[2]
                    print(yl)
                    # yl2 = yl[2].strip()
                    # print(yl2)
                

                # player = nextcord.FFmpegOpusAudio(next_song, **self.ffmpeg_options)
                # voice_client.play(player, after=lambda e: asyncio.run_coroutine_threadsafe(self.play_next(ctx), self.client.loop))
                # return 
                
        except Exception as e:
            print(f"Error in skip command: {e}")

一步步实现我的目标: 1. 执行跳过命令后,它会弹出“播放列表”键值中的最上面一行。 第 1 步的结果:

{
    "0": {
        "SID": 681262466650734596,
        "djs": [],
        "playlist": [
            "Pantera ~ Walk (2010 Remaster), 315, https://www.youtube.com/watch?v=XNjKN0_wx3s"
        ]
    }
}

然后,这条线运行:

next_song = PLDetails(ctx.guild.id, 'a')

我想要它做的是读取字典,访问“播放列表”键和值,找到最后一行并将其返回到跳过命令。

  1. 现在,next_song 变量被赋值为:
Pantera ~ 10's (2016 Remaster), 290, https://www.youtube.com/watch?v=3ZRiMcdGhqE

目前,next_song是一个列表,我想做的只是从列表中获取youtube链接。但我尝试的任何方法都不起作用,我收到一个又一个错误,我想要的只是列表中的最后一项,即 YouTube 链接。

4. 现在,一旦获得了 YouTube 链接, next_song = YouTube 链接 这段代码应该运行:

player = nextcord.FFmpegOpusAudio(next_song, **self.ffmpeg_options)
                voice_client.play(player, after=lambda e: asyncio.run_coroutine_threadsafe(self.play_next(ctx), self.client.loop))
                return 

但正如我所说,我无法通过第 3 步。我怎样才能实现我正在寻找的目标?请赐教

python bots review
1个回答
0
投票

看来你有

string
(不是
list
)在
next_song

next_song = "Pantera ~ 10's (2016 Remaster), 290, https://www.youtube.com/watch?v=3ZRiMcdGhqE"

所以你可以使用字符串的函数来处理它。

您可以在

split()
上使用
,
(或
, 
space
)并获取最后一个元素
[-1]
(如果您使用不带空格的
,
,则可能需要
strip()
空格)

url = next_song.split(',')[-1].strip()   # `split` without space

url = next_song.split(', ')[-1]          # `split` with space

如果字符串更复杂,那么您可以

find()
string
http
并使用其位置来切片字符串

pos = next_song.find('http')

url = next_song[pos:]
© www.soinside.com 2019 - 2024. All rights reserved.