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

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

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

Windows 10は、マイクロソフト社がリリースしたOSです。Modern UIを標準画面にした8.1から、10では再びデスクトップ主体に戻され、UIも変更されています。PCやスマホ、タブレットなど様々なデバイスに幅広く対応していることが特徴です。

Python 3.x

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

Python

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

Q&A

1回答

3990閲覧

Discord Bot VCに居た時間を測定して表示する

munya24

総合スコア2

Windows 10

Windows 10は、マイクロソフト社がリリースしたOSです。Modern UIを標準画面にした8.1から、10では再びデスクトップ主体に戻され、UIも変更されています。PCやスマホ、タブレットなど様々なデバイスに幅広く対応していることが特徴です。

Python 3.x

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

Python

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

0グッド

0クリップ

投稿2021/04/10 10:13

編集2021/04/14 10:06

#目的
discordのボイチャに入ったら、入った時間だけ時間を表示されるbotを作りたい。
#言語
python

#バージョン・ソフト
python3.9.4

vscode

#参考にしたサイト
https://qiita.com/tokkq/items/311aa297175b9cf7f946

#コード
import discord
import datetime

client = discord.Client()
pretime_dict = {}

@client.event
async def on_ready():
print('ログインしました')

@client.event
async def on_voice_state_update(member, before, after):
print("ボイスチャンネルで変化がありました")

if((before.self_mute is not after.self_mute) or (before.voice.self_deaf is not after.voice.self_deaf)):
print("ボイスチャンネルでミュート設定の変更がありました")
return

if(before.voice_channel is None):
pretime_dict[after.name] = datetime.datetime.now()
elif(after.voice_channel is None):
duration_time = pretime_dict[before.name] - datetime.datetime.now()
duration_time_adjust = int(duration_time.total_seconds()) * -1

reply_channel_name = "Study-time" reply_channel = [channel for channel in before.server.channels if channel.name == reply_channel_name][0] reply_text = after.name + " が "+ before.voice_channel.name + " から抜けました。 通話時間:" + str(duration_time_adjust) +"秒" await client.send_message(reply_channel ,reply_text)

client.run("token")

#追記2

import discord import datetime client = discord.Client() pretime_dict = {} @client.event async def on_ready(): print('ログインしました') @client.event async def on_voice_state_update(member, before, after): print("ボイスチャンネルで変化がありました") if((before.self_mute is not after.self_mute) or (before.voice.self_deaf is not after.voice.self_deaf)): print("ボイスチャンネルでミュート設定の変更がありました") return if(before.voice_channel is None): pretime_dict[after.name] = datetime.datetime.now() elif(after.voice_channel is None): duration_time = pretime_dict[before.name] - datetime.datetime.now() duration_time_adjust = int(duration_time.total_seconds()) * -1 reply_channel_name = "#Study-time" reply_channel = [channel for channel in before.server.channels if channel.name == reply_channel_name][0] reply_text = after.name + " が "+ before.voice_channel.name + " から抜けました。 通話時間:" + str(duration_time_adjust) +"秒" await Client.send_message(reply_channel ,reply_text) client.run("token") #エラー ※起動してボイチャに入って抜けて終わる動作です。 ボイスチャンネルで変化がありました Ignoring exception in on_voice_state_update Traceback (most recent call last): File "C:\Program Files\Python39\lib\site-packages\discord\client.py", line 343, in _run_event await coro(*args, **kwargs) File "c:\Users\user\Desktop\nob\Virtual_currency_of_the_tool\noob.py", line 15, in on_voice_state_update if((before.self_mute is not after.self_mute) or (before.voice.self_deaf is not after.voice.self_deaf)): AttributeError: 'VoiceState' object has no attribute 'voice' ログインしました ボイスチャンネルで変化がありました Ignoring exception in on_voice_state_update Traceback (most recent call last): File "C:\Program Files\Python39\lib\site-packages\discord\client.py", line 343, in _run_event await coro(*args, **kwargs) File "c:\Users\user\Desktop\nob\Virtual_currency_of_the_tool\noob.py", line 15, in on_voice_state_update if((before.self_mute is not after.self_mute) or (before.voice.self_deaf is not after.voice.self_deaf)): AttributeError: 'VoiceState' object has no attribute 'voice' Exception ignored in: <function _ProactorBasePipeTransport.__del__ at 0x00000255F0F0ADC0> Traceback (most recent call last): File "C:\Program Files\Python39\lib\asyncio\proactor_events.py", line 116, in __del__ self.close()ボイスチャンネルで変化がありました Ignoring exception in on_voice_state_update Traceback (most recent call last): File "C:\Program Files\Python39\lib\site-packages\discord\client.py", line 343, in _run_event await coro(*args, **kwargs) File "c:\Users\user\Desktop\nob\Virtual_currency_of_the_tool\noob.py", line 15, in on_voice_state_update if((before.self_mute is not after.self_mute) or (before.voice.self_deaf is not after.voice.self_deaf)): AttributeError: 'VoiceState' object has no attribute 'voice' ログインしました ボイスチャンネルで変化がありました Ignoring exception in on_voice_state_update Traceback (most recent call last): File "C:\Program Files\Python39\lib\site-packages\discord\client.py", line 343, in _run_event await coro(*args, **kwargs) File "c:\Users\user\Desktop\nob\Virtual_currency_of_the_tool\noob.py", line 15, in on_voice_state_update if((before.self_mute is not after.self_mute) or (before.voice.self_deaf is not after.voice.self_deaf)): AttributeError: 'VoiceState' object has no attribute 'voice' Exception ignored in: <function _ProactorBasePipeTransport.__del__ at 0x00000255F0F0ADC0> Traceback (most recent call last): File "C:\Program Files\Python39\lib\asyncio\proactor_events.py", line 116, in __del__ self.close() コード

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

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

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

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

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

