(节点:14808)未处理的承诺拒绝警告:类型错误:无法读取未定义的属性“角色”

时间:2021-04-10 02:30:03

标签: node.js heroku discord discord.js heroku-api

转到底部了解更多信息

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

module.exports = { name: "警告", 描述:“警告一个人”, 异步执行(客户端,消息,参数,不和谐){ if (!message.member.hasPermission("KICK_MEMBERS")) 返回 message.channel.send(${message.author}You 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}`);
            }

        }
    }

 }

}

//!!警告@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 个答案:

答案 0 :(得分:0)

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

我得出的结论是问题出在这条线上:

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;