LoginSignup
43

More than 1 year has passed since last update.

discord.py入門(3) 音声を使用する

Last updated at Posted at 2020-09-15

はじめに

discord.pyの非公式のサーバーを運用しています。
ここでは質問も受け付けています。
https://discord.gg/KPp2Wsu この記事についての質問もこちらにお願いします

この記事について

この記事はdiscord.pyについて段階を踏んで勉強していくための記事になります。
まず始めに基本の書き方を学び、次に発展的な内容を学びます。
【シリーズ】

discord.py入門(1)
discord.py入門(2)
discord.py入門(3)

注意

この記事は少しでもPythonができる人向けになります。
ただし、コピペするだけでも使えますが、その後書けなくなるので、もっと個別に機能を作りたい場合は、
本やpaizaラーニング、ドットインストール、Progate、京大のテキスト を使って勉強することをお勧めします。
私がお勧めな本は、辻真吾さんのPythonスタートブックです。

筆者の動作環境

Python 3.8.3
Mac OS Catalina 10.15.5
discord.py-1.4.1

この記事について

この記事では、今までのそれぞれの紹介のようなシステムではなく、順々に使い方を辿っていくような記事になります。

discord.pyを音声付きでインストールする

今まで皆さんはpip install discord.pyとインストールしていましたが、こうすると音声用のライブラリがインストールされません。ですので、音声用のライブラリも一緒にインストールする必要があります。

pip install discord.py[voice]

最後に[voice]と書くことで音声のライブラリも一緒にインストールされます。

もし既にインストールしている場合は、先にpip uninstall discord.pyでアンインストールしてください。

音声に接続する

まず、音声に接続するコードを書きましょう。

今回は、!joinと打つと接続し、!leaveと打つと切断する機能を作成します。

メッセージの送信者がボイスチャンネルに接続しているかどうかを判断する

メッセージの送信者がボイスチャンネルに接続していないと、どのボイスチャンネルに接続すれば良いかわからなくなってしまいます。

そこで、まずメッセージの送信者がボイスチャンネルに接続しているかどうかを判別する必要があります。

ここでは、discord.Member.voice変数を使用します。この変数は、もしボイスチャンネルに接続していた場合はdiscord.VoiceStateインスタンス、接続していない場合はNoneを返します。

# in on_message

if message.content == "!join":
    if message.author.voice is None:
        await message.channel.send("あなたはボイスチャンネルに接続していません。")
        return
    ...

ボイスチャンネルに接続する

上の項で判定したら、次はボイスチャンネルに接続します。discord.VoiceChannel.connectコルーチン関数で接続できますが、まずdiscord.VoiceChannelインスタンスを取得する必要があります。

これは、discord.Client.get_channel関数でも取得できますが、discord.VoiceState.channel変数も同じくVoiceChannelインスタンスであるので、それを使うことができます。

# in on_message

if message.content == "!join":
    if message.author.voice is None:
        await message.channel.send("あなたはボイスチャンネルに接続していません。")
        return
    # ボイスチャンネルに接続する
    await message.author.voice.channel.connect()

    await message.channel.send("接続しました。")

これだけで、ボイスチャンネルに接続できます!

ボイスチャンネルから切断する

次は、ボイスチャンネルから切断します。これは、少し特殊な方法を使う必要があります。

まず、ボイスチャンネルから切断するためには、discord.VoiceClient.disconnectコルーチン関数を実行する必要がありますが、このdiscord.VoiceClientインスタンスをdiscord.Guildから取得する必要があります。

voice_client = message.guild.voice_client

この値はもし接続していればdiscord.VoiceClient、接続していなければNoneになります。

これを使うと、


import discord


client = discord.Client()


@client.event
async def on_message(message: discord.Message):
    # メッセージの送信者がbotだった場合は無視する
    if message.author.bot:
        return

    if message.content == "!join":
        if message.author.voice is None:
            await message.channel.send("あなたはボイスチャンネルに接続していません。")
            return
        # ボイスチャンネルに接続する
        await message.author.voice.channel.connect()
        await message.channel.send("接続しました。")

    elif message.content == "!leave":
        if message.guild.voice_client is None:
            await message.channel.send("接続していません。")
            return

        # 切断する
        await message.guild.voice_client.disconnect()

        await message.channel.send("切断しました。")

こう書くことができます。

ボイスチャンネルを移動する

discord.VoiceClient.move_toコルーチン関数を使用します。move_toにはあたらしい音声チャンネルのインスタンスを渡します。

# メッセージを送信したユーザーがいるボイスチャンネルに移動する
message.guild.voice_client.move_to(message.author.voice.channel)

音声を再生・一時停止する

ボイスチャンネルに接続したら、次は音声を再生してみましょう。ここでは、example.mp3というファイルがあることを前提とします。

また、ffmpegという物も必要となるので、入れておいてください(検索したら入れ方はたくさん出ます)

!playと打つと再生するようにしてみましょう。

音声を再生する


if message.content == "!play":
    if message.guild.voice_client is None:
        await message.channel.send("接続していません。")
        return
    message.guild.voice_client.play(discord.FFmpegPCMAudio("example.mp3"))

これだけで再生ができます!意外かもしれませんが、play関数はコルーチン関数ではありません。

音声を止める

discord.VoiceClient.pause関数で一時停止をすることができます。

また、discord.VoiceClient.stop関数で停止(再開できない)することができます。

音声の再生を再開する

discord.VoiceClient.resume関数で再生再開できます。

音声の音量を変える

discord.PCMVolumeTransformerを使うことで変更できます。

source = discord.PCMVolumeTransformer(discord.FFmpegPCMAudio("example.mp3"), volume=0.5)

message.guild.voice_client.play(source)

volumeに音量を指定することができます。元音量が1で、最低が0です。(0.5で半分)

Youtubeの音楽を流す

注意

ここから先の内容はYoutubeのTOSに抵触する可能性があるので、自己責任で行ってください。ここでは例だけを示します。

ここを参考にできますが、この内容は高度(自分で書くと難しい)ので、簡単にしたものを紹介します。

まず、pip install youtube_dlとシェルで実行し、youtube_dlライブラリをインストールしてください。

次に、これをファイルの先頭に書いてください:


import asyncio

import discord
import youtube_dl
# 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)

これは https://github.com/Rapptz/discord.py/blob/master/examples/basic_voice.py にあるものです。

次に、これを使用してyoutubeの音楽を再生できるようにします。


import asyncio

import discord
import youtube_dl
# 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)


client = discord.Client()


@client.event
async def on_message(message: discord.Message):
    # メッセージの送信者がbotだった場合は無視する
    if message.author.bot:
        return

    if message.content == "!join":
        if message.author.voice is None:
            await message.channel.send("あなたはボイスチャンネルに接続していません。")
            return
        # ボイスチャンネルに接続する
        await message.author.voice.channel.connect()
        await message.channel.send("接続しました。")

    elif message.content == "!leave":
        if message.guild.voice_client is None:
            await message.channel.send("接続していません。")
            return

        # 切断する
        await message.guild.voice_client.disconnect()

        await message.channel.send("切断しました。")
    elif message.content.startswith("!play "):
        if message.guild.voice_client is None:
            await message.channel.send("接続していません。")
            return
        # 再生中の場合は再生しない
        if message.guild.voice_client.is_playing():
            await message.channel.send("再生中です。")
            return

        url = message.content[6:]
        # youtubeから音楽をダウンロードする
        player = await YTDLSource.from_url(url, loop=client.loop)

        # 再生する
        await message.guild.voice_client.play(player)

        await message.channel.send('{} を再生します。'.format(player.title))

    elif message.content == "!stop":
        if message.guild.voice_client is None:
            await message.channel.send("接続していません。")
            return

        # 再生中ではない場合は実行しない
        if not message.guild.voice_client.is_playing():
            await message.channel.send("再生していません。")
            return

        message.guild.voice_client.stop()

        await message.channel.send("ストップしました。")

終わりに

いかがだったでしょうか。音声の使い方を説明しました。他にも説明して欲しい箇所があればコメントやサーバーにてお願いいたします。

次の記事ではcommandsフレームワークについて解説できればと思います。

それでは。

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
43