gpt4 book ai didi

javascript - discord.js v12 检查用户是否开始流式传输

转载 作者:行者123 更新时间:2023-12-04 08:38:38 29 4
gpt4 key购买 nike

这按预期工作,直到它崩溃。每次用户开始流式传输时,它都会发布他们的流链接。
问题是,大约 5 分钟后,机器人崩溃了,我收到以下错误。

let oldStreamingStatus = oldMember.activities.find(activity =>  activity.type  === 'STREAMING') ? true : false;
^
TypeError: Cannot read property 'activities' of undefined
我的代码如下。
client.on('presenceUpdate', (oldMember, newMember) => {

let oldStreamingStatus = oldMember.activities.find(activity => activity.type === 'STREAMING') ? true : false;
let newStreamingStatus = newMember.activities.find(activity => activity.type === 'STREAMING') ? true : false;

if (oldStreamingStatus == newStreamingStatus) {
return;
}

if (newStreamingStatus) {
if (newMember.member.roles.cache.some(r => ["Raider","Trial"].includes(r.name))) {
const activity = newMember.activities.find(activity => activity.type === 'STREAMING')
// console.log(newMember.activities)
client.channels.cache.get("731911793257087006").send(`Hey! ${activity.url} is streaming!`);
return;
} else
return;
}});
我在下面尝试了这段代码。但是,如果有人更改了他们的事件或者他们当前正在流式传输然后开始收听 Spotify,它会发布另一条消息。
  client.on("presenceUpdate", (oldPresence, newPresence) => {

if (!newPresence.activities) return false;

newPresence.activities.forEach(activity => {
if (activity.type == "STREAMING") {
console.log(`${newPresence.user.tag} is streaming at ${activity.url}.`);
};
});
});
似乎我需要 10 个声望才能以正确的方式发布图片。对此我深表歉意。

最佳答案

如果我们遵循 discord.js 文档,我们可以看到 'oldMember' 是可选的:
enter image description here
和异常消息说:

Cannot read property 'activities' of undefined


添加额外检查:
client.on('presenceUpdate', (oldMember, newMember) => {
if(!oldMember){
//skip actions or get streaming status based only on newMember activities...
}
在您的其余代码之前。

关于javascript - discord.js v12 检查用户是否开始流式传输,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64671429/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com