1

I have a discord bot and I want to have an array that has the user ids of people who abuse the volume and music commands so that I can take away their abilities and give them back using commands like !nomusic and !musicback, but I have no idea how I would make it add or remove their ids from an array in the config file. My best guess is to use fs and have it push the member's id into the array, but I have no idea how I would go about doing this (I'm very new to node.js and especially fs, so sorry if this is a really easy thing to do and is really dumb to ask)

So far this is how far I've gotten(lots of the program not included so it's easier to read)

function readNoMusicJSON() {
    return JSON.parse(fs.readFileSync("./nomusic.json"));
}

var badmusicusers = readNoMusicJSON();

function nomusicsfoyou(badmusicusers, userId) {
    return nomusic.concat([userId]);
}

function saveNoMusicFile(badmusicusers) {
    fs.writeFileSync("./nomusic.json");
}
bot.on('message', async message => {
//some code ommited due to lack of importance
var args = message.content.slice(config.prefix.length).trim().split(/ +/g);    
var command = args.shift().toLowerCase();
switch(command){
    case"music":
        if(badmusicusers.find(id=>id == message.author.id)) return;
        // more ommitted code that don't matter
        break;
    case "nomusic":
        let sadmusicboi = message.mentions.members.first();
        badmusicusers = nomusicsfoyou((badmusicusers, sadmusicboi.id));
        saveNoMusicFile(badmusicusers);
        break;
    }
})
1
  • Can you share what you have tried so far? Any example of the object/array you want to manipulate? Adding a string to an object is as simple as: exampleObject.thing = '!nomusic'; Commented Nov 6, 2017 at 3:31

1 Answer 1

3

Suppose you have a list of bannedUsers in your config bannedUsers.json file.

["user-id-1", "user-id-2"]

On Startup of the program, you read the file into a variable:

function readBannedUsers() {
    return JSON.parse(fs.readFileSync("./bannedUsers.json"));
}

var bannedUsers = readBannedUser();

Whenever you want to ban a user:

function banUser(bannedUsers, userId) {
     return bannedUsers.concat([userId]);
}

function saveBannedUsers(bannedUsers) {
     fs.writeFileSync("./bannedUsers.json");
}

bannedUsers = banUser(bannedUsers, "user-3");
saveBannedUsers(bannedUsers);

That's all.

Sign up to request clarification or add additional context in comments.

2 Comments

Okay I got all this but I don't know how I could get it to check if the user's ID is in the array. I've tried if(readNoMusicJSON().find(id=>id == message.author.id)) return; but this doesn't seem to work (I changed some of the name to make it a bit easier to read for me, readNoMusicJSON is the same as the readBannedUsers function in your answer)
Okay, I thought of another thing where i replaces the readNoMusicJSON() with "badmusicusers" which is my program's equivalent of the bannedUsers variable in your answer, but that doesn't seem to work either.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.