(节点:14808)UnhandledPromiseRejectionWarning:TypeError:无法读取未定义的属性“角色”

Posted

技术标签:

【中文标题】(节点:14808)UnhandledPromiseRejectionWarning:TypeError:无法读取未定义的属性“角色”【英文标题】:(node:14808) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'role' of undefined 【发布时间】:2021-07-05 21:01:06 【问题描述】:

转到底部了解更多信息

warn file below
const  MessageAttachment  = require("discord.js");

module.exports = 名称:“警告”, 描述:“警告一个人”, 异步执行(客户端,消息,参数,不和谐) if (!message.member.hasPermission("KICK_MEMBERS")) return message.channel.send($message.authorYou Do Not Have Permission To Use This Command); if (!message.guild.me.hasPermission("KICK_MEMBERS")) return message.channel.send('我需要 `KICK_MEMBERS`\ 权限来管理用户警告');

    const warnRole1 = message.guild.roles.cache.find(role => role.name == '[Warning: 1]');
    const warnRole2 = message.guild.roles.cache.find(role => role.name == '[Warning: 2]');
    const warnRole3 = message.guild.roles.cache.find(role => role.name == '[Warning: 3]');
    const mentiondMember = message.guild.members.cache.get(args[0]) || message.mentions.members.first();
    let punishment = 1;
    let reson = args.slice(2).join(" ");
    if (!warnRole1) await message.guild.roles.create(
        data: 
            name: '[Warning: 1]',
            color: 'RED'

        
    ).catch(err => console.log(err));

    if (!warnRole1) await message.guild.roles.create(
        data: 
            name: '[Warning: 2]',
            color: 'RED'

        
    ).catch(err => console.log(err));

    if (!warnRole1) await message.guild.roles.create(
        data: 
            name: '[Warning: 3]',
            color: 'RED'

        
    ).catch(err => console.log(err));

    if (mentiondMember.role.cache.has(warnRole1.id)) punishment = 2;
    if (mentiondMember.role.cache.has(warnRole2.id)) punishment = 3;
    if (mentiondMember.role.cache.has(warnRole3.id)) punishment = 4;

    if (!args[0]) return message.channel.send(`You need to state a member along with if you are just checking, adding or removing warnings.`);
    if (!mentionedMember) return message.channel.send(`The Member Stated Is Not In The Server`);
    if (!reason) reason = 'No Reason Provided';

    if (!['add', 'remove'].includes(args[1])) 
        if (punishment == 1) 
            return message.channel.send(`$mentiondMember.user.tag has no warnings.`);
         else if (punishment == 2)
            return message.channel.send(`$mentiondMember.user.tag has one warnings.`);
         else if (punishment == 3)
            return message.channel.send(`$mentiondMember.user.tag has two warnings.`);
         else if (punishment == 4)
            return message.channel.send(`$mentiondMember.user.tag has three warnings.`);
        
    
    
    else 
        if (args[1] == 'add') 
            if (punishment == 1) 
                 await mentiondMember.roles.add(warnRole1.id).catch(err => console.log(err));
                 return message.channel.send(`$mentiondMember, you have been warned for: $reason`);
                
             else if (punishment == 2)
                await mentiondMember.roles.add(warnRole2.id).catch(err => console.log(err));
                await mentiondMember.roles.remove(warnRole1.id).catch(err => console.log(err));
                return message.channel.send(`$mentiondMember, you have been warned for: $reason`);
             else if (punishment == 3)
                await mentiondMember.roles.add(warnRole3.id).catch(err => console.log(err));
                await mentiondMember.roles.remove(warnRole2.id).catch(err => console.log(err));
                return message.channel.send(`$mentiondMember, you have been warned for: $reason`);
             else if (punishment == 4)
                message.channel.send(`THIS USER HAS REACHED MAX WARNINGS, CONTACT ADMIN/MANAGER/OWNER`)
                client.channels.cache.get(`822551294957846528`).send(`$mentiondMember, HAS REACHED MAX WARNINGS, IF YOU SEE THIS CONTACT AN ADMIN FOR PUNISHMENT`);
            
         else if (args[1] == 'remove') 
            if (punishment == 1) 
                return message.channel.send(`$mentiondMember.user.tag has no warnings to remove.`);
                
             else if (punishment == 2)
                await mentiondMember.roles.remove(warnRole1.id).catch(err => console.log(err));
                return message.channel.send(`I Removed a warning from $mentionedMember.user.tag`);
             else if (punishment == 3)
                await mentiondMember.roles.remove(warnRole2.id).catch(err => console.log(err));
                return message.channel.send(`I Removed a warning from $mentionedMember.user.tag`);
             else if (punishment == 4)
                await mentiondMember.roles.remove(warnRole1.id).catch(err => console.log(err));
                return message.channel.send(`I Removed a warning from $mentionedMember.user.tag`);
            

        
    

 

