这按预期工作,直到崩溃。每次用户开始直播时,它都会发布他们的直播链接。问题是,大约 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 声望才能以正确的方式发布图片。我对此深表歉意。
1 回答
catspeake
TA贡献1111条经验 获得超0个赞
如果我们遵循discord.js文档,我们可以看到“oldMember”是可选的:
异常消息显示:
无法读取未定义的属性“活动”
添加额外检查:
client.on('presenceUpdate', (oldMember, newMember) => {
if(!oldMember){
//skip actions or get streaming status based only on newMember activities...
}
在其余代码之前。
添加回答
举报
0/150
提交
取消