LoginSignup
4
7

More than 3 years have passed since last update.

【最新版】discord.pyでYouTubeの動画を流す!

Last updated at Posted at 2020-12-29

最初に

初めてMarkDownで記事を書くので色々とおかしい部分などあると思いますが温かい目で見て頂けると幸いです(;´Д`)
訂正点などありましたらお知らせください...
そしてdiscord.pyの方も初心者ですのでこちらに関しても温かい目で見て頂けると幸いですが間違った記載をしていると駄目なのでもし見つけた場合もお知らせください。

動作環境

Python 3.9.1
Discord.py 1.5.1
PyNaCl 1.4.0
youtube_dl 2020.12.26
Windows10

準備 -パッケージ

まずは必要なパッケージの準備ですが、前提としてBotが動く環境が整っていることが前提となります。なのでPythonとDiscord.pyは恐らく入っているはずですのでそこは省きます。
今回はyoutube_dlを使いたいと思うので

$ pip install youtube_dl

をターミナルで実行してyoutube_dlを落としましょう。

準備 -ffmpeg

まずサイトに行き、真ん中ぐらいにgitのLinksが2つあると思います。
どちらでも良いのでそれをダウンロード(URLをクリックするとダウンロードが始まります)します。
※恐らくですが上のfullが容量が大きく、下のessentialsはfullに比べて容量が小さいのだと思います。どちらでも変わらないと思うのでダウンロード、解凍が早いessentialsで良いかと思います。

ダウンロードが終わったら解凍すると色々と入っているので

ダウンロードしたffmpegのファイル

bin
┗ ffmpeg.exe

となっているはずなのでffmpeg.exeをコピーなり切り取りなりして、
Botのメインのファイルがあるディレクトリに貼り付けます。
※C:の直下に置いても恐らくエラーが出ます。

これで準備が完了したはずなので次はコードです。

コード

import asyncio

import discord
import youtube_dl

from discord.ext import commands

# Suppress noise about console usage from errors
youtube_dl.utils.bug_reports_message = lambda: ''


ytdl_format_options = {
    'format': 'bestaudio/best',
    'outtmpl': '%(extractor)s-%(id)s-%(title)s.%(ext)s',
    'restrictfilenames': True,
    'noplaylist': True,
    'nocheckcertificate': True,
    'ignoreerrors': False,
    'logtostderr': False,
    'quiet': True,
    'no_warnings': True,
    'default_search': 'auto',
    'source_address': '0.0.0.0' # bind to ipv4 since ipv6 addresses cause issues sometimes
}

ffmpeg_options = {
    'options': '-vn'
}

ytdl = youtube_dl.YoutubeDL(ytdl_format_options)


class YTDLSource(discord.PCMVolumeTransformer):
    def __init__(self, source, *, data, volume=0.5):
        super().__init__(source, volume)

        self.data = data

        self.title = data.get('title')
        self.url = data.get('url')

    @classmethod
    async def from_url(cls, url, *, loop=None, stream=False):
        loop = loop or asyncio.get_event_loop()
        data = await loop.run_in_executor(None, lambda: ytdl.extract_info(url, download=not stream))

        if 'entries' in data:
            # take first item from a playlist
            data = data['entries'][0]

        filename = data['url'] if stream else ytdl.prepare_filename(data)
        return cls(discord.FFmpegPCMAudio(filename, **ffmpeg_options), data=data)


class Music(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @commands.command()
    async def join(self, ctx, *, channel: discord.VoiceChannel):
        """Joins a voice channel"""

        if ctx.voice_client is not None:
            return await ctx.voice_client.move_to(channel)

        await channel.connect()

    @commands.command()
    async def play(self, ctx, *, query):
        """Plays a file from the local filesystem"""

        source = discord.PCMVolumeTransformer(discord.FFmpegPCMAudio(query))
        ctx.voice_client.play(source, after=lambda e: print('Player error: %s' % e) if e else None)

        await ctx.send('Now playing: {}'.format(query))

    @commands.command()
    async def yt(self, ctx, *, url):
        """Plays from a url (almost anything youtube_dl supports)"""

        async with ctx.typing():
            player = await YTDLSource.from_url(url, loop=self.bot.loop)
            ctx.voice_client.play(player, after=lambda e: print('Player error: %s' % e) if e else None)

        await ctx.send('Now playing: {}'.format(player.title))

    @commands.command()
    async def stream(self, ctx, *, url):
        """Streams from a url (same as yt, but doesn't predownload)"""

        async with ctx.typing():
            player = await YTDLSource.from_url(url, loop=self.bot.loop, stream=True)
            ctx.voice_client.play(player, after=lambda e: print('Player error: %s' % e) if e else None)

        await ctx.send('Now playing: {}'.format(player.title))

    @commands.command()
    async def volume(self, ctx, volume: int):
        """Changes the player's volume"""

        if ctx.voice_client is None:
            return await ctx.send("Not connected to a voice channel.")

        ctx.voice_client.source.volume = volume / 100
        await ctx.send("Changed volume to {}%".format(volume))

    @commands.command()
    async def stop(self, ctx):
        """Stops and disconnects the bot from voice"""

        await ctx.voice_client.disconnect()

    @play.before_invoke
    @yt.before_invoke
    @stream.before_invoke
    async def ensure_voice(self, ctx):
        if ctx.voice_client is None:
            if ctx.author.voice:
                await ctx.author.voice.channel.connect()
            else:
                await ctx.send("You are not connected to a voice channel.")
                raise commands.CommandError("Author not connected to a voice channel.")
        elif ctx.voice_client.is_playing():
            ctx.voice_client.stop()

bot = commands.Bot(command_prefix=commands.when_mentioned_or("!"),
                   description='Relatively simple music bot example')

@bot.event
async def on_ready():
    print('Logged in as {0} ({0.id})'.format(bot.user))
    print('------')

bot.add_cog(Music(bot))
bot.run('token')

今回はgithubから取っています。ここからembedとかで見やすくしていく等しても良いかもです。(投げやりで申し訳ないです)

実行

実行すると以下のコマンドが使えます。
join [channel] / [channel]に接続
yt [url] / [url]で指定したリンクを再生 ※[url]は曲名などでもsearchされます
stream [url] / [url]で指定したリンクを再生 ※こっちは分からない
volume [volume] / [volume]で指定した音量に調整
stop / VCから切断します

おまけ

先程の投げやりは流石に記事書く意味がなくなるので(?)私がちょっとコードを書き換えて使いやすくしたのでどうぞ。

    @commands.command(aliases=["p"])
    async def play(self, ctx, *, url):
        channel = ctx.author.voice.channel
        if channel is None:
            return await ctx.send("VCに接続していません。")

        await channel.connect()
        async with ctx.typing():
            player = await YTDLSource.from_url(url, loop=self.bot.loop)
            ctx.voice_client.play(player, after=lambda e: print('Player error: %s' % e) if e else None)
            await ctx.send("再生中:{}".format(player.title))
    @commands.command(aliases=["vol"])
    async def volume(self, ctx, volume: int):
        channel = ctx.author.voice.channel
        if channel is None:
            return await ctx.send(VCに接続していません)

        ctx.voice_client.source.volume = volume / 100
        await ctx.send("ボリューム変更:{}".format(volume))
    @commands.command(aliases=["bye","disconnect"])
    async def stop(self, ctx):
        channel = ctx.author.voice.channel
        if channel is None:
            return await ctx.send(VCに接続していません)

        await ctx.voice_client.disconnect()
        await ctx.send("VCから離脱しました。")

上記のコードの改善点としては
・joinを廃止
・playでVCに接続
・全てにVCに接続しているかの確認
ぐらいの改善をしました。

最後に

いかかだったでしょうか?まだ慣れていない部分もありましたが無事に書き終えることが出来ました!
今はRythmやMEE6などといったBotが有名ですがそれに負けないくらいのBotを目指しています!
宣伝になってしまいますが、私のBotを導入して頂けると嬉しいです!サポートサーバーでBotの機能を試してみたり招待リンクがあったりします。是非見て頂けると嬉しいです。
段々と記事を作ろうと思っていますが書くことが無いのでもし何かあればお知らせください。

それでは良いお年を!

4
7
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
7