CodecademyCommunity / codecademy-discord-bot

Custom moderation bot for the official Codecademy Community Discord server.
https://codecademycommunity.github.io/
MIT License
11 stars 1 forks source link

Bot crashes in DM #119

Closed Shaylin-8bit closed 3 years ago

Shaylin-8bit commented 3 years ago

Describe the bug

In the event a user DMs the bot with a command that has some form of the isHighRoller() function. This is due to the fact that since the user is not considered part of a server at the time msg.members.role is equal to null.

To Reproduce

Steps to reproduce the behavior:

  1. DM the bot with cc!kick
  2. The bot will crash due to null variables.

Additional context

The bug is fixable by having the condition `if (!(msg.guild === null)) checked before the bot attempts to run commands.

aedwardg commented 3 years ago

Rather than having nested if blocks, it's probably best to just change the message listener in app.js to something like this:

client.on('message', (msg) => {
  if (message.channel.type === "dm") {
    return;
  }

  if (msg.content.substring(0, 3) === 'cc!' && !(msg.member === client)) {
    commandParser(msg);
  }
});

We could also put a response in before the return if we want.

aedwardg commented 3 years ago

Another example of how we might address this: https://discordjs.guide/command-handling/adding-features.html#guild-only-commands

It's fairly similar to what's discussed above.