python - 不支持的链接:Spotify、Discord.py 音乐机器人
问题描述
我正在制作一个不和谐的机器人。但我有 spotify 问题。
它说
“spotdl:错误:无法识别的参数:-s:(音乐链接)”
我已经安装了 pip youtubedl,spotdl,我需要的每个 pip。我正在为我的不和谐服务器制作这个机器人。
请帮我。
我从这个 Youtube链接中了解到这一点。在本教程中,机器人工作正常。但不是我的。我现在该怎么办?
import os
import shutil
from os import system
import discord
import youtube_dl
from discord.ext import commands
from discord.utils import get
TOKEN = 'TOKEN'
BOT_PREFIX = '.'
bot = commands.Bot(command_prefix=BOT_PREFIX)
@bot.event
async def on_ready():
print("Logged in as: " + bot.user.name + "\n")
@bot.command(pass_context=True, aliases=['j', 'joi'])
async def join(ctx):
global voice
channel = ctx.message.author.voice.channel
voice = get(bot.voice_clients, guild=ctx.guild)
if voice and voice.is_connected():
await voice.move_to(channel)
else:
voice = await channel.connect()
await voice.disconnect()
if voice and voice.is_connected():
await voice.move_to(channel)
else:
voice = await channel.connect()
print(f"The bot has connected to {channel}\n")
await ctx.send(f"Joined {channel}")
@bot.command(pass_context=True, aliases=['l', 'lea'])
async def leave(ctx):
channel = ctx.message.author.voice.channel
voice = get(bot.voice_clients, guild=ctx.guild)
if voice and voice.is_connected():
await voice.disconnect()
print(f"The bot has left {channel}")
await ctx.send(f"Left {channel}")
else:
print("Bot was told to leave voice channel, but was not in one")
await ctx.send("Don't think I am in a voice channel")
@bot.command(pass_context=True, aliases=['p', 'pla'])
async def play(ctx, url: str):
def check_queue():
Queue_infile = os.path.isdir("./Queue")
if Queue_infile is True:
DIR = os.path.abspath(os.path.realpath("Queue"))
length = len(os.listdir(DIR))
still_q = length - 1
try:
first_file = os.listdir(DIR)[0]
except:
print("No more queued song(s)\n")
queues.clear()
return
main_location = os.path.dirname(os.path.realpath(__file__))
song_path = os.path.abspath(os.path.realpath("Queue") + "\\" + first_file)
if length != 0:
print("Song done, playing next queued\n")
print(f"Songs still in queue: {still_q}")
song_there = os.path.isfile("song.mp3")
if song_there:
os.remove("song.mp3")
shutil.move(song_path, main_location)
for file in os.listdir("./"):
if file.endswith(".mp3"):
os.rename(file, 'song.mp3')
voice.play(discord.FFmpegPCMAudio("song.mp3"), after=lambda e: check_queue())
voice.source = discord.PCMVolumeTransformer(voice.source)
voice.source.volume = 100.00
else:
queues.clear()
return
else:
queues.clear()
print("No songs were queued before the ending of the last song\n")
song_there = os.path.isfile("song.mp3")
try:
if song_there:
os.remove("song.mp3")
queues.clear()
print("Removed old song file")
except PermissionError:
print("Trying to delete song file, but it's being played")
await ctx.send("ERROR: Music playing")
return
Queue_infile = os.path.isdir("./Queue")
try:
Queue_folder = "./Queue"
if Queue_infile is True:
print("Removed old Queue Folder")
shutil.rmtree(Queue_folder)
except:
print("No old Queue folder")
await ctx.send("Getting everything ready now")
voice = get(bot.voice_clients, guild=ctx.guild)
ydl_opts = {
'format': 'bestaudio/best',
'quiet': True,
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}],
}
try:
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
print("Downloading audio now\n")
ydl.download([url])
except:
print("FALLBACK: youtube-dl does not support this URL, using Spotify (This is normal if Spotify URL)")
c_path = os.path.dirname(os.path.realpath(__file__))
system("spotdl -f " + '"' + c_path + '"' + " -s " + url)
for file in os.listdir("./"):
if file.endswith(".mp3"):
name = file
print(f"Renamed File: {file}\n")
os.rename(file, "song.mp3")
voice.play(discord.FFmpegPCMAudio("song.mp3"), after=lambda e: check_queue())
voice.source = discord.PCMVolumeTransformer(voice.source)
voice.source.volume = 0.07
nname = name.rsplit("-", 2)
await ctx.send(f"Playing: {nname[0]}")
print("playing\n")
@bot.command(pass_context=True, aliases=['pa', 'pau'])
async def pause(ctx):
voice = get(bot.voice_clients, guild=ctx.guild)
if voice and voice.is_playing():
print("Music paused")
voice.pause()
await ctx.send("Music paused")
else:
print("Music not playing failed pause")
await ctx.send("Music not playing failed pause")
@bot.command(pass_context=True, aliases=['r', 'res'])
async def resume(ctx):
voice = get(bot.voice_clients, guild=ctx.guild)
if voice and voice.is_paused():
print("Resumed music")
voice.resume()
await ctx.send("Resumed music")
else:
print("Music is not paused")
await ctx.send("Music is not paused")
@bot.command(pass_context=True, aliases=['s', 'sto'])
async def stop(ctx):
voice = get(bot.voice_clients, guild=ctx.guild)
queues.clear()
queue_infile = os.path.isdir("./Queue")
if queue_infile is True:
shutil.rmtree("./Queue")
if voice and voice.is_playing():
print("Music stopped")
voice.stop()
await ctx.send("Music stopped")
else:
print("No music playing failed to stop")
await ctx.send("No music playing failed to stop")
queues = {}
@bot.command(pass_context=True, aliases=['q', 'que'])
async def queue(ctx, url: str):
Queue_infile = os.path.isdir("./Queue")
if Queue_infile is False:
os.mkdir("Queue")
DIR = os.path.abspath(os.path.realpath("Queue"))
q_num = len(os.listdir(DIR))
q_num += 1
add_queue = True
while add_queue:
if q_num in queues:
q_num += 1
else:
add_queue = False
queues[q_num] = q_num
queue_path = os.path.abspath(os.path.realpath("Queue") + f"\song{q_num}.%(ext)s")
ydl_opts = {
'format': 'bestaudio/best',
'quiet': True,
'outtmpl': queue_path,
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}],
}
try:
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
print("Downloading audio now\n")
ydl.download([url])
except:
print("FALLBACK: youtube-dl does not support this URL, using Spotify (This is normal if Spotify URL)")
q_path = os.path.abspath(os.path.realpath("Queue"))
system(f"spotdl -ff song{q_num} -f " + '"' + q_path + '"' + " -s " + url)
await ctx.send("Adding song " + str(q_num) + " to the queue")
print("Song added to queue\n")
@bot.command(pass_context=True, aliases=['n', 'nex'])
async def next(ctx):
voice = get(bot.voice_clients, guild=ctx.guild)
if voice and voice.is_playing():
print("Playing Next Song")
voice.stop()
await ctx.send("Next Song")
else:
print("No music playing")
await ctx.send("No music playing failed")
bot.run('TOKEN')
解决方案
你好,Md Sadman Muhtasim Billah!
好吧,似乎问题实际上出在您提交的 URL 中,因为它无法识别 '-s'参数,而不是参数本身。因此,您可能应该尝试他在您发送的 YouTube 视频中使用的相同 URL 来测试它。或尝试其他链接。我在代码中找不到缺陷,它确实与 YT 教程中使用的相同。
希望我有所帮助。:)
推荐阅读
- python - 我如何在 keras 模型中拟合我的数据框?
- c - 我的程序运行但我收到一些警告,告诉我格式 %d 不能去那里
- .net-core - 使用 DotNet Core 设置 Fitnesse
- apache-spark - 在 Spark 中读取 parquet 数据时创建了多少个任务
- spring - 用于异常处理的 Spring AOP
- asp.net-mvc - 应用程序池启动模式:OnDemand 与 AlwaysRunning,哪个最好?
- laravel - 我正在尝试下载 pdf 文件,但使用 laravel 时出现错误
- python - 在python中一起调用两个函数调用时如何修补它们
- java - 在同一个 Spring Boot 应用程序中使用 Tomcat HTTP 和 Netty UDP
- angular - 适用于 Android 较低版本的 PDF 查看器插件