//!!warn @member [添加,删除] [原因]

full error: 

    (node:21096) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'role' of undefined
    at Object.execute (E:\Software\Github Repository shit\casey-discord-bot\commands\warn.js:41:28)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:21096) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:21096) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

关于事物的信息:我尝试使用本教程发出警告命令:https://www.youtube.com/watch?v=vk_FF887N0o。我一步一步地跟着它两次,它创建了一个角色,但之后它没有修复。它创造了这个角色,然后它表明我的其他东西是未定义的。如果我输入 !!warn (!! 是前缀)它将成为我在代码中声明的 3 个角色,但随后它给出了错误。我试着再做一次 !!warn ,它会产生更多的角色。我对为什么会发生这种情况有点困惑,如果您有任何想法,请告诉我。这个机器人也由 heroku 托管(如果这有什么帮助的话)。我还尝试在没有数据库的情况下创建角色命令,并且看起来它可以正常工作,只是因为某些奇怪的原因,它说角色和角色未定义。

机器人信息:这是一个简单的机器人,包括一个命令和事件处理程序(忽略部分,直到在代码顶部执行之后)。

【问题讨论】:

【参考方案1】:

在查看了一些代码和错误消息之后。

我得出的结论是问题出在这一行:

if (mentiondMember.role.cache.has(warnRole1.id)) punishment = 2;
if (mentiondMember.role.cache.has(warnRole2.id)) punishment = 3;
if (mentiondMember.role.cache.has(warnRole3.id)) punishment = 4;

问题 1:

变量“mentiondMember”是未定义,您可能需要检查您的:

const mentiondMember = message.guild.members.cache.get(args[0]) || message.mentions.members.first();

并确保它们返回有效的 GuildMember 对象。

提示: 如果您使用的是调试器,您可以在“mentiondMember”附近放置一个断点以轻松检查变量!

问题 2:

您正试图从“mentiondMember”访问一个不存在的对象“角色”!

我不确定您的 Discord.JS 版本,但在 Discord.JS 版本 12.5.3 的文档中:

A GuildMember doesn't have "role" properties, instead they have the "roles" properties !

所以我认为您需要将该行更改为:

if (mentiondMember.roles.cache.has(warnRole1.id)) punishment = 2;
if (mentiondMember.roles.cache.has(warnRole2.id)) punishment = 3;
if (mentiondMember.roles.cache.has(warnRole3.id)) punishment = 4;

【讨论】:

以上是关于(节点:14808)UnhandledPromiseRejectionWarning:TypeError:无法读取未定义的属性“角色”的主要内容,如果未能解决你的问题,请参考以下文章

将 NodeJS Docker 容器连接到 MySQL Docker 容器 - docker-compose 3

7.2 Models -- Defining Models

不在models.py中的models

auto_cmdb--01之models.py建表

用Diffusion Models实现image-to-image转换

7.4 Models -- Pushing Records into the Store