LoginSignup
1
0

discord.pyを使って、音楽BOTを作った話

Last updated at Posted at 2024-03-28

皆さん、こんにちは。
takazonessです。

時間があったので、Githubに載ってるサンプルコードをもとにBOTを作ってみました。
ほぼサンプルコードのままで、ログと個人的に要らないと感じた機能を削った感じになりました。

前提

・DiscordBOTの作成・サーバーへの参加
・Pythonの実行環境

ディレクトリの中に入れるもの

BOT用のディレクトリを作成し、下記のような構成にしてください。
ff~のどれかのリンクをクリックすれば、全部同じリンクにたどり着きますので、ダウンロードするファイルは環境に応じて選択してください。
ffmpeg,ffplay,ffprobeはダウンロードした後、展開を行い、フォルダー内binの中に入っています。

・discordbot.py(プログラムの中身)
ffmpeg
ffplay
ffprobe
yt-dlp

Pythonファイルを実行するうえで必要なモジュール

・asyncio
・discord.py
・yt_dlp

モジュールの入れ方

・discord.pyの場合

pip install discord

・yt-dlpの場合

pip install yt_dlp

プログラムの中身

コードについて、改良版を出しました!
https://qiita.com/takazoness/items/5ece64050a2b793e3af4
詳しくは上記記事へ!!

discordbot.py
import asyncio
import discord
import yt_dlp as 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,
    '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 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(f'Player error: {e}') if e else None)

        await ctx.send(f'Now playing: {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(f"Changed volume to {volume}%")

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

        await ctx.voice_client.disconnect()

    @yt.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()


intents = discord.Intents.default()
intents.message_content = True

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


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


async def main():
    async with bot:
        await bot.add_cog(Music(bot))
        await bot.start('BOTのトークン貼ってネ')

asyncio.run(main())

最後に

やはりドキュメントや公式の出しているサンプルコードは偉大ですね!
分かりづらいところありましたらコメントください!

1
0
0

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
1
0