【发布时间】:2020-12-23 04:19:29
【问题描述】:
我一直在尝试永久在线托管我的 Discord Bot。虽然所有语音频道(音乐命令)都不再工作,但它并没有太大变化。
我的 requirements.txt 文件包括: 不和谐 ,youtube_dl ,ffmpeg
在构建日志上,它会毫无问题地安装它们,并且机器人很快就会在日志上上线。 为了您的兴趣:我使用 Github 将我的代码推送到 Heroku(它已连接到 Github)。
global voice
channel = ctx.message.author.voice.channel
voice = get(client.voice_clients, guild=ctx.guild)
if voice and voice.is_connected():
await voice.move_to(channel)
else:
voice = await channel.connect()
song_there = os.path.isfile("song.mp3")
try:
if song_there:
os.remove("song.mp3")
queues.clear()
except PermissionError:
return
ydl_opts = {
'format': 'bestaudio/best',
'quiet': True,
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}],
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download([url])
for file in os.listdir("./"):
if file.endswith(".mp3"):
name = file
os.rename(file, "song.mp3")
def my_after(error):
coro = voice.disconnect()
for file in os.listdir("./"):
if file.endswith(".mp3"):
os.remove("song.mp3")
fut = asyncio.run_coroutine_threadsafe(coro, client.loop)
try:
fut.result()
except:
pass
voice.play(discord.FFmpegPCMAudio('song.mp3'), after=my_after)
voice.source = discord.PCMVolumeTransformer(voice.source)
voice.source.volume = 0.10
上面只是一个示例代码,我的一个音乐命令的样子。
这是我的所有导入,使其可以在我的本地 PC 上运行
import discord
import random
import asyncio
import youtube_dl
import os
import shutil
from discord import FFmpegPCMAudio
from discord.ext import commands
from discord.ext.commands import has_permissions, MissingPermissions
from discord.ext.commands import Bot as BotBase
from discord.utils import get
我知道 youtube_dl 和 ffmpegPCMAudio 下载音频文件(如代码中所示)并将其保存在 Queue 文件夹中。但是我怎样才能实现/将其转移到 Heroku 以使其可执行?
【问题讨论】:
标签: python heroku discord discord.py discord.py-rewrite