TypeError: Cannot read property 'join' of undefined discord bot js say command

Viewed 752

So I am making a discord bot and here the problem :

if(cmd === `${prefix}say`){
    if (!message.member.hasPermission("MANAGE_MESSAGE")) return message.reply("cant use this command");
    let botmessage = args.join(" ");
    message.delete().catch();
    message.channel.send(botmessage)
    for(var item=0; item<re.length; item++) 
    {}

and when I open the .bat to execute the bot it say : TypeError: Cannot read property 'join' of undefined

1 Answers

You have to define args first. And how did you define cmd? That doesn't look really convenenient if you have to put ${prefix} everytime in your command. You may want to change it aswell. Put this piece of code somewhere high up in your code

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

(and don't forget to remove your own definition of "cmd"!)

You can then simply use commands this way:

if (cmd === "say") {
  if (!message.member.hasPermission("MANAGE_MESSAGE")) return message.reply("cant use this command");
  let botmessage = args.join(" ");
  message.delete().catch();
  for (var item=0; item<re.length; item++) {
    // whatever there should go in
  };
};
Related