迪斯科音乐机器人坐立不安的yt视频



嗨,我想知道如何让我的机器人根据YouTube搜索播放音乐,因为url复制和粘贴让我很恼火。机器人有YouTube_dl和ffmpg。这是更新后的代码,它仍然不起作用。抱歉我英语不好。

@client.command(pass_context=True, aliases=['p', 'pla'])
async def play(ctx, url: str):
song_there = os.path.isfile("song.mp3")
try:
if song_there:
os.remove("song.mp3")
print("Usunięto starą piosenke.")
except PermissionError:
print("Próbuję usunąć plik utworu, ale jest on odtwarzany ")
await ctx.send(":no_entry_sign:  Muzyka już gra!")
return
await ctx.send(":mag_right: Szukanie i Pobieranie")
voice = get(client.voice_clients, guild=ctx.guild)
ydl_opts = {
'format': 'bestaudio/best',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}],
}
yt = yt_search.build(url)
search_result = yt.search("keyword", sMax=10, sType=["video"])
print(search_result)
with youtube_dl.YoutubeDL(options) as ytdl:
if url[:4] == 'http':
ytdl.download([url])
else: # "yts" is youtube-search imported as
link = json.loads(yts(url, max_results=1).to_json())['videos'][0]['url_suffix']
link = 'https://www.youtube.com' + link
ytdl.download([link])
for file in os.listdir("./"):
if file.endswith(".mp3"):
name = file
print(f"Zmnieniono nazwe: {file}n")
os.rename(file, "song.mp3")
voice.play(discord.FFmpegPCMAudio("song.mp3"), after=lambda e: print("Koniec!"))
voice.source = discord.PCMVolumeTransformer(voice.source)
voice.source.volume = 0.17
nname = name.rsplit("-", 2)
await ctx.send(f" :notes: Teraz Leci: `{nname[0]} - {nname[1]}`")
print("Puszczanien")

在youtube搜索中查找文档。这里有一个例子:

with youtube_dl.YoutubeDL(options) as ytdl:
if url[:4] == 'http':
ytdl.download([url])
else: # "yts" is youtube-search imported as
link = json.loads(yts(url, max_results=1).to_json())['videos'][0]['url_suffix']
link = 'https://www.youtube.com' + link
ytdl.download([link])

基本上说";如果它以http开头,那么只使用作为链接。否则,使用youtube搜索来获得youtube上第一个搜索结果的链接"这可能不是一个好方法,可以先使用try/except来查看搜索是否作为链接工作,except重试传递获取的链接。

最新更新