我为discord.py机器人做了一个音乐功能,当我从机器上运行它时一切正常,但是当我将其部署到heroku时,play命令停止工作。
我尝试过的事情:
预期结果: 我希望该机器人能够下载并使用youtube-dl和ffmpeg播放音乐
实际结果: 由于某种原因,它会将某个文件另存为一个很长的名称,看起来有点像youtube-dl选项。 这是完整的回溯:
2020-09-19T09:59:56.637094+00:00 app[worker.1]: Downloading audio
2020-09-19T09:59:57.362517+00:00 app[worker.1]: ERROR: unable to open for writing: [Errno 36] File name too long: 'videoplayback-videoplaybackexpire=1600531195&ei=mtZlX7qyOcyfhwbXz42ICw&ip=3.80.84.128&id=o-ACr3otvjR8X3AKbcspCs4ohKJozzHs5FeSiCdpvKGRix&itag=248&aitags=133,134,135,136,137,160,242,243,244,247,248,278,394,395,396,397,398,399&source=youtube&requiressl=yes&mh=u9&mm=31,26&mn=sn-p5qlsndk,sn-vgqsrnel&ms=au,onr&mv=m&mvi=5&pl=12&initcwndbps=886250&vprv=1&mime=video_webm&gir=yes&clen=22705009&dur=186.unknown_video.part'
2020-09-19T09:59:57.363053+00:00 app[worker.1]: Ignoring exception in command play:
2020-09-19T09:59:57.366683+00:00 app[worker.1]: Traceback (most recent call last):
2020-09-19T09:59:57.366721+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.6/site-packages/youtube_dl/downloader/http.py", line 250, in download
2020-09-19T09:59:57.366722+00:00 app[worker.1]: ctx.tmpfilename, ctx.open_mode)
2020-09-19T09:59:57.366724+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.6/site-packages/youtube_dl/utils.py", line 2054, in sanitize_open
2020-09-19T09:59:57.366724+00:00 app[worker.1]: stream = open(encodeFilename(filename), open_mode)
2020-09-19T09:59:57.366762+00:00 app[worker.1]: OSError: [Errno 36] File name too long: 'videoplayback-videoplaybackexpire=1600531195&ei=mtZlX7qyOcyfhwbXz42ICw&ip=3.80.84.128&id=o-ACr3otvjR8X3AKbcspCs4ohKJozzHs5FeSiCdpvKGRix&itag=248&aitags=133,134,135,136,137,160,242,243,244,247,248,278,394,395,396,397,398,399&source=youtube&requiressl=yes&mh=u9&mm=31,26&mn=sn-p5qlsndk,sn-vgqsrnel&ms=au,onr&mv=m&mvi=5&pl=12&initcwndbps=886250&vprv=1&mime=video_webm&gir=yes&clen=22705009&dur=186.unknown_video.part'
2020-09-19T09:59:57.366779+00:00 app[worker.1]:
2020-09-19T09:59:57.366779+00:00 app[worker.1]: During handling of the above exception, another exception occurred:
2020-09-19T09:59:57.366780+00:00 app[worker.1]:
2020-09-19T09:59:57.366795+00:00 app[worker.1]: Traceback (most recent call last):
2020-09-19T09:59:57.366863+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.6/site-packages/discord/ext/commands/core.py", line 85, in wrapped
2020-09-19T09:59:57.366863+00:00 app[worker.1]: ret = await coro(*args, **kwargs)
2020-09-19T09:59:57.366865+00:00 app[worker.1]: File "/app/cogs/music.py", line 146, in play
2020-09-19T09:59:57.366866+00:00 app[worker.1]: ydl.download([url])
2020-09-19T09:59:57.366885+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.6/site-packages/youtube_dl/YoutubeDL.py", line 2019, in download
2020-09-19T09:59:57.366886+00:00 app[worker.1]: url, force_generic_extractor=self.params.get('force_generic_extractor', False))
2020-09-19T09:59:57.366887+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.6/site-packages/youtube_dl/YoutubeDL.py", line 808, in extract_info
2020-09-19T09:59:57.366888+00:00 app[worker.1]: return self.process_ie_result(ie_result, download, extra_info)
2020-09-19T09:59:57.366905+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.6/site-packages/youtube_dl/YoutubeDL.py", line 863, in process_ie_result
2020-09-19T09:59:57.366906+00:00 app[worker.1]: return self.process_video_result(ie_result, download=download)
2020-09-19T09:59:57.366907+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.6/site-packages/youtube_dl/YoutubeDL.py", line 1644, in process_video_result
2020-09-19T09:59:57.366908+00:00 app[worker.1]: self.process_info(new_info)
2020-09-19T09:59:57.366923+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.6/site-packages/youtube_dl/YoutubeDL.py", line 1926, in process_info
2020-09-19T09:59:57.366923+00:00 app[worker.1]: success = dl(filename, info_dict)
2020-09-19T09:59:57.366925+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.6/site-packages/youtube_dl/YoutubeDL.py", line 1865, in dl
2020-09-19T09:59:57.366925+00:00 app[worker.1]: return fd.download(name, info)
2020-09-19T09:59:57.366941+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.6/site-packages/youtube_dl/downloader/common.py", line 366, in download
2020-09-19T09:59:57.366941+00:00 app[worker.1]: return self.real_download(filename, info_dict)
2020-09-19T09:59:57.366943+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.6/site-packages/youtube_dl/downloader/http.py", line 342, in real_download
2020-09-19T09:59:57.366943+00:00 app[worker.1]: return download()
2020-09-19T09:59:57.366959+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.6/site-packages/youtube_dl/downloader/http.py", line 255, in download
2020-09-19T09:59:57.366959+00:00 app[worker.1]: self.report_error('unable to open for writing: %s' % str(err))
2020-09-19T09:59:57.366961+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.6/site-packages/youtube_dl/downloader/common.py", line 165, in report_error
2020-09-19T09:59:57.366961+00:00 app[worker.1]: self.ydl.report_error(*args, **kargs)
2020-09-19T09:59:57.366991+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.6/site-packages/youtube_dl/YoutubeDL.py", line 625, in report_error
2020-09-19T09:59:57.366992+00:00 app[worker.1]: self.trouble(error_message, tb)
2020-09-19T09:59:57.366992+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.6/site-packages/youtube_dl/YoutubeDL.py", line 595, in trouble
2020-09-19T09:59:57.366992+00:00 app[worker.1]: raise DownloadError(message, exc_info)
2020-09-19T09:59:57.366996+00:00 app[worker.1]: youtube_dl.utils.DownloadError: ERROR: unable to open for writing: [Errno 36] File name too long: 'videoplayback-videoplaybackexpire=1600531195&ei=mtZlX7qyOcyfhwbXz42ICw&ip=3.80.84.128&id=o-ACr3otvjR8X3AKbcspCs4ohKJozzHs5FeSiCdpvKGRix&itag=248&aitags=133,134,135,136,137,160,242,243,244,247,248,278,394,395,396,397,398,399&source=youtube&requiressl=yes&mh=u9&mm=31,26&mn=sn-p5qlsndk,sn-vgqsrnel&ms=au,onr&mv=m&mvi=5&pl=12&initcwndbps=886250&vprv=1&mime=video_webm&gir=yes&clen=22705009&dur=186.unknown_video.part'
2020-09-19T09:59:57.367016+00:00 app[worker.1]:
2020-09-19T09:59:57.367017+00:00 app[worker.1]: The above exception was the direct cause of the following exception:
2020-09-19T09:59:57.367017+00:00 app[worker.1]:
2020-09-19T09:59:57.367019+00:00 app[worker.1]: Traceback (most recent call last):
2020-09-19T09:59:57.367051+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.6/site-packages/discord/ext/commands/bot.py", line 903, in invoke
2020-09-19T09:59:57.367051+00:00 app[worker.1]: await ctx.command.invoke(ctx)
2020-09-19T09:59:57.367053+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.6/site-packages/discord/ext/commands/core.py", line 855, in invoke
2020-09-19T09:59:57.367053+00:00 app[worker.1]: await injected(*ctx.args, **ctx.kwargs)
2020-09-19T09:59:57.367070+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.6/site-packages/discord/ext/commands/core.py", line 94, in wrapped
2020-09-19T09:59:57.367070+00:00 app[worker.1]: raise CommandInvokeError(exc) from exc
2020-09-19T09:59:57.367092+00:00 app[worker.1]: discord.ext.commands.errors.CommandInvokeError: Command raised an exception: DownloadError: ERROR: unable to open for writing: [Errno 36] File name too long: 'videoplayback-videoplaybackexpire=1600531195&ei=mtZlX7qyOcyfhwbXz42ICw&ip=3.80.84.128&id=o-ACr3otvjR8X3AKbcspCs4ohKJozzHs5FeSiCdpvKGRix&itag=248&aitags=133,134,135,136,137,160,242,243,244,247,248,278,394,395,396,397,398,399&source=youtube&requiressl=yes&mh=u9&mm=31,26&mn=sn-p5qlsndk,sn-vgqsrnel&ms=au,onr&mv=m&mvi=5&pl=12&initcwndbps=886250&vprv=1&mime=video_webm&gir=yes&clen=22705009&dur=186.unknown_video.part'
我是通过heroku logs
获得的。
我的代码:
import discord
from discord.ext import commands
import youtube_dl
from helpers.yt_searcher import search
from discord.utils import get
import shutil
import os
class Music(commands.Cog):
def __init__(self, bot):
self.bot = bot
self.queues = {}
@commands.command(aliases=['p'])
async def play(self, ctx, *, search_query):
url = search(search_query)
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 songs.')
self.queues.clear()
return
main_loc = os.path.dirname(os.path.realpath(__file__))
song_path = os.path.abspath(os.path.realpath("Queue") + "\\" + first_file)
if length != 0:
print('Song finished, playing next in queue.')
print(f"Songs left in queue: {still_q}")
song_there = os.path.isfile("song.mp3")
if song_there:
os.remove("song.mp3")
shutil.move(song_path, main_loc)
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 = 0.3
else:
self.queues.clear()
return
else:
self.queues.clear()
print('No songs left in queue.')
song_there = os.path.isfile("song.mp3")
try:
if song_there:
os.remove("song.mp3")
self.queues.clear()
except PermissionError as e:
print("Trying to delete song file but it is being played.")
await ctx.channel.send(':x: Error: music still 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.')
voice = get(self.bot.voice_clients, guild=ctx.guild)
ytdl_opts = {
'format': 'bestaudio/best',
'quiet': True,\
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192'
}],
}
with youtube_dl.YoutubeDL(ytdl_opts) as ydl:
print('Downloading audio')
ydl.download([url])
name = ""
for file in os.listdir('./'):
if file.endswith('.mp3'):
name = file
print(f"Renamed file {name}")
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.3
nname = name.rsplit("-", 2)
await ctx.channel.send(f":musical_note: Playing {nname[0]}")
def setup(bot):
bot.add_cog(Music(bot))
这些都在齿轮中。