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

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

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

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

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Q&A

0回答

504閲覧

discord.pyで送付された音楽を一時ファイルとしてダウンロードはできるが再生できない

ArLgon

総合スコア0

Discord

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

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

0グッド

0クリップ

投稿2023/03/29 06:44

編集2023/03/29 10:50

実現したいこと

discord.pyで送付された音楽を再生する

前提

discord.pyで送付された音楽を再生するbotを製作しているのですが、エラーメッセージも出ず、一時ファイルのダウンロードも成功しているのに音が鳴りません。botの話しているインジゲーターもつかない状態です。
botがVCに入室し添付ファイルのmp3をダウンロードするところまでは確認できたのですが、再生が行われません。ffmpegもインストールしパスも通しましたし、本当に何のエラーメッセージも出ていません。

検索してもエラーが出ていない同じような状況の人が見つからない状況です。

discordにarl!playというメッセージが送られたときに送付されたmp3をサーバーがダウンロードするところまではできるのですが、

sound = discord.FFmpegPCMAudio("C:/ffmpeg-master-latest-win64-gpl-shared/bin/temp.mp3") await vc.play(sound)

でsoundを再生する部分でうまくいきません。(パスは合っています)

該当のソースコード

関係のない機能のコードもありますが念のため全文張ります

python

