javascript - 机器人状态临时更改
问题描述
我设置了 PresenceUpdate,因此当特定用户流式传输机器人状态更新并在频道中宣布时。它确实宣布并且工作正常,但机器人状态似乎只改变了约 20 秒,然后它会恢复到默认值。
我尝试使用以下方法修复它:
- 循环
- While 循环
- 递归函数
谁能帮我解决这个问题?这是我的代码:
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"});
};
};
});
});
编辑:好的,所以我设法通过删除如果他们没有流式传输状态返回默认值的部分来修复它。但是,现在即使用户不再流式传输,它也会卡在流式传输上。我该如何解决这个问题?代码也已更新。
解决方案
您可以将流用户的 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;
}
});
推荐阅读
- vue.js - Vue CLI 构建到外部主机?
- java - 如何使用 jdbc 显示列表视图
- android - 如何在 Kotlin 中使用 JUnit Mockito 测试/模拟 API
- android - 有没有办法快速创建不同的屏幕布局?
- php - PHP 将表情符号保留为 unicode,但也将文本保留为纯文本
- docker - 如何将 dockerfile 添加到部署到 gitlab 的 jekyll
- python - 如何仅打印解决方案取决于初始猜测的优化问题的唯一解决方案?
- excel - 如何修复“无法运行宏”错误?
- python - 如何加速这种线性搜索算法?
- javascript - Repeater.js 使用多个嵌套形式。添加按钮将字段添加到所有