pythonでDiscordのボットを作っています。
今、YouTubeから音声をダウンロードして音楽を流すボットを作ろうとしているんですが、ボイスチャンネルに接続することはできるんですけど、音楽を流してくれません。
Python 3.7です。youtube-dlなどは、最新版だと思います。
プログラムがこちら(とある記事を参考にしました。)
python
import discord import youtube_dl 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.event async def on_message(message): if message.content.startswith('p!join'): if message.author.voice is None: await message.channel.send('あなたはどのボイスチャンネルにも接続していません。') await message.add_reaction('❌') return voice = await message.author.voice.channel.connect() await message.channel.send('ボイスチャンネルに接続しました。') await message.add_reaction('🆗') message.guild.voice_client.play(discord.FFmpegPCMAudio('https://www.youtube.com/watch?v=RH24wbgMcqk')) if message.content.startswith('p!leave'): if message.guild.voice_client is None: await message.channel.send('私はどのボイスチャンネルにも接続していません。') await message.add_reaction('❌') return await message.guild.voice_client.disconnect() await message.channel.send('ボイスチャンネルから切断しました。') await message.add_reaction('🆗') if message.content.startswith('p!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[7:] print(url) player = await YTDLSource.from_url(url, loop=client.loop) await message.guild.voice_client.play(player) await message.channel.send('{} を再生します。'.format(player.title))
発生したエラーのログ
youtube_dl.utils.DownloadError: ERROR: unable to open for writing: [Errno 13] Permission denied: 'youtube-jK2aIUmmdP4-Different_Heaven_EH_DE_-_My_Heart_NCS_Release.webm.part'
※ 「'youtube-jK2aIUmmdP4-Different_Heaven_EH_DE_-_My_Heart_NCS_Release.webm.part'」は、YouTubeの動画のIDとタイトル。
試したこと
youtube-dlの再インストール
エラーメッセージをインターネットで調べた
何かわかる人教えて!
まだ回答がついていません
会員登録して回答してみよう