Search code examples
node.jsdiscord.js

How do I code a discord bot not stop after a incorrect command


So anytime I send an incorrect command like !pong (where!ping is the correct one) the bot returns with the following error:

(if(command.permissions.length){ ^

TypeError: Cannot read property 'permissions' of undefined

even if I remove the whole permissions code it then gives me a

TypeError: Cannot read property 'users' of undefined

and it's only when I have a wrong command of that of !pong when there is no command or !me anything with the prefix that isn't a actual command. below is the code im using

Message.js

require('dotenv').config();

const cooldowns = new Map();

module.exports = (Discord, client, message) =>{ const prefix = process.env.PREFIX; if(!message.content.startsWith(prefix) || message.author.bot) return;

const args = message.content.slice(prefix.length).split(/ +/);
const cmd = args.shift().toLowerCase();

const command = client.commands.get(cmd) || client.commands.find(a => a.aliases && a.aliases.includes(cmd));
const validPermissions = [
    "CREATE_INSTANT_INVITE",
    "KICK_MEMBERS",
    "BAN_MEMBERS",
    "ADMINISTRATOR",
    "MANAGE_CHANNELS",
    "MANAGE_GUILD",
    "ADD_REACTIONS",
    "VIEW_AUDIT_LOG",
    "PRIORITY_SPEAKER",
    "STREAM",
    "VIEW_CHANNEL",
    "SEND_MESSAGES",
    "SEND_TTS_MESSAGES",
    "MANAGE_MESSAGES",
    "EMBED_LINKS",
    "ATTACH_FILES",
    "READ_MESSAGE_HISTORY",
    "MENTION_EVERYONE",
    "USE_EXTERNAL_EMOJIS",
    "VIEW_GUILD_INSIGHTS",
    "CONNECT",
    "SPEAK",
    "MUTE_MEMBERS",
    "DEAFEN_MEMBERS",
    "MOVE_MEMBERS",
    "USE_VAD",
    "CHANGE_NICKNAME",
    "MANAGE_NICKNAMES",
    "MANAGE_ROLES",
    "MANAGE_WEBHOOKS",
    "MANAGE_EMOJIS",
  ]

  if(command.permissions.length){
    let invalidPerms = []
    for(const perm of command.permissions){
      if(!validPermissions.includes(perm)){
        return console.log(`Invalid Permissions ${perm}`);
      }
      if(!message.member.hasPermission(perm)){
        invalidPerms.push(perm);
      }
    }
    if (invalidPerms.length){
      return message.channel.send(`Missing Permissions: \`${invalidPerms}\``);
    }
  }



if(!cooldowns.has(command.name)){
    cooldowns.set(command.name, new Discord.Collection());
}

const current_time = Date.now();
const time_stamps = cooldowns.get(command.name);
const cooldown_ammount = (command.cooldown) * 1000;

if(time_stamps.has(message.author.id)){
    const expiration_time = time_stamps.get(message.author.id) + cooldown_ammount;

    if(current_time < expiration_time){
        const time_left = (expiration_time - current_time) / 1000;

        return message.reply(`Please wait ${time_left.toFixed(1)} more seconds before using ${command.name}`)
    }
}

time_stamps.set(message.author.id, current_time);
setTimeout(() => time_stamps.delete(message.author.id), cooldown_ammount);

try{
    command.execute(message,args, cmd, client, Discord);
} catch (err){
    message.reply("There was an error trying to execute this command!");
    console.log(err);
}
 }

ping.js

module.exports = { name: 'ping', cooldown: 10, permissions: ["SEND_MESSAGES",], description: "this is a ping command!", execute(client, message, args, cmd, Discord){ message.channel.send('pong!'); } }


Solution

  • In your message.js, after the declaration of command, you need to check if it’s undefined or null

    const command = client.commands.get(cmd) || client.commands.find(a => a.aliases && a.aliases.includes(cmd));
    if(!command) return;
    

    Edit: your error comes from here:

    command.execute(message,args, cmd, client, Discord);
    //^^how you execute
    execute(client, message, args, cmd, Discord){ message.channel.send('pong!'); }
    //^^how you define it
    

    So as you can see, you put them in the wrong order.