質問編集履歴

2

あああ

2024/07/12 17:03

投稿

yaki_zakana
yaki_zakana

スコア0

test CHANGED
@@ -1 +1 @@
1
- discordのbotがvcに接続しているにもかかわらず、discord.errors.ClientException: Not connected to voice.というエラーが出る
1
+ aaaaaaaaaa
test CHANGED
@@ -1,185 +1,2 @@
1
- ### 実現したいこと
1
+ aaaaaaaaaaaaaa
2
2
 
3
-
4
- discordのテキストチャットにメッセージを送ると、ボイスチャンネルで読み上げる
5
-
6
- ### 前提
7
-
8
- pythonで、テキストチャットの内容を、voicevoxで音声合成してボイスチャットで読み上げるdiscordのbotを作ろうとしていました。
9
- このエラーが発生したときは、/yjoinをテキストチャットに送信し、botがボイスチャットに参加している状態で、テキストチャットに何か(読み上げてほしい文章)を送信したときです。
10
-
11
- ### 発生している問題・エラーメッセージ
12
-
13
- Task exception was never retrieved
14
- future: <Task finished coro=<yomiage() done, defined at c:/Users/yuuki/Documents/python_work/discordbottest/var001.py:122> exception=ClientException('Not connected to voice.')>
15
- Traceback (most recent call last):
16
- File "c:/Users/yuuki/Documents/python_work/discordbottest/var001.py", line 143, in yomiage
17
- this_guild.voice_client.play(source)#なんかした音声を再生する
18
- File "C:\Users\yuuki\Documents\python_work\discordbottest\discordbot_kun\lib\site-packages\discord\voice_client.py", line 555, in play
19
- raise ClientException('Not connected to voice.')
20
- discord.errors.ClientException: Not connected to voice.
21
- ### 該当のソースコード
22
-
23
- ```python
24
- #トークン メモ (discordのトークン)
25
-
26
- #下でインポートしているものに加えて、ffmpegとPyNaClをインストールする必要がある
27
-
28
- import discord#pip install discord.py
29
- import asyncio
30
- import json
31
- import requests#pip install requests
32
- import simpleaudio#pip install simpleaudio
33
- from discord.ext import commands
34
-
35
- intents = discord.Intents.all()
36
- client = discord.Client(intents=intents)
37
-
38
- input_sperker_id = 3#これはあとで自由に変えられるようにしたい
39
-
40
- #voicevoxの動作に必要な関数を定義する
41
- def text_2_wav(text, speaker_id=input_sperker_id, max_retry=20, filename='audio.wav'):
42
- # 音声合成のための、クエリを作成
43
- query_payload = {"text": text, "speaker": speaker_id}
44
- for query_i in range(max_retry):
45
- response = requests.post("http://localhost:50021/audio_query",
46
- params=query_payload,
47
- timeout=60)
48
- if response.status_code == 200:
49
- query_data = response.json()
50
- break
51
- else:
52
- raise ConnectionError('リトライ回数が上限に到達しました。')
53
-
54
- # 音声合成データの作成して、wavファイルに保存
55
- synth_payload = {"speaker": speaker_id}
56
- for synth_i in range(max_retry):
57
- response = requests.post("http://localhost:50021/synthesis",
58
- params=synth_payload,
59
- data=json.dumps(query_data),
60
- timeout=60)
61
- if response.status_code == 200:
62
- with open(filename, "wb") as fp:
63
- fp.write(response.content)
64
- break
65
- else:
66
- raise ConnectionError('リトライ回数が上限に到達しました。')
67
-
68
- def play_auido_by_filename(filename: str):
69
- # 保存したwavファイルを、再生(つかってない)
70
- wav_obj = simpleaudio.WaveObject.from_wave_file(filename)
71
- play_obj = wav_obj.play()
72
- play_obj.wait_done()
73
-
74
-
75
- #Botの準備ができた時の処理
76
- @client.event
77
- async def on_ready():
78
- client.loop.create_task(yomiage())
79
- print("準備完了")
80
-
81
- #いろんな変数やらリストの初期化場所
82
- readlist = []#読むべき内容を集めるリスト
83
- commandlist = ["/yjoin" , "/yleave"]#botの操作をするコマンドの一覧。
84
-
85
-
86
- #メッセージが送信されたときの処理
87
- @client.event
88
- async def on_message(message):
89
- #もしBOTだったら無視する
90
- if message.author.bot:
91
- return
92
-
93
- #yomiage()のために、読み上げるべきサーバーを取得しておく
94
- global this_guild
95
- this_guild = message.guild
96
- print(this_guild)
97
-
98
- #yomiage()のために、bot自身がvcに接続しているかを確認しておく
99
- global vcniiru
100
- if message.guild.voice_client is None:
101
- vcniiru = False
102
- else:
103
- vcniiru = True
104
-
105
-
106
- #読み上げる内容を決定するプログラム
107
- #送信されたメッセージの情報を取得
108
- mescontent = message.content
109
- sousinysa = message.author.display_name
110
-
111
- #読み上げる内容を決定
112
- read_txt = sousinysa + "、" + mescontent
113
-
114
- #読み上げリストに読み上げるべき内容を追加する
115
- if mescontent not in commandlist:#もし送信されたメッセージがコマンドならリストに追加しない(処理を実行しない)
116
- readlist.append(read_txt)#送信されたメッセージがコマンドじゃなかったらこれが実行される
117
-
118
- print(readlist)
119
- #await message.channel.send(read_txt)#テスト用。完成時は消す
120
-
121
-
122
- #vc入室操作用
123
- if message.content == "/yjoin":
124
- #ボイスチャンネル接続コマンド実行者がVCに入ってないと実行を拒否
125
- if message.author.voice is None:
126
- await message.channel.send("このコマンドを実行した人が、このサーバーのボイスチャンネルに入っている必要があります")
127
- return
128
-
129
- # ボイスチャンネルに接続する
130
- await message.author.voice.channel.connect()
131
-
132
- await message.channel.send("接続しました。")
133
-
134
- #ボイスチャンネルから切断する
135
- if message.content =="/yleave":
136
- if message.guild.voice_client is None:
137
- await message.channel.send("そもそも接続してねぇよ何言ってんだバーカ")
138
- return
139
- await message.guild.voice_client.disconnect()
140
-
141
- await message.channel.send("切断しました。")
142
-
143
-
144
- #読み上げ用関数
145
- async def yomiage():
146
- while True:
147
- #読み上げリストが空なら何もしない
148
- if not readlist:
149
- await asyncio.sleep(1)
150
- #print("そこには何もなかった")#テスト用
151
-
152
-
153
- #読み上げリストに何かあったのなら、リストの0番目を読んでリストから削除する
154
- else:
155
- print("読み上げの時間だ")
156
- readcontent = readlist[0]
157
-
158
- if __name__ == '__main__':
159
- filename = 'audio.wav' # 音声データのファイル名
160
- #play_auido_by_filename(filename) # 合成した音声をpcで再生。テスト用。完成時は消す
161
-
162
- #bot自身がvcに入っている場合は実行する
163
- if vcniiru:
164
- text_2_wav(readcontent, filename=filename) # 読み上げリストの0番目を音声合成
165
- source = discord.FFmpegPCMAudio('audio.wav')#合成した音声をなんかする
166
- this_guild.voice_client.play(source)#なんかした音声を再生する
167
- del readlist[0]
168
-
169
- #bot自身がvcに参加していないときは、読み上げない
170
- else:
171
- print("vcに接続されていないと判定されています")
172
- del readlist[0]
173
-
174
-
175
-
176
- #botを起動する
177
- client.run("discordのtトークン")
178
- ```
179
-
180
- ### 試したこと
181
-
182
- pip install --upgrade discord.py
183
- bot再起動したりvcに入れなおしたり
184
- /yjoinを複数回したときに、discord.errors.ClientException: Already connected to a voice channel.のエラーメッセージが出ることを確認した
185
-

1

コピペするエラーメッセージを間違えていたので修正。試したことの追加

2023/11/27 15:52

投稿

yaki_zakana
yaki_zakana

スコア0

test CHANGED
File without changes
test CHANGED
@@ -10,15 +10,14 @@
10
10
 
11
11
  ### 発生している問題・エラーメッセージ
12
12
 
13
- Ignoring exception in on_message
13
+ Task exception was never retrieved
14
+ future: <Task finished coro=<yomiage() done, defined at c:/Users/yuuki/Documents/python_work/discordbottest/var001.py:122> exception=ClientException('Not connected to voice.')>
14
15
  Traceback (most recent call last):
15
- File "C:\Users\yuuki\Documents\python_work\discordbottest\discordbot_kun\lib\site-packages\discord\client.py", line 343, in _run_event
16
- await coro(*args, **kwargs)
17
- File "c:/Users/yuuki/Documents/python_work/discordbottest/var001.py", line 107, in on_message
16
+ File "c:/Users/yuuki/Documents/python_work/discordbottest/var001.py", line 143, in yomiage
18
- await message.author.voice.channel.connect()
17
+ this_guild.voice_client.play(source)#なんかした音声を再生する
19
- File "C:\Users\yuuki\Documents\python_work\discordbottest\discordbot_kun\lib\site-packages\discord\abc.py", line 1274, in connect
18
+ File "C:\Users\yuuki\Documents\python_work\discordbottest\discordbot_kun\lib\site-packages\discord\voice_client.py", line 555, in play
20
- raise ClientException('Already connected to a voice channel.')
19
+ raise ClientException('Not connected to voice.')
21
- discord.errors.ClientException: Already connected to a voice channel.
20
+ discord.errors.ClientException: Not connected to voice.
22
21
  ### 該当のソースコード
23
22
 
24
23
  ```python
@@ -182,4 +181,5 @@
182
181
 
183
182
  pip install --upgrade discord.py
184
183
  bot再起動したりvcに入れなおしたり
184
+ /yjoinを複数回したときに、discord.errors.ClientException: Already connected to a voice channel.のエラーメッセージが出ることを確認した
185
185