介绍
我想知道关于不和谐的通话时间,但是似乎不和谐中没有这样的功能,所以我做了一个机器人。由于缺乏学习,可能会有一些奇怪的术语。
环境
Python 3.6.0
怎么做
如何制作机器人
我以此为参考。
https://qiita.com/PinappleHunter/items/af4ccdbb04727437477f
取得通话时间
进入或离开语音通道时,将获取当前时间并测量通话时间。
根据
文档,当语音通道发生变化(语音聊天的进入/退出,语音聊天中的麦克风/扬声器静音)时,将调用
参数
1 2 3 4 5 6 7 8 9 | import discord client = discord.Client() @client.event async def on_voice_state_update(before, after): print("ボイスチャンネルで変化がありました") client.run("token")#botのトークン |
这次,我只想在进入和退出语音聊天时进行处理,因此在静音麦克风和扬声器时将播放呼叫。
检查麦克风是否已被
1 2 3 | if((before.voice.self_mute is not after.voice.self_mute) or (before.voice.self_deaf is not after.voice.self_deaf)): print("ボイスチャンネルでミュート設定の変更がありました") return |
接下来,获取通话时间。我使用标准库的datetime模块的
使用
进入语音通道时,名称和当前时间会记录在词典中,而离开语音通道时,会从离开房间的时间中减去进入房间的时间。
通话时间的结果为负,因此请在结尾处将其乘以-1。另外,我正在使用
1 2 3 4 5 6 7 8 9 | import datetime pretime_dict = {} 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 |
最后,在Discord上发送一条消息。似乎可以使用
1 2 3 4 5 6 7 | #メッセージを送りたいテキストチャンネルの名前 reply_channel_name = "general" 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) |
整个代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | import discord import datetime client = discord.Client() pretime_dict = {} @client.event async def on_voice_state_update(before, after): print("ボイスチャンネルで変化がありました") if((before.voice.self_mute is not after.voice.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 = "general" 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")#ボットのトークン |
不和谐行为
参考
不和谐文档
如何使用Python创建简单的Discord Bot?
python获取当前时间