Bot crashed because of message with no 'guild' property

My bot crashed on a messageCreate event and I don't really understand why. I filter out DM messages, and then check message.guild.id because my bot is made for two specific servers. However, 'guild' was null so I got a TypeError when trying to access 'id'. My code looks something like this:
client.on('messageCreate', (message) => {
// ignore any messages from bots or people blocked from interacting with the bot
if (message.author.bot) { return; }
if (botBlocked.includes(message.author.id)) { return; }

// handle DMs
if (message.channel.type === 'DM') {
return botDMs.handleDMs(message, client);

// handle messages in servers the bot is available in
} else if (Object.values(serversServed).includes(message.guild.id)) {
// do bot stuff
}
}
client.on('messageCreate', (message) => {
// ignore any messages from bots or people blocked from interacting with the bot
if (message.author.bot) { return; }
if (botBlocked.includes(message.author.id)) { return; }

// handle DMs
if (message.channel.type === 'DM') {
return botDMs.handleDMs(message, client);

// handle messages in servers the bot is available in
} else if (Object.values(serversServed).includes(message.guild.id)) {
// do bot stuff
}
}
The only thing that changed is that recently I switched from using Heroku to a server specifically for the bot. Heroku restarted the bot on a daily basis, this server doesn't. This happened after ~3 days of uninterrupted runtime, could that be related? Or is there a channel type (which bots can participate in) that isn't DMs but that also doesn't have a guild property?
2 Replies
Unknown User
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
Wawajabba
Wawajabba•2y ago
14.6.0, and node 16.15.0 It doesn't happen continuously, this was after about 3 days. And the server has a lot of messages every day I guess I could wrap the entire thing in a big try/catch and log any errors... Oooh, that could be it. I haven't tried sending any DMs to the bot since updating I'll check if that's the problem, thanks! I updated a while ago so it's just been luck that prevented it from crashing for this long... Right on the money, thank you 🙂