If 语句在不应该触发的时候触发
If statement firing when it shouldn't
如果没有带有公会 ID 的命令,第 14 行应该触发,
如果用户在该公会中没有命令,第 15 行应该触发,
但是
!commands @Rusty#2746
returns No commands in this guild.
在服务器 Go away
中。 (我在这个服务器中有一个命令)
!commands @Rusty#2746
returns 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);
});
}
});
如果解决方案对其他人有帮助,我在聊天中与 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);
});
});
如果没有带有公会 ID 的命令,第 14 行应该触发,
如果用户在该公会中没有命令,第 15 行应该触发, 但是
!commands @Rusty#2746
returns No commands in this guild.
在服务器 Go away
中。 (我在这个服务器中有一个命令)
!commands @Rusty#2746
returns 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);
});
}
});
如果解决方案对其他人有帮助,我在聊天中与 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);
});
});