我正在制作一个与discord.py一起将mp3文件输出到语音聊天的机器人,它可以通过以下方式在本地工作:
vc.play(discord.FFmpegPCMAudio(executable="ffmpeg/bin/ffmpeg.exe", source=noise.mp3))
但是我现在将其托管在Heroku上,我已经安装了buildpack,但是我的代码如何替换上面的代码来访问它
答案 0 :(得分:2)
您未包含代码,所以我不知道您是否希望在不和谐的情况下编写命令或在机器人准备就绪时播放,所以我决定让机器人在以下情况下播放他已经准备好了(如果您希望它加入命令中,您可以轻松地编辑我的代码或要求我这样做)。同样,我认为如果您使用 youtube_dl 会更容易(如果您的歌曲在YouTube上不可用,您可以将其上传为公开而不是私人!)。
import discord
from discord.ext import commands
import youtube_dl
import ctypes
import ctypes.util
Token = "XXXXXX" #your token
client = commands.Bot(command_prefix = ":")
ydl_opts = {
'format': 'bestaudio/best',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}],
}
def endSong(guild, path):
os.remove(path)
url = "https://www.youtube.com/watch?v=dQw4w9WgXcQ" #link to your song on YouTube
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
file = ydl.extract_info(url, download=True)
guild = "1234567890" #id of your server which you can get by right clicking on server name and clicking "Copy ID" (developer mode must be on)
path = str(file['title']) + "-" + str(file['id'] + ".mp3")
channel1 = client.get_channel(1234567890) #id of your channel (you get it like server id, but by right clicking on channel)
voice_client = await channel1.connect()
voice_client.play(discord.FFmpegPCMAudio(path), after=lambda x: endSong(guild, path))
voice_client.source = discord.PCMVolumeTransformer(voice_client.source, 1)
while voice_client.is_playing(): #waits until song ends
await asyncio.sleep(1)
else:
await voice_client.disconnect() #and disconnects
print("Disconnected")
还请记住在Heroku上包括4个buildpack,以确保一切正常(您的APP>“设置”> Buildpacks :
然后检查您的 requirements.txt 中是否包含所有这些内容: