我设置了PresenceUpdate,因此当特定用户流式传输bot状态更新并在频道中进行公告时。它确实宣布了,并且运行良好,但是机器人状态似乎仅更改了约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"});
};
};
});
});
编辑:好的,因此,我设法删除了一部分(如果他们不进行流式传输,则状态会恢复为默认值)来进行修复。但是,现在即使用户不再流媒体,它仍然停留在流媒体上。我该如何解决?该代码也已更新。
答案 0 :(得分:0)
您可以将流用户的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;
}
});