Fetching Values

To fetch these responses in your code, a convenient property has been added to your client object, the "default Responses" property. This property only has one method, which consists of defaultResponses#getValue. For the example below, say we setup a new message.json file with extra properties as follows...

// ./message.json
{
"en": {
...defaultValues,
"TEST_COMMAND": "This is a response from the message.json file!"
}
}

To fetch this value from our code inside of a command, all we need to do is call the getValue method with the appropriate information. For now we can ignore the ISO codes, as we will talk more about them in Language Support. For this example we will just continue to use the Ping.js command we made earlier.

JavaScript
TypeScript
JavaScript
// ./commands/Ping.js
const { Command, Validator } = require("cdcommands");
module.exports = new Command({
...commandOptions,
run: ({ message, args, client, prefix, language }) => {
const message_json_response = client.defaultResponses.getValue(
"en",
"TEST_COMMAND",
"",
[],
); // In this example, getValue will return a string.
// getValue can also return a MessageEmbed more information in Embeds.
return message.channel.send(message_json_response);
},
});
TypeScript
// ./commands/Ping.ts
import { Command, Validator } from "cdcommands";
export default new Command({
...commandOptions,
run: ({ message, args, client, prefix, language }) => {
const message_json_response = client.defaultResponses.getValue(
"en",
"TEST_COMMAND",
"",
[],
); // In this example, getValue will return a string.
// getValue can also return a MessageEmbed more information in Embeds.
return message.channel.send(message_json_response);
},
});

The above code, when run with ?ping or ?pong, should respond with the content as This is a response from the message.json file!, meaning it successfully read from your message.json file! But you may be asking, what is that extra empty string doing there? What is that empty array there for? What is "en" doing there? Don't worry, these will all be answered, for now we can focus on the second empty string. Why is it there? This secondary string, or key in the context of the message.json file, will read values inside of an object in the message.json file. With this, you have the ability to setup responses under a main category of responses. To do this, all you need to change is the "TEST_COMMAND" property in your message.json file to an object. In this example we will add two values to the object.

// ./message.json
{
"en": {
...defaultValues,
"TEST_COMMAND": {
"TEST_VALUE_ONE": "This is the first test value under \"TEST_COMMAND\"",
"TEST_VALUE_TWO": "This is the second test value under \"TEST_COMMAND\""
}
}
}

To access the two values in TEST_COMMAND now, we will use an almost identical format for the getValue method, only adding one extra value in the place of where the extra string was.

JavaScript
TypeScript
JavaScript
// ./commands/Ping.js
const { Command, Validator } = require("cdcommands");
module.exports = new Command({
...commandOptions,
run: ({ message, args, client, prefix, language }) => {
const json_response_v_one = client.defaultResponses.getValue(
"en",
"TEST_COMMAND",
"TEST_VALUE_ONE",
[],
);
const json_response_v_two = client.defaultResponses.getValue(
"en",
"TEST_COMMAND",
"TEST_VALUE_TWO",
[],
);
message.channel.send(json_response_v_one);
message.channel.send(json_response_v_two);
},
});
TypeScript
// ./commands/Ping.js
import { Command, Validator } from "cdcommands";
export default new Command({
...commandOptions,
run: ({ message, args, client, prefix, language }) => {
const json_response_v_one = client.defaultResponses.getValue(
"en",
"TEST_COMMAND",
"TEST_VALUE_ONE",
[],
);
const json_response_v_two = client.defaultResponses.getValue(
"en",
"TEST_COMMAND",
"TEST_VALUE_TWO",
[],
);
message.channel.send(json_response_v_one);
message.channel.send(json_response_v_two);
},
});

The above code snippet should respond with first the message This is the first test value under "TEST_COMMAND" then the message This is the second test value under "TEST_COMMAND". The first string acts as the language that you want to look into in your message.json file, which you can find more information for under Language Support, the second string is the first key that you are getting values by, then depending on if the value received by the first key is an object or a string, the second key will either be the next property you want to get or an empty string respectively, as shown in the last two examples. The last value can either be an object or an array of objects, which will act as your replacers. More information on replacers can be found under Replacers.