Currently, I am in the process of creating a bot with a unique feature - a config
command that enables users to customize specific functionalities within their servers. This customization is facilitated through a straightforward JSON file named config.json
, which stores the server ID along with various boolean variables representing the features:
{
"servers": [
{
"id": INSERT_ID,
"delete": true
},
{
"id": INSERT_ID,
"delete": true
},
{
"id": INSERT_ID,
"delete": false
}
]
}
The main objective is for the bot to search through this list when the config
command is executed and locate the server that matches the ID of the message sender's server. To achieve this functionality, I have devised the following code snippet:
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);
Interestingly, upon using console.log
on message.guild.id
, it accurately displays a number matching the strings stored within my JSON file. However, the IF statement
if (server.id === message.guild.id)
consistently returns false, leaving me puzzled as to the underlying cause.