【发布时间】:2020-06-28 21:19:48
【问题描述】:
我正在制作一个可以播放音乐的 discord.py 机器人。当我在本地机器上运行它时,有时一切正常,但有时当我尝试使用播放列表运行它时,它只是无法在播放列表中找到歌曲。
播放命令如下:
@commands.command(name='play',aliases=["p","sing"])
async def _play(self, ctx: commands.Context, *, song1: str):
"""Plays a song.
If there are songs in the queue, this will be queued until the
other songs finished playing.
This command automatically searches from various sites if no URL is provided.
A list of these sites can be found here: https://rg3.github.io/youtube-dl/supportedsites.html
"""
if not ctx.voice_state.voice:
await ctx.invoke(self._join)
x = False
if "playlist?" not in song1:
songs = [song1]
else:
x = True
"""html_content = urllib.request.urlopen(song1)
html_content = html_content.read().decode()"""
r = requests.get(song1)
html_content = r.text
pattern = "href=\"\/watch\?v=(.{11})"
songs = re.findall(pattern,html_content)
print(f"Before: {songs}")
songs = list(dict.fromkeys(songs))
print(f"after: {songs}")
#songs.append(f"https://www.youtube.com/watch?v={code}")
for search in songs:
s = search if not x else f"https://www.youtube.com/watch?v={search}"
try:
source = await YTDLSource.create_source(ctx, s, loop=self.bot.loop)
except YTDLError as e:
await ctx.send('An error occurred while processing this request: {}'.format(str(e)))
else:
song = Song(source)
await ctx.voice_state.songs.put(song)
if not x:
await ctx.send('Enqueued {}'.format(str(source)))
if x:
await ctx.send(f"Succesfully queued the playlist `{song1}`")
控制台中有什么
我运行的命令不和谐:
pplay https://www.youtube.com/playlist?list=PL2n_fVXKImKlfv3PlcHZTzLk3CoGUV9Hm
工作时输出:
Before: ['gEbRqpFkTBk', 'gEbRqpFkTBk', 'gEbRqpFkTBk', 'gEbRqpFkTBk', '4ZvnbsfXRk0', '4ZvnbsfXRk0', 'besNDPvEwQw', 'besNDPvEwQw', 'QglaLzo_aPk', 'QglaLzo_aPk', 'YJTae5ScvQA', 'YJTae5ScvQA', '9Va88Kt0NN0', '9Va88Kt0NN0']
after: ['gEbRqpFkTBk', '4ZvnbsfXRk0', 'besNDPvEwQw', 'QglaLzo_aPk', 'YJTae5ScvQA', '9Va88Kt0NN0']
大部分时间输出:
Before: []
after: []
它不会给出任何错误。
【问题讨论】:
-
我之前遇到过
youtube_dl的问题。也许尝试更新
标签: python-3.x python-requests discord.py python-re