【发布时间】:2021-05-31 19:12:30
【问题描述】:
当我尝试使用 Visual Code Studio 调试我的机器人时,一切正常。但是,当我将所有内容上传到 Heroku 时,!skip 命令就像 !stop 命令一样。我联系了一些人,在查看代码后,他们说这与为队列创建新文件夹有关,我应该将 .mp3 文件下载到主目录中。唯一的问题是,我在代码中多次尝试更改目录,但它要么根本不起作用,要么在我尝试使用 !skip 命令时仍然完全停止音乐。有谁知道如何解决这个问题?
这是我尝试使用 ./Queue 和 Queue 目录更新的 !play 和 !queue 命令。
任何帮助将不胜感激!
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))
try:
first_file = os.listdir(DIR)[0]
except:
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:
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 = 1.00
else:
queues.clear()
return
else:
queues.clear()
song_there = os.path.isfile("song.mp3")
try:
if song_there:
os.remove("song.mp3")
except PermissionError:
return
Queue_infile = os.path.isdir("./Queue")
try:
Queue_folder = "./Queue"
if Queue_infile is True:
shutil.rmtree(Queue_folder)
except:
print("No old Queue folder")
voice = get(client.voice_clients, guild=ctx.guild)
ydl_opts = {
'format': 'bestaudio/best',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}],
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
get_info = ydl.extract_info(url, download=False)
if get_info["duration"] >= 630:
await ctx.send("Sorry, this video is too long. Please try sending a shorter video.")
return False
ydl.download([url])
for file in os.listdir("./"):
if file.endswith(".mp3"):
name = file
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 = 1.00
nname = name.rsplit("-", 2)
await ctx.send(f"Now playing: {nname[0]}")
@client.command()
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',
'outtmpl': queue_path,
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}],
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
get_info = ydl.extract_info(url, download=False)
if get_info["duration"] >= 630:
await ctx.send("Sorry, this video is too long. Please try sending a shorter video.")
return False
ydl.download([url])
【问题讨论】:
-
您是否已将最新版本的代码部署到 Heroku 中?您是否已将最新版本的代码上传到 github?
-
我已将最新版本的代码部署到 Heroku 中,是的。我没有在 GitHub 上这样做过,没有。 Heroku 有点新,代码也需要在 GitHub 上吗?
标签: python heroku discord.py