如何在discord.py中记录用户离开和加入语音频道的时间?

2024-05-23 14:35:41 发布

您现在位置:Python中文网/ 问答频道 /正文

嘿,伙计们,我正在写一个discord机器人,我想在用户进入和离开语音频道时登录。但显然,我现在有点困惑。你们对我如何使用on_voice_state_update()有什么想法吗


Tags: 用户on机器人update语音频道statevoice
2条回答

请参阅here

它包含两个参数beforeafter,因此要检查是否有人离开,只需比较beforeafter,如果不一样,您可以将其记录在某个文件中(可能jsonpicklesqlite库可以提供帮助)

我在下面提供了一些快速示例代码:

@client.event
async def on_voice_state_update(member, before, after):
    if before.channel is None and after.channel is not None: 
        print("difference")

我们观察到一些事情:首先,beforeafterVoiceState对象,您可以查看它们的属性here,这就是我获取before.channel的方式,等等

其次,client是您命名的bot(即client = commands.Bot(..)

@bot.event
async def on_voice_state_update(member, before, after):
    ...

文件:discord.py

相关问题 更多 >