【问题标题】:Is it possible to track how long a user is in a voice channel?是否可以跟踪用户在语音频道中的时间?
【发布时间】:2021-05-26 11:26:48
【问题描述】:

我正在尝试制作一个基于 Statbot 的 Discord 统计机器人。 它的一项功能是跟踪用户在语音频道中的停留时间。

我检查了VoiceState 的文档(从voiceStateUpdate 事件中检索到),似乎没有关于用户在频道中停留多长时间的内置属性。

我该怎么做?

(编辑:我希望有一个解决方案,我不必每次有人加入/离开时都保存)

【问题讨论】:

    标签: discord.js


    【解决方案1】:

    要检查您的用户在语音频道中停留了多长时间,您需要知道用户何时加入房间以及用户何时离开房间并根据您的喜好存储用户加入某处的时间所以代码将是

    
    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
       }
    });
    
    

    【讨论】:

    • 不错!但是,我正在寻找一种不需要保存加入/离开日期的解决方案。如果没有其他人的答案满足上述标准,我将使用它。还是谢谢!
    • 你必须这样做,这似乎是现在唯一的方法。如果有帮助,请标记它的解决方案。可能有一些库支持这个会以这种方式编写
    猜你喜欢
    • 2021-03-30
    • 1970-01-01
    • 2021-01-31
    • 2015-12-18
    • 1970-01-01
    • 2013-03-20
    • 1970-01-01
    • 2021-05-03
    • 1970-01-01
    相关资源
    最近更新 更多