0

在使用猫鼬时,我的机器人每次出现此错误时都会崩溃,只是不知道如何修复它。

0|Trinity  | [07:26:07] Cluster 0 | [Aug 08 2021 07:26:07] Rethink log: TypeError: Cannot read property 'autoRole' of null
0|Trinity  |     at /root/TrinityVPS/src/events/guildMemberAdd.js:7:20
0|Trinity  |     at immediate (/root/TrinityVPS/node_modules/mongoose/lib/model.js:4870:18)
0|Trinity  |     at process._tickCallback (internal/process/next_tick.js:61:11)
0|Trinity  | [07:26:07] Cluster Manager | cluster 0 disconnected
0|Trinity  | [07:26:07] Cluster Manager | cluster 0 died

这是我的代码:

const Guild = require('../db/models/Guild');

exports.handle = async function (guild, member) {

    try {
        Guild.findOne({ guildID: guild.id }, function (err, doc) {
            if(doc.autoRole.enabled) {
                if(doc.autoRole.role) {
                    member.addRole(guild.roles.find(rl=> rl.name === doc.autoRole.role).id, "Trinity's autorole.")
                }
            }

            if(doc.logsChannel.enabled) {
                if(doc.logsChannel.channelID) {
                    this.bot.createMessage(doc.logsChannel.channelID, {
                        embed: {
                            author: {
                                name: `${member.username}#${member.discriminator}`,
                                icon_url: member.avatarURL
                            },
                            color: 0x2196F3,
                            timestamp: new Date(),
                            fields: [
                                {
                                    name: "Member Joined",
                                    value: `**${member.username}** has joined **${guild.name}**`
                                }
                            ]
                        }
                    })
                }
            }
        })
    } catch (err) { }
}

这是自动角色代码:

async function autoroleCommand (msg, args) {
    // const db = await this.db.getGuild(msg.channel.guild.id);
    // const prefix = db ? db.prefix : this.config.options.prefix;

    Guild.findOne({ guildID: msg.guildID }, function (err, doc) {
        let auto;

        if (!doc.autoRole.enabled) auto = `Status: deactivated.`;
        else auto = `Status: active.`

        let roleid;

        if (!doc.autoRole.role) roleid = `No role has being set.`
        else roleid = `Role is set to: ${doc.autoRole.role}`

        if (!args[0]) {
            return msg.channel.createMessage({
                embed: {
                    color: 0x2196F3,
                    description: `Autorole configuration for this server:\n${auto}\n${roleid}`
                }
            });
        }

        switch(args[0]) {

            case 'role': {
                var role = args.slice(1).join(' ');
                if(!msg.member.permissions.has('manageGuild') && !this.config.options.devs.includes(msg.author.id)) return msg.channel.createMessage('You are missing the `manageGuild` permission!<:notdone:334852376034803714>');
                if(role.length === 0) return msg.channel.createMessage('Role is a required argument!\nUsage: `autorole role <role>`');
                if(!msg.channel.guild.roles.find(rl => rl.name === role)) return msg.channel.createMessage(`I couldn't find the role \`${role}\`. <:notdone:334852376034803714>`);

                if(role === doc.autoRole.role) {
                    return msg.channel.createMessage('That role is already set as the autorole.')
                } else {
                    doc.autoRole.role = role;
                    doc.save();

                    msg.channel.createMessage('Role has been set! :ok_hand:');
                }
                break;
            }

            case 'disable': {
                if(!msg.member.permissions.has('manageGuild') && !this.config.options.devs.includes(msg.author.id)) return msg.channel.createMessage('You are missing the `manageGuild` permission!<:notdone:334852376034803714>');

                if(doc.autoRole.enabled === false) {
                    return msg.channel.createMessage('Autorole is already disabled.')
                } else {
                    doc.autoRole.enabled = false;
                    doc.save().then(async () => {
                        await msg.channel.createMessage('Successfully disabled autorole. To enable autorole: `autorole enable`')
                    })
                }
                break;
            }

            case 'enable': {
                if(!msg.member.permissions.has('manageGuild') && !this.config.options.devs.includes(msg.author.id)) return msg.channel.createMessage('You are missing the `manageGuild` permission!<:notdone:334852376034803714>');

                if(doc.autoRole.enabled === true) {
                    return msg.channel.createMessage('Autorole is already true.')
                } else {
                    doc.autoRole.enabled = true;
                    doc.save().then(async () => {
                        await msg.channel.createMessage('Successfully enabled autorole. To disable autorole: `autorole disable`')
                    })
                }
                break;
            }

            case 'help':
                msg.channel.createMessage('To set autorole, use the following command: `autorole role <role>`\nTo enable autorole: `autorole enable`\nTo disable autorole: `autorole disable`')
        }
    })
}

我不明白,因为在我的代码中我确保检查它是否启用然后继续,但它只是崩溃了。它也为我执行此操作doc.logsChannel.channelID并继续使我的机器人崩溃。我怎样才能解决这个问题?

4

1 回答 1

1

问题是因为docnull,我假设是因为无论是什么都不Guild存在。guildIDmsg.guildID

然后因为docnull,在你的代码中你正在做的任何地方doc.autorole都会出错Cannot read property 'autoRole' of null

基本上你需要决定什么时候你想做什么doc(当没有找到null给定的公会时)。guildID也许您只是想返回某种错误消息。无论您决定什么,您都无法访问nullvalue 的属性。

于 2021-08-09T20:09:16.160 回答