Reputation: 35
My bot crashes everytime on this error, when using mongoose and just cannot know how to fix it.
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
This is my code:
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) { }
}
This is autorole code:
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`')
}
})
}
I don't understand as in my code i made sure to check if its enabled then proceeds, but it just crashes. It also does this for my doc.logsChannel.channelID
and proceeds to crash my bot. How can i fix this?
Upvotes: 0
Views: 96
Reputation: 937
The issue is because doc
is null
, i'm assuming because no Guild
exists with the guildID
of whatever msg.guildID
is.
Then because doc
is null
, anywhere in your code where you are doing doc.autorole
will error with Cannot read property 'autoRole' of null
.
Basically you need to decide what you want to do when doc
is null
(when a Guild with the given guildID
is not found). Perhaps you simply want to return with some kind of error message. Whatever you decide, you cannot access properties of a null
value .
Upvotes: 1