3

I have been trying to look through so many different types of tutorial for this but nothing I do seems to be working. For my discord bot I created a json file that houses the following object

{"trainer":[{"name":"Bob","C0":45,"C1":84,"C2":147},
    {"name":"Paul","C0":60,"C1":104,"C2":167}],
"prefix":!
}

Now I am trying to figure out how I can make a command so if someone types in !trainer Bob I want the bot display the values for CO, C1, and C2

At the moment I have the following:

const args = message.content.slice(X.prefix.length).split(/ +/);
const command = args.shift().toLowerCase();

X is the name of the json file. the following is where I get stuck

 if(command === "trainer"){
    message.channel.send('WHAT DO I PUT HERE?')
}

Now I am very new to this all, but if my understanding is correct, my argument 0 is trainer in this case, so what I would like is to use my first argument (which would be the name after !trainer) to link back to the array in my json file so that I can get the other values in the array. Now what I am thinking of doing is creating a const with Shift(1) so that const holds my first argument, but I am still not sure how to use that to get the information from the array. Any guidance on this would be greatly appreciated!

1 Answer 1

1

You can filter the available trainers in your json data based on the user's first argument which should be the trainer's name. If a name in the json data matches the user’s requested trainer, you can make use of a template literal to cleanly construct a string to send, else you can tell the user that the trainer doesn't exist.

let queriedTrainerName = args[0];
let trainersArray = X.trainer;
let matchingTrainers = trainersArray.filter(trainerObject => trainerObject.name.toLowerCase() === queriedTrainerName.toLowerCase());
if (matchingTrainers.length > 0) {
    let firstMatch = matchingTrainers[0];
    message.channel.send(`For trainer ${queriedTrainerName}: C0 is ${firstMatch.C0}, C1 is ${firstMatch.C1}, C2 is ${firstMatch.C2}`);
} else message.channel.send(`Trainer ${queriedTrainerName} does not exist!`);
Sign up to request clarification or add additional context in comments.

Comments

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.