#目的
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() コード