不和谐.py |从 url 播放音频

时间:2021-02-09 08:16:05

标签: python discord bots discord.py

我想让我的机器人从一个 url 播放音频,但我不想下载文件......

这是我的代码:

{
"user": "login,
"password": "nagibator228",
"auth_type": "session"
}

我尝试了 create_ytdl_player 方法,但发现它不再支持我该怎么办?

2 个答案:

答案 0 :(得分:1)

使用pafy。

首先导入一些东西并设置 FFmpeg 选项...

import pafy
from discord import FFmpegPCMAudio, PCMVolumeTransformer


FFMPEG_OPTIONS = {'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5','options': '-vn'}

接着玩

@commands.command(name='test')
    async def test(self, ctx):

        search = "morpheus tutorials discord bot python"

        if ctx.message.author.voice == None:
            await ctx.send(embed=Embeds.txt("No Voice Channel", "You need to be in a voice channel to use this command!", ctx.author))
            return

        channel = ctx.message.author.voice.channel

        voice = discord.utils.get(ctx.guild.voice_channels, name=channel.name)

        voice_client = discord.utils.get(self.client.voice_clients, guild=ctx.guild)

        if voice_client == None:
            voice_client = await voice.connect()
        else:
            await voice_client.move_to(channel)

        search = search.replace(" ", "+")

        html = urllib.request.urlopen("https://www.youtube.com/results?search_query=" + search)
        video_ids = re.findall(r"watch\?v=(\S{11})", html.read().decode())

        
        await ctx.send("https://www.youtube.com/watch?v=" + video_ids[0])

        song = pafy.new(video_ids[0])  # creates a new pafy object

        audio = song.getbestaudio()  # gets an audio source

        source = FFmpegPCMAudio(audio.url, **FFMPEG_OPTIONS)  # converts the youtube audio source into a source discord can use

        voice_client.play(source)  # play the source
        

答案 1 :(得分:0)

如果需要,请将 <picture> <source [srcset]="element.thumbWebp" type="image/webp" /> <source [srcset]="element.thumb" type="image/jpeg" /> <source [srcset]="element.imageWebp" type="image/webp" /> <img [src]="element.image" [alt]="element.title" height="204" width="145" onerror="this.onerror=; this.src='(DEFAULTIMAGELINKDELETEFORPRIVACY)';" /> </picture> <script> function changeElement() { } </script> 替换为 bot。然后,试试这个:

client

这不仅会从 URL 播放,还会从视频本身的名称播放。