K_3578

2021/04/14 06:10

回答が得られなかった等の理由により、同様の質問を投稿するのは禁止です。 https://teratail.com/questions/333104 は削除リクエストを送ってください。 そして、上記質問でも指摘のあるとおり、teratailではソースコードはMarkDownの<code>ブロック内に 貼り付けてください。インデントがされていないので非常に見づらいです。
munya24

2021/04/14 06:14

すいませんでした
K_3578

2021/04/14 06:17

対応して頂ければ問題ありませんよ。解決したくて焦る気持ちは分かりますが、 厚意で成り立っているコミュニティなので、マナーとモラルを持って質問して頂けると。 <code>ブロックに関しては、使い方がわからなかったら下記質問が参考になると思います。 一度見てみて下さい。 https://teratail.com/questions/238564
munya24

2021/04/14 06:19

見てみます インテントは1個以上できるんですか?
K_3578

2021/04/14 06:22

インテント→インデントですかね? ちょっと言ってる意味が理解出来ませんが、恐らく<code>ブロックだと仮定しますが、 <code>ブロック自体は複数使用できます。 ただし、複数使用する場合はブロック間で1行以上スペースを空けてあげないと一つのブロックとして 認識されてしまうので注意してください。
munya24

2021/04/14 06:23

できました インデントです。すいません
K_3578

2021/04/14 06:25

謝らなくていいです。あまり卑屈になられてもこちらも困惑するので。
guest

回答1

0

voice_state_updateイベントのイベントハンドラには3つの位置引数が渡されるのに before と after しか受け取らないように記述されているからですよ。

py

1@client.event 2async def on_voice_state_update(member, before, after):

でどうでしょう。

(追記)

AttributeError: 'VoiceState' object has no attribute 'voice'

参考サイトのコードは投稿日を見る限りかなり古いので参考になりません。
全体的に書き直すことになりそうです。

この AttributeError は、before、afterにはそれぞれVoiceStateオブジェクトが入るのでvoice属性を参照できないために発生します。

before.voice.self_mute などvoice属性を参照する部分は before.self_mute というように置き換えてみてください。


###(追記2)

エラーの読み方を知ったほうがよいですね

AttributeError: '〇〇' object has no attribute '▲▲' このエラー文はそのまま 「▲▲」という属性は 〇〇 オブジェクトにはない、ということです
AttributeError: 'VoiceState' object has no attribute 'voice'

ゆえにこれは VoiceState オブジェクトは voice 属性を持っていないという意味になります。
VoiceState はここでは beforeafter の型です
どこで voice 属性を参照しているでしょうか?

ここです:

py

1if((before.self_mute is not after.self_mute) or (before.voice.self_deaf is not after.voice.self_deaf)):

before.voice.self_deaf is not after.voice.self_deaf

