首页 > 解决方案 > 如何在 discord.py 中将用户的离开和加入时间记录到语音频道?

问题描述

嘿伙计们,我正在编写一个不和谐的机器人,我想在用户进入和离开语音频道时进行记录。但显然,我现在有点困惑。你们对我如何使用有任何想法on_voice_state_update()吗?

标签: pythondiscord.py

解决方案


请看这里

它接受两个参数beforeand after,因此要检查是否有人离开,只需比较beforeand after,如果不一样,您可以将其记录在某个文件中(也许jsonor pickleorsqlite库可以帮助解决这个问题)

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

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

我们观察一些事情:首先,beforeandafter是对象,你可以在这里VoiceState查看它们的属性,这就是我得到的等等。before.channel

其次,client是你命名你的机器人的任何东西(即client = commands.Bot(..)


推荐阅读