有人玩Discord.js v12时的自动角色

时间:2020-05-04 16:56:48

标签: javascript node.js discord discord.js

我想在某人玩耍时使用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);
    }
  };

1 个答案:

答案 0 :(得分:0)

根据documentation oldPresencenewPresence已经是Presence类型。因此,您想将newPresence.presence.activities / oldPresence.presence.activities的所有实例替换为newPresence.activities / oldPresence.activities