简体   繁体   中英

discord.js v13 TypeError: Cannot read properties of undefined (reading 'presence')

I'm trying to make a joke command with my friends to ban people who are playing Roblox or if he has the Roblox game status. I've searched everything in the inte.net and in stack overflow if there's a solution but I can't find a single working one. Here's my code:

module.exports = async (client) => {
    const guild = client.guilds.cache.get('877016354581004389');
    if(guild){
        setInterval(async () => {
            if(!guild.me.permissions.has('ADMINISTRATOR')) return;
            const channel = guild.channels.cache.get('877016354581004392');
            if(!channel) return console.log('ANNOUNCEMENT CHANNEL NOT FOUND...')
            let bannedAmount = 0;
            await guild.members.fetch().then(members => {
                members.forEach(member => {
                    for(const allMember of Object.keys(member)){
                        if(allMember.user.presence.activities){
                            const activity = allMember.user.presence.activities[0]
                            if(activity.name.toLowerCase() == "roblox"){
                                channel.send(`${allMember} got banned because he's playing ROBLOX!!`)
                                allMember.ban({ reason: `he played roblox lol`});
                                bannedAmount = bannedAmount + 1
                                break;
                            }
                        }
                    }
                })
            });

            let members;
            if(bannedAmount > 1) members = 'members';
            let kid;
            if(bannedAmount > 1) kid = 'kids';

            if(bannedAmount <= 0 || null){
                console.log(`No member banned for playing Roblox...`);
            } else if(bannedAmount > 0) {
                channel.send(`${bannedAmount} ${kid || 'kid'} got banned in this server for playing Roblox. Don't do it kids!`)
                console.log(`${bannedAmount} ${members || 'member'} got banned for playing Roblox...`)
            }
        }, 20000);
    }
}

I have no hate with the game Roblox guys just fooling around lol

PS: I have already enabled intents in both discord dev website and also in the index.js file and also had used <member>.presence.activities but it looks like only user has presence property.

Well, the problem is that GuildMember#presence returns or a Presence class OR undefined , because it's an optional property. What you do in your code is taking the presence property of a user who probably doesn't has any presence and that's why allMember.user.presence returns undefined and allMember.user.presence.activities[0] too. Also keep in mind that you take the User of a member, but presences go on GuildMembers and not on Users.

Debugging:

  • Just use allMember instead of allMember.user , so u take the presence of a GuildMember instead of a User , after this a Member who has a presence will return the presence data too.
  • Add a if statement that controls if a member has a presence or not, so you don't run in the same error again.
  • I don't know if you tought about this, but if a member has more than one presence and the first presence in the array isn't Roblox, but for example the second presence in the array is Roblox the member won't get banned.

I hope this helps you, if you have questions just contact me on Discord ~ Iliannnn#0001

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM