简体   繁体   English

discord.js v13 类型错误:无法读取未定义的属性(读取“存在”)

[英]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.我想和我的朋友开玩笑命令禁止正在玩 Roblox 的人或者他是否具有 Roblox 游戏状态。 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.如果有解决方案,我已经搜索了 inte.net 和堆栈溢出中的所有内容,但我找不到一个有效的解决方案。 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我不讨厌玩 Roblox 的家伙,他们只是在闲逛,哈哈

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. PS:我已经在 discord 开发网站和 index.js 文件中启用了意图,并且还使用了<member>.presence.activities但看起来只有用户具有存在属性。

Well, the problem is that GuildMember#presence returns or a Presence class OR undefined , because it's an optional property.那么,问题是GuildMember#presence返回或Presence class OR undefined ,因为它是一个可选属性。 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.您在代码中所做的是获取可能不存在的用户的存在属性,这就是为什么allMember.user.presence返回 undefined 和allMember.user.presence.activities[0]的原因。 Also keep in mind that you take the User of a member, but presences go on GuildMembers and not on Users.另请记住,您使用成员的用户,但在 GuildMembers 而不是用户上存在 go。

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.只需使用allMember而不是allMember.user ,这样您就可以使用GuildMember而不是User的存在,此后具有存在的 Member 也将返回存在数据。
  • Add a if statement that controls if a member has a presence or not, so you don't run in the same error again.添加一个 if 语句来控制成员是否存在,这样您就不会再次遇到相同的错误。
  • 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.我不知道你是否对此有异议,但是如果一个成员有多个存在并且阵列中的第一个存在不是 Roblox,但是例如阵列中的第二个存在是 Roblox,则该成员不会被禁止.

I hope this helps you, if you have questions just contact me on Discord ~ Iliannnn#0001我希望这对你有帮助,如果你有任何问题,请联系我 Discord ~ Iliannnn#0001

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeError:无法在 discord.js v13 中读取未定义的属性(读取“路径”) - TypeError: Cannot read properties of undefined (reading 'path') in discord.js v13 discord.js v13 - 类型错误:无法读取未定义的属性(读取“正常运行时间”) - discord.js v13 - TypeError: Cannot read properties of undefined (reading 'uptime') 类型错误:无法读取未定义的属性(读取“设置”)discord.js v13 斜杠命令处理程序 - TypeError: Cannot read properties of undefined (reading 'set') discord.js v13 slash commands handler 类型错误:无法读取未定义的属性(读取“获取”)Discord.js v13 - TypeError: Cannot read properties of undefined (reading 'fetch') Discord.js v13 discord.js v13 permissions.has() function 不工作(TypeError: Cannot read properties of undefined (reading 'has')) - discord.js v13 permissions.has() function not working (TypeError: Cannot read properties of undefined (reading 'has')) 无法读取未定义的属性(读取“权限”)。 | Discord.js v13 - Cannot read properties of undefined (reading 'permissions'). | Discord.js v13 Discord.js v13 无法读取属性 o 未定义的读取发送 - Discord.js v13 cannot read properties o undefined reading send 无法读取未定义的属性(读取“发送”)(Discord.js v13) - Cannot read properties of undefined (reading 'send') (Discord.js v13) 无法在 Discord.js V13 中读取未定义的属性(读取“删除”) - Cannot read properties of undefined (reading 'delete') in Discord.js V13 Discord JS V13 member.presence.status(无法读取 null 的属性(读取“状态”)) - Discord JS V13 member.presence.status (Cannot read properties of null (reading 'status'))
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM