【发布时间】:2021-05-26 11:26:48
【问题描述】:
我正在尝试制作一个基于 Statbot 的 Discord 统计机器人。 它的一项功能是跟踪用户在语音频道中的停留时间。
我检查了VoiceState 的文档(从voiceStateUpdate 事件中检索到),似乎没有关于用户在频道中停留多长时间的内置属性。
我该怎么做?
(编辑:我希望有一个解决方案,我不必每次有人加入/离开时都保存)
【问题讨论】:
标签: discord.js
我正在尝试制作一个基于 Statbot 的 Discord 统计机器人。 它的一项功能是跟踪用户在语音频道中的停留时间。
我检查了VoiceState 的文档(从voiceStateUpdate 事件中检索到),似乎没有关于用户在频道中停留多长时间的内置属性。
我该怎么做?
(编辑:我希望有一个解决方案,我不必每次有人加入/离开时都保存)
【问题讨论】:
标签: discord.js
要检查您的用户在语音频道中停留了多长时间,您需要知道用户何时加入房间以及用户何时离开房间并根据您的喜好存储用户加入某处的时间所以代码将是
bot.on('voiceStateUpdate', async (oldState, newState) => {
let newUserChannel = newState.channel;
let oldUserChannel = oldState.channel;
if (oldUserChannel === null && newUserChannel !== null) {
// User Join a voice channel
// Handle your save when user join in memcache, database , ...
} else if (oldUserChannel !== null && newUserChannel === null) {
// User Leave a voice channel
// Calculate with previous save time to get in voice time
} else if (
oldUserChannel !== null &&
newUserChannel !== null &&
oldUserChannel.id != newUserChannel.id
) {
// User Switch a voice channel
// This is bonus if you want to do something futhermore
}
});
【讨论】: