Sunday, 23 April 2017

If statement firing when it shouldn't

Line 14 should fire if there are no commands with the guild id,

Line 15 should fire if the user has no commands in that guild, but

!commands @Rusty#2746 returns No commands in this guild. in the server Go away. (I have a command in this server)

!commands @Rusty#2746 returns No commands in this guild. AND the actual command in the server SurgicalGoblin. ( I have a command in this server)

let user = message.mentions.users.first();
if (args.length !== 1) return message.reply("Incorrect arguments, " + args.length + " given, 1 required.");
if (message.mentions.users.size !== 1) return message.reply("You must mention a user to view the commands of.");

fs.readFile(path.join(__dirname, "../jsonFiles") + "/customCommands.json", "utf-8", function(err, data) {
  if (err) throw err;

  var arrayOfObjects = JSON.parse(data);

  if (arrayOfObjects.commands.length === 0) return message.reply("No custom commands found.");

  for (let i = 0; i < arrayOfObjects.commands.length; i++) {
  console.log(message.guild.id) // matches the guild_id in json file
    if (message.guild.id !== arrayOfObjects.commands[i].guild_id) return message.reply("No commands in guild.");
    if (message.guild.id !== arrayOfObjects.commands[i].guild_id && user.id !== arrayOfObjects.commands[i].user_id) return message.reply(user.username + " has no commands in this guild.");

    fs.writeFile(path.join(__dirname, "../jsonFiles") + "/customCommands.json", JSON.stringify(arrayOfObjects, null, 2), "utf-8", function(err) {
      if (err) throw err;
      const embed = new Discord.RichEmbed()
      .setColor(0x08F258)
      .setAuthor("Custom Commands for " + user.username, message.author.avatarURL)
      .addField(arrayOfObjects.commands[i].command_name, arrayOfObjects.commands[i].command_reply)
       return message.channel.sendEmbed(embed);
    });
  }
});

JSON: https://hastebin.com/zucogajita.json



via Rusty

No comments:

Post a Comment