我想在某人玩耍时使用presenceUpdate
事件添加角色
但是每当我尝试时,在discord.js v12中都会收到此错误
TypeError: Cannot read property 'activities' of undefined
我在做什么错了?
这是我的代码:
const yetkiler = require('../yetkiler.json');
module.exports = (oldPresence, newPresence) => {
const guild = newPresence.guild;
const oyunda = guild.roles.cache.find(role => role.name === yetkiler.oyunda);
const yayında = guild.roles.cache.find(role => role.name === yetkiler.yayında);
const dinliyor = guild.roles.cache.find(role => role.name === yetkiler.dinliyor);
const eskiOyun = oldPresence.presence.activities && [0, 1, 2, 3].includes(oldPresence.presence.activities.type) ? true : false;
const yeniOyun = newPresence.presence.activities && [0, 1, 2, 3].includes(newPresence.presence.activities.type) ? true : false;
if (!eskiOyun && yeniOyun) { //
if(newPresence.presence.activities.type === 0){
newPresence.roles.add(oyunda)
.then(() => console.log(`${oyunda.name} added to ${newPresence.user.tag}.`))
.catch(console.error);
}
else if(newPresence.presence.activities.type === 1){
newPresence.roles.add(yayında)
.then(() => console.log(`${yayında.name} added to ${newPresence.user.tag}.`))
.catch(console.error);
}
else if(newPresence.presence.activities.type === 2){
newPresence.roles.add(dinliyor)
.then(() => console.log(`${dinliyor.name} added to ${newPresence.user.tag}.`))
.catch(console.error);
}
} else if (eskiOyun && !yeniOyun) { //
newPresence.roles.remove(oyunda) && newPresence.roles.remove(yayında) && newPresence.roles.remove(dinliyor)
.then(() => console.log(`${playingRole.name} removed from ${newPresence.user.tag}.`))
.catch(console.error);
}
};
答案 0 :(得分:0)
根据documentation oldPresence
和newPresence
已经是Presence类型。因此,您想将newPresence.presence.activities
/ oldPresence.presence.activities
的所有实例替换为newPresence.activities
/ oldPresence.activities