Discord.py 重写 - YoutubeDL 播放音乐的来源是什么?

Lew*_*s H 3 python-3.x youtube-dl discord.py

正如此处文档中提到的,我需要使用 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())
Run Code Online (Sandbox Code Playgroud)

“YTDLSource”是源的占位符。

非常感谢任何帮助,谢谢。

Lia*_*ugh 5

我确信有更好的方法可以通过重写来做到这一点,但我和你处境相同。我很长时间都想不通。

在查看了 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}")
Run Code Online (Sandbox Code Playgroud)

播放命令:

@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()
Run Code Online (Sandbox Code Playgroud)

离开命令:

@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")
Run Code Online (Sandbox Code Playgroud)

所有需要导入的东西(我认为):

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
Run Code Online (Sandbox Code Playgroud)

您可能还需要从他们的网站下载 ffmpeg(youtube 上有关于如何下载和安装的教程)

使用带有 youtube url ('/play www.youtube.com') 的 Play 命令帖子,它将首先查找 'song.mp3' 并删除它(如果有),下载新歌曲将其重命名为 'song.mp3' ' 然后播放 mp3 文件。mp3 文件将放在与 bot.py 相同的目录中

就像我之前说的,可能有一种更好的方法来做到这一点,允许队列命令,但我现在还不知道这种方法。

希望这可以帮助!