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

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

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

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

Python

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

Q&A

解決済

1回答

2098閲覧

【Discord.py】 読み上げ機能を作りたいんですが、エラーを吐いてしまいます

kuripasanda

総合スコア5

Discord

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

Python

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

0グッド

0クリップ

投稿2021/09/04 07:34

編集2021/09/05 10:17

前提・実現したいこと

Python初心者です。
Discord.pyを使用して、読み上げ機能を作りたいです。
windowsでは問題がなかったんですが、本番環境にアップロードすると、エラー吐いてしまいます。

発生している問題・エラーメッセージ

Ignoring exception in on_message Traceback (most recent call last): File "/home/music1225/.local/lib/python3.6/site-packages/discord/client.py", line 343, in _run_event await coro(*args, **kwargs) File "/opt/discord_bot/kai/plugins/chat/bin/read_bot.py", line 72, in on_message creat_WAV(message.content) File "/opt/discord_bot/kai/plugins/chat/bin/voice_generator.py", line 44, in creat_WAV subprocess.run(cmd) File "/usr/lib64/python3.6/subprocess.py", line 423, in run with Popen(*popenargs, **kwargs) as process: File "/usr/lib64/python3.6/subprocess.py", line 729, in __init__ restore_signals, start_new_session) File "/usr/lib64/python3.6/subprocess.py", line 1364, in _execute_child raise child_exception_type(errno_num, err_msg, err_filename) FileNotFoundError: [Errno 2] No such file or directory: 'open_jtalk -x dic -m mei/mei_happy.htsvoice -r 1.0 -ow output.wav input.txt': 'open_jtalk -x dic -m mei/mei_happy.htsvoice -r 1.0 -ow output.wav input.txt'

該当のソースコード

read_bot.py

python

1import discord 2from discord.ext import commands 3import asyncio 4import os 5import subprocess 6import ffmpeg 7import time 8from voice_generator import creat_WAV 9 10TOKEN = 'TOKEN' 11 12client = commands.Bot(command_prefix='kai.') 13voice_client = None 14 15 16@client.event 17async def on_ready(): 18 print('KAI-読み上げプラグイン | INFO : 起動しました') 19 time.sleep(1) 20 print('KAI-読み上げプラグイン | INFO : ログイン完了') 21 print(client.user.name) 22 print(client.user.id) 23 print('------') 24 25 26@client.command() 27async def join(ctx): 28 print('KAI-読み上げプラグイン | INFO : #join') 29 print('KAI-読み上げプラグイン | INFO : #voicechannelを取得') 30 vc = ctx.author.voice.channel 31 print('KAI-読み上げプラグイン | INFO : #voicechannelに接続') 32 await vc.connect() 33async def con(ctx): 34 print('KAI-読み上げプラグイン | INFO : #con') 35 print('KAI-読み上げプラグイン | INFO : #voicechannelを取得') 36 vc = ctx.author.voice.channel 37 print('KAI-読み上げプラグイン | INFO : #voicechannelに接続') 38 await vc.connect() 39 40@client.command() 41async def dc(ctx): 42 print('KAI-読み上げプラグイン | INFO : #dc') 43 print('KAI-読み上げプラグイン | INFO : #切断') 44 await ctx.voice_client.disconnect() 45async def disconnect(ctx): 46 print('KAI-読み上げプラグイン | INFO : #disconnect') 47 print('KAI-読み上げプラグイン | INFO : #切断') 48 await ctx.voice_client.disconnect() 49 50@client.command() 51async def register(ctx, arg1, arg2): 52 with open('dic.txt', mode='a') as f: 53 f.write('\n'+ arg1 + ',' + arg2) 54 print('KAI-読み上げプラグイン | INFO : dic.txtに書き込み:''\n'+ arg1 + ',' + arg2) 55 await ctx.send('KAI-読み上げプラグイン | INFO : `' + arg1+'` を `'+arg2+'` として登録しました') 56 57 58 59@client.event 60async def on_message(message): 61 print('KAI-読み上げプラグイン | INFO : ---on_message_start---') 62 63 msgclient = message.guild.voice_client 64 print(msgclient) 65 if message.content.startswith('kai.'): 66 pass 67 68 else: 69 if message.guild.voice_client: 70 print('#message.content:'+ message.content) 71 creat_WAV(message.content) 72 source = discord.FFmpegPCMAudio("output.wav") 73 message.guild.voice_client.play(source) 74 else: 75 pass 76 await client.process_commands(message) 77 print('---on_message_end---') 78 79 80client.run(TOKEN) 81

voice_generator.py

python

1import subprocess 2import re 3 4# remove_custom_emoji 5# 絵文字IDは読み上げない 6def remove_custom_emoji(text): 7 pattern = r'<:[a-zA-Z0-9_]+:[0-9]+>' # カスタム絵文字のパターン 8 return re.sub(pattern,'',text) # 置換処理 9 10# urlAbb 11# URLなら省略 12def urlAbb(text): 13 pattern = "https?://[\w/:%#$&?()~.=+\-]+" 14 return re.sub(pattern,'URL省略',text) # 置換処理 15 16encode_code = 'utf-8' 17 18def creat_WAV(text): 19 #message.contentをテキストファイルに書き込み 20 input_file = 'input.txt' 21 22 with open(input_file,'w',encoding=encode_code) as file: 23 file.write(text) 24 25 #辞書のPath 26 x = 'dic' 27 28 #ボイスファイルのPath 29 m = 'mei/mei_happy.htsvoice' 30 31 #発声のスピード 32 r = '1.0' 33 34 #出力ファイル名 and Path 35 ow = 'output.wav' 36 37 command = 'open_jtalk -x '+x+' -m '+m+' -r '+r+' -ow '+ow+' '+input_file 38 39 args= {'x':x, 'm':m, 'r':r, 'ow':ow, 'input_file':input_file} 40 41 cmd= command.format(**args) 42 print(cmd) 43 44 subprocess.run(cmd) 45 return True 46 47if __name__ == '__main__': 48 creat_WAV('テスト')

試したこと

open_jtalk・ffmpegを再インストールしてみたり、環境変数をいじってみたりしましたが、効果なし...

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

使用言語 : Discord.py
OS : CentOS Stream release 8

open_jtalk-1.10
ffmpeg-4.2.4

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

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

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

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

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

guest

回答1

0

ベストアンサー

(トークンが入っていたためGistで無効化しました。)

Windows以外だとファイルや引数を空白ごとに区切る必要があります。
https://docs.python.org/ja/3/library/shlex.html#shlex.split で空白の処理も含めて分割してくれるので、

python

1import shlex 2# ... 3 subprocess.run(shlex.split(cmd))

とすれば動くはずです。

投稿2021/09/04 07:46

sevenc-nanashi

総合スコア643

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

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

kuripasanda

2021/09/04 09:26

ありがとうございましたm(__)m! ずっと苦戦していた問題が解決しました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問