如果声明在不应该

时间:2017-04-23 21:16:40

标签: javascript json node.js fs

如果没有带有公会ID的命令

,则应触发第14行

如果用户在该公会中没有命令,则会触发第15行, 但是

!commands @Rusty#2746会在服务器No commands in this guild.中返回Go away。 (我在这个服务器上有一个命令)

!commands @Rusty#2746返回No commands in this guild. AND 服务器SurgicalGoblin中的实际命令。 (我在这个服务器上有一个命令)

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

1 个答案:

答案 0 :(得分:0)

如果该解决方案对其他人有帮助,我在聊天中与OP讨论了这个问题并发现问题发生是因为OP希望将当前的message公会与相应的公会进行比较在JSON数据中。因此,我建议完全取出for循环,然后首先找到公会(使用find()),然后进行比较:

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.");

  var guild = arrayOfObjects.commands.find(function(obj) { return obj.guild_id == message.guild.id; });

  if (message.guild.id !== guild.guild_id) return message.reply("No commands in guild.");
  if (message.guild.id !== guild.guild_id && user.id !== guild.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(guild.command_name, guild.command_reply)
       return message.channel.sendEmbed(embed);
    });

});
相关问题