首页 > 解决方案 > 机器人状态临时更改

问题描述

我设置了 PresenceUpdate,因此当特定用户流式传输机器人状态更新并在频道中宣布时。它确实宣布并且工作正常,但机器人状态似乎只改变了约 20 秒,然后它会恢复到默认值。

我尝试使用以下方法修复它:

谁能帮我解决这个问题?这是我的代码:

client.on("presenceUpdate", (oldPresence, newPresence) => {
  let announcement = "Announcement Message";
  if (!newPresence.activities) return false;
  newPresence.activities.forEach(activity => {
      if (activity.type == "STREAMING") {
        if(newPresence.user.id == "ID OF USER STREAMING") {
          console.log(`${newPresence.user.tag} is streaming at ${activity.url}.`);
          client.channels.cache.get("My Channel ID").send(announcement);
          client.user.setActivity("to the server", { type: "STREAMING",  url: "Twitch Link"});
       };
      };
  });
});

编辑:好的,所以我设法通过删除如果他们没有流式传输状态返回默认值的部分来修复它。但是,现在即使用户不再流式传输,它也会卡在流式传输上。我该如何解决这个问题?代码也已更新。

标签: javascriptdiscorddiscord.js

解决方案


您可以将流用户的 id 存储在某处,然后检查是否是同一用户,只要存在更新。

这是一个例子:

if (client.streamingUserID !== undefined && client.streamingUserID !== newPresence.user.id) return;
newPresence.activities.forEach(activity => {
    if (activity.type == "STREAMING") {
        if(newPresence.user.id == "ID OF USER STREAMING") { 
            console.log(`${newPresence.user.tag} is streaming at ${activity.url}.`);
            client.channels.cache.get("My Channel ID").send(announcement);
            client.user.setActivity("to the server", { type: "STREAMING", url: "Twitch Link"});
            client.streamingUserID = newPresence.user.id;
        }; 
    } else {
        // set back activity to default and
        delete client.streamingUserID;
    }
});


推荐阅读