1import discord 2from discord.ext import commands, tasks 3import requests 4import asyncio 5from datetime import datetime, timedelta 6from discord.utils import get 7from discord import FFmpegPCMAudio 8import os 9import random 10import string 11client = commands.Bot(command_prefix='arl!',intents=discord.Intents.all()) 12reminders = [] 13whitelist = [] 14cwhitelist = [] 15locked_vc = [] 16status_messages = ['arl!でいろいろできます', 'あぁるごんのbotです', '要望は製作者まで'] 17status_index = 0 18@tasks.loop(seconds=30) 19async def change_status(): 20 global status_index 21 await client.change_presence(activity=discord.Game(name=status_messages[status_index])) 22 status_index = (status_index + 1) % len(status_messages) 23@client.event 24async def on_ready(): 25 print('Bot is ready.') 26 change_status.start() 27 28@client.event 29async def on_voice_state_update(member, before, after): 30 if after.channel not in locked_vc: 31 # /lockが実行されていないVCではキックしない 32 return 33 34 if before.channel != after.channel: 35 # ボイスチャンネルからの移動の場合のみ実行 36 if after.channel is not None: 37 # VCに入ったユーザーがホワイトリストに入っていない場合はキックする 38 if member.id not in whitelist: 39 if member.id not in cwhitelist: 40 user = await client.fetch_user(member.id) # USER_IDには、DMを送信したいユーザーのIDを入力します 41 user2 = await client.fetch_user(cwhitelist[0]) 42 await user.send(f'あなたはホワイトリストに入っていないため{after.channel}からキックされました') # メッセージの内容を入力します 43 await member.move_to(None) 44 await user2.send(f'{member.name}がキックされました') 45@client.command() 46async def lock(ctx): 47 if not locked_vc: 48 author_name = ctx.author.name 49 if ctx.author.voice.channel not in locked_vc: 50 # /lockが実行されたVCを記録する 51 locked_vc.append(ctx.author.voice.channel) 52 cwhitelist.append(ctx.author.id) 53 await ctx.send(f'VCをロックしました。{author_name}さんはコマンドホワイトリストに追加されました。') 54 else: 55 await ctx.send('VCはもうすでにロックされています。') 56 57 else: 58 await ctx.send('ほかのVCをロックしているので無理です') 59@client.command() 60async def unlock(ctx): 61 author_name = ctx.author.name 62 if ctx.author.id in cwhitelist: 63 if locked_vc: 64 # /unlockが実行されたら、locked_vcを空にする 65 locked_vc.clear() 66 await ctx.send('ロックを解除しました。') 67 else: 68 await ctx.send('コマンドホワイトリストに追加されているメンバーのみがこの操作をすることができます。') 69 70@client.command() 71async def add(ctx, user: discord.Member): 72 author_name = ctx.author.name 73 if ctx.author.id in cwhitelist: 74 # 最初にVCに入ったユーザーのみがホワイトリストの操作を行えるようにする 75 whitelist.append(user.id) 76 await ctx.send(f'{user.name}さんがホワイトリストに追加されました') 77 else: 78 await ctx.send('コマンドホワイトリストに追加されているメンバーのみがこの操作をすることができます。') 79@client.command() 80async def C_add(ctx, user: discord.Member): 81 author_name = ctx.author.name 82 if ctx.author.id in cwhitelist: 83 # 最初にVCに入ったユーザーのみがホワイトリストの操作を行えるようにする 84 cwhitelist.append(user.id) 85 await ctx.send(f'{user.name}さんがコマンドホワイトリストに追加されました') 86 else: 87 await ctx.send('コマンドホワイトリストに追加されているメンバーのみがこの操作をすることができます。') 88@client.command() 89async def remove(ctx, user: discord.Member): 90 author_name = ctx.author.name 91 if ctx.author.id in cwhitelist: 92 if user.id in whitelist: 93 whitelist.remove(user.id) 94 await ctx.send(f'{user.name}さんがホワイトリストから削除されました。') 95 else: 96 await ctx.send(f'{user.name}さんはホワイトリストにいませんでした。') 97 else: 98 await ctx.send('コマンドホワイトリストに追加されているメンバーのみがこの操作をすることができます') 99@client.command() 100async def C_remove(ctx, user: discord.Member): 101 author_name = ctx.author.name 102 if ctx.author.id in cwhitelist: 103 if user.id in cwhitelist: 104 cwhitelist.remove(user.id) 105 await ctx.send(f'{user.name}さんがコマンドホワイトリストから削除されました。') 106 else: 107 await ctx.send(f'{user.name}さんはコマンドホワイトリストにいませんでした。') 108 else: 109 await ctx.send('コマンドホワイトリストに追加されているメンバーのみがこの操作をすることができます') 110@client.command() 111async def kick(ctx, user: discord.Member): 112 author_name = ctx.author.name 113 if ctx.author.id in cwhitelist: 114 if user.voice is not None: 115 # ボイスチャンネルにいる場合はキックする 116 await user.move_to(None) 117 await ctx.send(f'{user.name}をキックしました。') 118 member = await client.fetch_user(user.id) # USER_IDには、DMを送信したいユーザーのIDを入力します 119 await member.send('あなたはr!kickされたためキックされました') # メッセージの内容を入力します 120 else: 121 await ctx.send(f'{user.name} はもともとこのVCにいません') 122 else: 123 await ctx.send('コマンドホワイトリストに追加されているメンバーのみがこの操作をすることができます') 124@client.command() 125async def rist(ctx): 126 await ctx.send(whitelist) 127 await ctx.send(cwhitelist) 128@client.command() 129async def scramble_2(ctx): 130 response = requests.get('http://localhost:2014/scramble/.txt?e=222*1') 131 await ctx.send('2x2x2のスクランブルです!') 132 await ctx.send(response.text) 133@client.command() 134async def scramble_3(ctx): 135 response = requests.get('http://localhost:2014/scramble/.txt?e=333*1') 136 await ctx.send('3x3x3のスクランブルです!') 137 await ctx.send(response.text) 138@client.command() 139async def scramble_sk(ctx): 140 response = requests.get('http://localhost:2014/scramble/.txt?e=kewb') 141 await ctx.send('スキューブのスクランブルです!') 142 await ctx.send(response.text) 143@client.command() 144async def scramble_pr(ctx): 145 response = requests.get('http://localhost:2014/scramble/.txt?e=pyram*1') 146 await ctx.send('ぴラミンクスのスクランブルです!') 147 await ctx.send(response.text) 148@client.command() 149async def invite(ctx, user: discord.Member): 150 author_name = ctx.author.name 151 author_c = ctx.author.voice.channel 152 invite = await author_c.create_invite() 153 user = await client.fetch_user(user.id) # USER_IDには、DMを送信したいユーザーのIDを入力します 154 await user.send(f'あなたは{author_name}によって{author_c}に招待されました') # メッセージの内容を入力します 155 await user.send(invite.url) 156 whitelist.append(user.id) 157 await ctx.send(f'{user.name}を招待しました。') 158@client.command() 159async def ping(ctx): 160 await ctx.send(f'Pong! {round(client.latency * 1000)}ms') 161@client.command() 162async def remind(ctx, time:int, *, message:str): 163 # calculate the time when the reminder should go off 164 reminder_time = datetime.now() + timedelta(minutes=time) 165 166 # add the reminder to the list 167 reminders.append((reminder_time, message, ctx.channel.id, ctx.author.id)) 168 169 await ctx.send(f"リマインダーを{time}分後に設定したよ: `{message}`") 170 171@client.event 172async def on_message(message): 173 # check for reminders that need to be sent 174 now = datetime.now() 175 for reminder in reminders: 176 if now >= reminder[0]: 177 channel = client.get_channel(reminder[2]) 178 user = client.get_user(reminder[3]) 179 msg = reminder[1] 180 await channel.send(f"{user.mention}{msg}の時間だ!!!!") 181 reminders.remove(reminder) 182 183 await client.process_commands(message) 184@client.command() 185async def join(ctx): 186 if ctx.author.voice is None: 187 await ctx.send("音声チャンネルに接続してください。") 188 return 189 190 # VCに接続する 191 await ctx.send("ok。") 192 # コマンド実行者のVCを取得 193 voice_channel = ctx.message.author.voice.channel 194 # VCに接続してVoiceClientを取得 195 global voice_client 196 voice_client = await voice_channel.connect() 197@client.command() 198async def leave(ctx): 199 await voice_client.disconnect() 200 await ctx.send("OK") 201@client.command() 202async def play(ctx): 203 # ボイスチャンネルに接続されているか確認 204 if not ctx.author.voice or not ctx.author.voice.channel: 205 await ctx.send('ボイスチャンネルに接続してください。') 206 return 207 208 # メッセージに添付されたファイルを取得する 209 if not ctx.message.attachments: 210 await ctx.send('ファイルが添付されていません。') 211 return 212 attachment = ctx.message.attachments[0] 213 214 # 拡張子が.mp3であるか確認する 215 if not attachment.filename.endswith('.mp3'): 216 await ctx.send('ファイル形式が正しくありません。') 217 return 218 # mp3ファイルをダウンロードし、再生する 219 file_name ="temp.mp3" 220 file_path = os.path.join("C:/ffmpeg-master-latest-win64-gpl-shared/bin/", file_name) 221 222 # ファイルをダウンロードする 223 await attachment.save(file_path) 224 if os.path.isfile(file_path): 225 print("ファイルが正しくダウンロードされました。") 226 else: 227 print("ファイルのダウンロードに問題がありました。") 228 # VoiceChannelに接続する 229 vc = await ctx.author.voice.channel.connect() 230 # 音楽ファイルを再生する 231 sound = discord.FFmpegPCMAudio("C:/ffmpeg-master-latest-win64-gpl-shared/bin/temp.mp3") 232 await vc.play(sound) 233 234 # 再生が終わるまで待つ 235 while vc.is_playing(): 236 await asyncio.sleep(1) 237 238 # 再生が終わったら切断する 239 await vc.disconnect() 240 241 # 一時ファイルを削除する 242 os.remove(file_path) 243 244 245client.run('token') 246

試したこと

ファイルがダウンロードされているか確認
FFmpegのインストールし直し
ダウンロードしたmp3ファイルをWindowsMediaPlayer等で再生できた

補足情報(FW/ツールのバージョンなど)

Python 3.6.3
discord.py1.7.3

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

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

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

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

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

otn

2023/03/29 08:44

試したことに書かれてないのですが、ダウンロードしたmp3ファイルをWindowsMediaPlayer等で再生出来ることは確認できていますか?
ArLgon

2023/03/29 09:06

確認できています。一時ファイルを再生することは可能です。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

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

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

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問