【问题标题】:How to log custom status updates using discord.js?如何使用 discord.js 记录自定义状态更新?
【发布时间】:2021-05-19 17:29:29
【问题描述】:

我一直在研究列入黑名单的单词选项,以及当公会成员设置包含黑名单单词的自定义状态时进行记录。但是,我遇到了一个问题,有时我的旧状态与新状态相同(仅适用于某些公会,其他公会很好)。我的代码在这里:

client.on("presenceUpdate", (oldPresence, newPresence) => {
    const newCustomStatus = newPresence.activities[0].state
    const oldCustomStatus = oldPresence.activities[0].state
    console.log('old status: ' + oldCustomStatus)
    console.log('new status: ' + newCustomStatus)
})

oldCustomStatus 在某些公会中有时与newCustomStatus 相同,这使得无法记录旧状态。它只发生在成员和机器人共享的半数公会中。 我考虑过在 bot 上创建所有状态的缓存,并且只切换一次事件,这样我就可以读取新旧状态,因为总是至少有一个公会具有正确的信息。但是,我无法对列入黑名单的单词进行每个公会的检查,因为我不知道从用户对象中读取公会 ID 的方法。
有什么方法可以修复损坏的旧状态和新状态?或者获取用户和机器人共享的公会 ID 列表的方法?
谢谢

【问题讨论】:

    标签: discord.js


    【解决方案1】:

    我通过记录公会 ID 发现它不止一次触发。这似乎解决了一半的问题,因为下一次火灾将新状态作为旧状态和新状态。不知道为什么它会触发多次。
    记录器的一个简单解决方案是使用 if 语句并比较新旧状态,并且仅在它们不同时记录它们:

    client.on("presenceUpdate", (oldPresence, newPresence) => {
        const newCustomStatus = newPresence.activities[0].state
        const oldCustomStatus = oldPresence.activities[0].state
        if(newCustomStatus != oldCustomStatus) {
            console.log('old status: ' + oldCustomStatus)
            console.log('new status: ' + newCustomStatus)
        }
    })
    

    不知道为什么它会触发不止一次,这个解决方案对我来说 100% 有效。

    【讨论】:

      猜你喜欢
      • 2020-11-17
      • 1970-01-01
      • 2021-03-26
      • 1970-01-01
      • 1970-01-01
      • 2019-10-18
      • 2022-01-15
      • 2019-05-14
      • 1970-01-01
      相关资源
      最近更新 更多