before や after にvoice属性はないことを念頭に置いてください:

before.self_deaf is not after.self_deaf

###(追記3)

Pythonでは、同じレベルのブロックの中ではインデントの数は同じにする必要があります。
というより、インデントの数によってブロックの深さを表現しています。

py

1 print("ボイスチャンネルで変化がありました") 2 3if((before.self_mute is not after.self_mute) or (before.voice.self_deaf is not after.voice.self_deaf)): 4 print("ボイスチャンネルでミュート設定の変更がありました") 5 return 6 7 if(before.voice_channel is None): 8 pretime_dict[after.name] = datetime.datetime.now()

インデントの数がどれもバラバラなので、これはエラーになります。

ブロックというのは、「:」記号以降に記述された一連の処理です。

py

1# ここは一番浅く 2 3def f(): 4 # 関数宣言には「:」がつくので1段下げる 5 for i in range(5): 6 # for文には「:」がつくので1段下げる

このように、「:」でブロックが発生したら1段下げるように覚えればよいと思います
(インデントは半角4個のスペースで表現するのが一般的)

ゆえに、

py

1@client.event 2async def on_voice_state_update(member, before, after): 3 print("ボイスチャンネルで変化がありました") 4 5 if((before.self_mute is not after.self_mute) or (before.voice.self_deaf is not after.voice.self_deaf)): 6 print("ボイスチャンネルでミュート設定の変更がありました") 7 return 8 9 if(before.voice_channel is None): 10 pretime_dict[after.name] = datetime.datetime.now() 11 elif(after.voice_channel is None): 12 duration_time = pretime_dict[before.name] - datetime.datetime.now() 13 duration_time_adjust = int(duration_time.total_seconds()) * -1

このように修正するとよいです。

投稿2021/04/10 10:28

編集2021/04/14 07:47
coolwind0202

総合スコア708

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

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

munya24

2021/04/10 10:43

ボイスチャンネルで変化がありました Ignoring exception in on_voice_state_update Traceback (most recent call last): File "C:\Program Files\Python39\lib\site-packages\discord\client.py", line 343, in _run_event await coro(*args, **kwargs) File "C:\Users\user\Desktop\DISCORD\Virtual_currency\bot.py", line 11, in on_voice_state_update if((before.voice.self_mute is not after.voice.self_mute) or (before.voice.self_deaf is not after.voice.self_deaf)): AttributeError: 'VoiceState' object has no attribute 'voice' ボイスチャンネルで変化がありました Ignoring exception in on_voice_state_update Traceback (most recent call last): File "C:\Program Files\Python39\lib\site-packages\discord\client.py", line 343, in _run_event await coro(*args, **kwargs) File "C:\Users\user\Desktop\DISCORD\Virtual_currency\bot.py", line 11, in on_voice_state_update if((before.voice.self_mute is not after.voice.self_mute) or (before.voice.self_deaf is not after.voice.self_deaf)): AttributeError: 'VoiceState' object has no attribute 'voice' Exception ignored in: <function _ProactorBasePipeTransport.__del__ at 0x00000166C9D99DC0> Traceback (most recent call last): File "C:\Program Files\Python39\lib\asyncio\proactor_events.py", line 116, in __del__ File "C:\Program Files\Python39\lib\asyncio\proactor_events.py", line 108, in close File "C:\Program Files\Python39\lib\asyncio\base_events.py", line 746, in call_soon File "C:\Program Files\Python39\lib\asyncio\base_events.py", line 510, in _check_closed RuntimeError: Event loop is closed こういうふうになりました。
munya24

2021/04/10 10:44

多分「ボイスチャンネルで変化がありました」というところは、ボイチャに入って抜けたときです。
munya24

2021/04/13 09:53

追記しました。
munya24

2021/04/14 05:44

追記しました。
coolwind0202

2021/04/14 05:51

このエラーはインデントの数が正しくないという意味なのですが、直接コードを貼ると行頭のインデントが表示されないので、 コードを貼ったあと ``` 記号で囲ってください。 Pythonの場合は以下のように: ```py print("Hello world") ```
munya24

2021/04/14 06:03

はい わかりました。
munya24

2021/04/14 10:41

追記しました。
coolwind0202

2021/04/14 23:03

なぜbeforeやafterからvoice属性を参照したままなのでしょうか?
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

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

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

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問