【问题标题】:Why does mongoose crash my bot over this error?为什么猫鼬会因为这个错误使我的机器人崩溃?
【发布时间】:2021-10-12 18:41:25
【问题描述】:

我的机器人在每次使用 mongoose 时都会因这个错误而崩溃,并且不知道如何修复它。

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 执行此操作,并继续使我的机器人崩溃。我该如何解决这个问题?

【问题讨论】:

    标签: node.js mongoose discord eris


    【解决方案1】:

    问题是因为docnull,我假设是因为Guild 不存在与guildID 的任何msg.guildID 是什么。

    那么因为docnull,所以在你的代码中你正在执行doc.autorole 的任何地方都会出现Cannot read property 'autoRole' of null 错误。

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

    【讨论】:

    • 好吧,这是有道理的。我应该喜欢用类似的东西创建公会:``` const newGuild = await Guild.create({ // 然后我在我的模式中拥有的任何东西)} ``
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-01
    • 1970-01-01
    • 2021-03-25
    • 2018-09-09
    • 2019-01-13
    相关资源
    最近更新 更多