pythonでDiscordのボットを作っています。
今、YouTubeから音声をダウンロードして音楽を流すボットを作ろうとしているんですが、ボイスチャンネルに接続することはできるんですけど、音楽を流してくれません。
Python 3.7です。youtube-dlなどは、最新版だと思います。
プログラムがこちら(とある記事を参考にしました。)
python
1import discord 2import youtube_dl 3 4youtube_dl.utils.bug_reports_message = lambda: '' 5 6ytdl_format_options = { 7 'format': 'bestaudio/best', 8 'outtmpl': '%(extractor)s-%(id)s-%(title)s.%(ext)s', 9 'restrictfilenames': True, 10 'noplaylist': True, 11 'nocheckcertificate': True, 12 'ignoreerrors': False, 13 'logtostderr': False, 14 'quiet': True, 15 'no_warnings': True, 16 'default_search': 'auto', 17 'source_address': '0.0.0.0' # bind to ipv4 since ipv6 addresses cause issues sometimes 18} 19 20ffmpeg_options = { 21 'options': '-vn' 22} 23 24ytdl = youtube_dl.YoutubeDL(ytdl_format_options) 25 26 27class YTDLSource(discord.PCMVolumeTransformer): 28 def __init__(self, source, *, data, volume=0.5): 29 super().__init__(source, volume) 30 31 self.data = data 32 33 self.title = data.get('title') 34 self.url = data.get('url') 35 36 @classmethod 37 async def from_url(cls, url, *, loop=None, stream=False): 38 loop = loop or asyncio.get_event_loop() 39 data = await loop.run_in_executor(None, lambda: ytdl.extract_info(url, download=not stream)) 40 41 if 'entries' in data: 42 # take first item from a playlist 43 data = data['entries'][0] 44 45 filename = data['url'] if stream else ytdl.prepare_filename(data) 46 return cls(discord.FFmpegPCMAudio(filename, **ffmpeg_options), data=data) 47 48@client.event 49async def on_message(message): 50 if message.content.startswith('p!join'): 51 if message.author.voice is None: 52 await message.channel.send('あなたはどのボイスチャンネルにも接続していません。') 53 await message.add_reaction('❌') 54 return 55 voice = await message.author.voice.channel.connect() 56 await message.channel.send('ボイスチャンネルに接続しました。') 57 await message.add_reaction('🆗') 58 message.guild.voice_client.play(discord.FFmpegPCMAudio('https://www.youtube.com/watch?v=RH24wbgMcqk')) 59 if message.content.startswith('p!leave'): 60 if message.guild.voice_client is None: 61 await message.channel.send('私はどのボイスチャンネルにも接続していません。') 62 await message.add_reaction('❌') 63 return 64 await message.guild.voice_client.disconnect() 65 await message.channel.send('ボイスチャンネルから切断しました。') 66 await message.add_reaction('🆗') 67 if message.content.startswith('p!play '): #音楽再生コマンド。問題の部分。 68 if message.guild.voice_client is None: 69 await message.channel.send('私はどのボイスチャンネルにも接続していません。') 70 return 71 if message.guild.voice_client.is_playing(): 72 await message.channel.send('既に音楽を再生中です。') 73 return 74 75 url = message.content[7:] 76 print(url) 77 player = await YTDLSource.from_url(url, loop=client.loop) 78 79 await message.guild.voice_client.play(player) 80 81 await message.channel.send('{} を再生します。'.format(player.title)) 82
発生したエラーのログ
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の再インストール
エラーメッセージをインターネットで調べた
何かわかる人教えて!
あなたの回答
tips
プレビュー