質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Discord

Discordは、ゲーマー向けのボイスチャットアプリです。チャット・通話がブラウザ上で利用可能で、個人専用サーバーも開設できます。通話中でも音楽を流したり、PC画面を共有できるなど多機能な点が特徴です。

YouTube

YouTubeとはユーザーがビデオをアップロード・共有・閲覧できるビデオ共有ウェブサイトです。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

0回答

374閲覧

【discord.py】音楽を流すボットが作れない

piku

総合スコア0

Discord

Discordは、ゲーマー向けのボイスチャットアプリです。チャット・通話がブラウザ上で利用可能で、個人専用サーバーも開設できます。通話中でも音楽を流したり、PC画面を共有できるなど多機能な点が特徴です。

YouTube

YouTubeとはユーザーがビデオをアップロード・共有・閲覧できるビデオ共有ウェブサイトです。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2022/04/04 07:07

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の再インストール
エラーメッセージをインターネットで調べた

何かわかる人教えて!

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問