SirArchibald
SirArchibald

Reputation: 516

Comparing a Guild ID to an identical string

As part of a bot I am making, I am making a config command which allows users to toggle certain features in their servers. To do this, I am using a simple JSON file called config.json to store the ID of the server along with several boolean variables to represent the features:

{
  "servers": [
    {
      "id": INSERT_ID,
      "delete": true
    },
    {
      "id": INSERT_ID,
      "delete": true
    },
    {
      "id": INSERT_ID,
      "delete": false
    }
  ]
}

I am trying to get it so when the config command is run, the bot looks through this list to find the server which matches the ID of the server the message came from. I have made this code to do this:

let data = fs.readFileSync(__dirname + "/config.json");
let config = JSON.parse(data);

let found = false;
for(let server of config.servers) {
    if (server.id === message.guild.id.toString()) {
        found = true;
        if (server.delete) {
            server.delete = false;
            message.reply("`Message Delete` has been toggled to `False`");
        } else {
            server.delete = true;
            message.reply("`Message Delete` has been toggled to `True`");
        }
    }
}
if (!found) {
    message.reply("I couldn't find this server in the database!");
}

let newData = JSON.stringify(config)
fs.writeFileSync(__dirname + "/config.jsom", newData);

If I console.log the message.guild.id it prints a number which matches the strings stored in my JSON file, however the IF statement if (server.id === message.guild.id) evaluates to false and I am not sure why.

Upvotes: 0

Views: 252

Answers (1)

Cannicide
Cannicide

Reputation: 4520

I know your question has already been answered in the comments, but I just wanted to mention a different way of achieving the same result (and I also wanted to post a full answer to the question).

Instead of using a for/of loop to find the item that matches your specified criteria (matching guild IDs), you can simply utilize arrays' built-in find method to do so. Here's an example of how that would look in your code:

let found = config.servers.find(server => server.id == message.guild.id);

if (found) {
    if (found.delete) {
        found.delete = false;
        message.reply("`Message Delete` has been toggled to `False`");
    } else {
        found.delete = true;
        message.reply("`Message Delete` has been toggled to `True`");
    }
}
else {
    message.reply("I couldn't find this server in the database!");
}

I personally find this method to be cleaner than using the for/of loop by reducing the overall number of embedded conditionals/loops. And this code functions the same as your current code (in fact, the method probably utilizes a for/of loop itself). Of course, the main solution to the problem you were having is that you should use == instead of === due to their differing meanings in javascript, and it is also worth noting that you do not need to call toString() on message.guild.id when using ==.

Upvotes: 1

Related Questions