- 确保您使用的是最新版本的yt-dl和discord.py。
- 尝试在代码中增加超时时间例如:
# 这里是关键的代码
import time
from discord.ext import commands
ytdl = YoutubeDL(ytdl_options)
bot = commands.Bot(command_prefix='!')
@bot.command()
async def play(ctx, url: str):
# 获取音乐列表
with ytdl:
info = ytdl.extract_info(url, download=False)
# 如果是个播放列表
if "_type" in info and info["_type"] == "playlist":
# 等待15秒钟
await ctx.send("正在加载音乐,请稍等片刻...")
time.sleep(15)
# 尝试加载3次
for i in range(3):
try:
# 播放列表不要缓存
player = await YTDLSource.create_playlist_player(ctx, url, ytdl, stream=False, preload=False)
break
except Exception as e:
print("Error loading playlist", e)
await ctx.send(f"出错了: {e}")
time.sleep(5)
continue
else:
await ctx.send("抱歉,未能完成加载。请稍后再试。")
# 播放列表中的音乐
async with ctx.typing():
for song in player:
await ctx.send(f"正在播放:{song['webpage_url']}")
player.start_playing(song)
# 等待下一首歌
await player.wait_for_song()
else:
await play_song(ctx, url, ytdl)
- 如果对此仍有疑问,请加入Discord的音乐机器人支持群,获得更多帮助。