python-3.x - Discord.py 重写 - YoutubeDL 播放音乐的来源是什么?
问题描述
正如此处的文档中所述,我需要使用 play() 命令使用源来播放音乐,我正在尝试使用 YoutubeDL,但我无法弄清楚。
我已经检查了 rapptz discord.py 基本语音示例,但是由于我没有使用面向对象的编程,这让我很困惑。在我看过的所有地方,他们的示例都是使用 v0.16 discord.py,我无法弄清楚如何将其转换player = await voice_client.create_ytdl_player(url)
为重写。
我现在的播放功能是这样的:
async def play(ctx, url = None):
...
player = await YTDLSource(url)
await ctx.voice_client.play(player)
await ctx.send("Now playing: " + player.title())
“YTDLSource”是源的占位符。
非常感谢任何帮助,谢谢。
解决方案
我相信重写有更好的方法,但我和你在同一条船上。我很长时间都想不通。
在浏览了 youtube-dl 文档和重写文档之后,这是我能想到的最好的。请记住,我不知道这是否适用于队列系统(可能不会)。另外我不知道这是一个错误还是当机器人加入时我做错了什么,然后你使用播放命令它不会输出音乐,但是如果机器人离开然后再次加入,音乐就会播放。为了解决这个问题,我让我的加入命令加入、离开和加入。
加入命令:
@bot.command(pass_context=True, brief="Makes the bot join your channel", aliases=['j', 'jo'])
async def join(ctx):
channel = ctx.message.author.voice.channel
if not channel:
await ctx.send("You are not connected to a voice channel")
return
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()
await ctx.send(f"Joined {channel}")
播放命令:
@bot.command(pass_context=True, brief="This will play a song 'play [url]'", aliases=['pl'])
async def play(ctx, url: str):
song_there = os.path.isfile("song.mp3")
try:
if song_there:
os.remove("song.mp3")
except PermissionError:
await ctx.send("Wait for the current playing music end or use the 'stop' command")
return
await ctx.send("Getting everything ready, playing audio soon")
print("Someone wants to play music let me get that ready for them...")
voice = get(bot.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:
ydl.download([url])
for file in os.listdir("./"):
if file.endswith(".mp3"):
os.rename(file, 'song.mp3')
voice.play(discord.FFmpegPCMAudio("song.mp3"))
voice.volume = 100
voice.is_playing()
离开命令:
@bot.command(pass_context=True, brief="Makes the bot leave your channel", aliases=['l', 'le', '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()
await ctx.send(f"Left {channel}")
else:
await ctx.send("Don't think I am in a voice channel")
所有需要导入的(我认为):
import discord
import youtube_dl
import os
from discord.ext import commands
from discord.utils import get
from discord import FFmpegPCMAudio
from os import system
您可能还需要从他们的网站上下载 ffmpeg(有 youtube 教程介绍如何下载并安装它)
使用带有 youtube url ('/play www.youtube.com') 的播放命令后,它将首先查找“song.mp3”,如果有则将其删除,下载新歌曲将其重命名为“song.mp3” ' 然后播放 mp3 文件。mp3 文件将放在与您的 bot.py 相同的目录中
就像我之前说的那样,可能有一种允许队列命令的击球方式来做到这一点,但我现在还不知道这种方式。
希望这可以帮助!
推荐阅读
- c# - 类模型中的接口为空
- buildroot - libicudata 的 Buildroot 问题。这是什么?
- python - 是否有 Binance API 端点来关闭所有头寸?
- c# - Blazor Webassembly,DatabaseCRUD:已发布版本的内部服务器错误 500
- python - 如何在 python 中使用 apache tika 提取受密码保护的 PDF 数据?
- javascript - Sortable.js 对表格内的元素进行排序
- powerbi - 在另一个表中查找值 - Power BI (DAX)
- linux - 在linux中用另一个字符串替换两列中某些行开头的字符串
- node.js - 什么是使用 Javascript 运行 Discord 机器人的好程序?
- javascript - 如何在一个按钮中实现两个 onclicks?