I'm trying to get my client to know which array it should follow.
var json = [{
command: "command 1",
giveRole: "884005303811702794"
}, {
command: "command 2",
giveRole : "948609651673563179"
}];
for (var key in json) {
if (json.hasOwnProperty(key)) {
console.log(json[key].command);
console.log(json[key].giveRole);
}
}
client.on('message', message => {
if (message.content.startsWith(json[key].command)) {
target.roles.add([json[key].giveRole]);
}
});
For example if I send "command 1" it should give role "884005303811702794" not "948609651673563179".
CodePudding user response:
So you need to look it up. You can use find to do it.
var json = [{
command: "command 1",
giveRole: "884005303811702794"
}, {
command: "command 2",
giveRole: "948609651673563179"
}];
function getCommand (command) {
return json.find(function (item) {
return item.command === command;
});
}
console.log("command 1", getCommand("command 1"));
console.log("command 2", getCommand("command 2"));
console.log("command 3", getCommand("command 3"));
Other option is change your commands so it is an object instead of an array.
var myCommands = {
"command 1": {
giveRole: "884005303811702794"
},
"command 2": {
giveRole: "948609651673563179"
},
};
function getCommand(command) {
return myCommands[command] || null;
}
console.log("command 1", getCommand("command 1"));
console.log("command 2", getCommand("command 2"));
console.log("command 3", getCommand("command 3"));
And if the command is not the whole string, you can loop
var json = [{
command: "command 1",
giveRole: "884005303811702794"
}, {
command: "command 2",
giveRole: "948609651673563179"
}];
function getCommand (command) {
return json.find(function (item) {
return command.toLowerCase().startsWith(item.command);
});
}
console.log("command 1", getCommand("command 1 eat pizza"));
console.log("command 2", getCommand("command 2 drink water"));
console.log("command 3", getCommand("command 3 dance"));
CodePudding user response:
First of all, you seem to try to find the element in your array by using a key, but your array is not keyed.
In Javascript, you can use the find method on arrays. This will return the first element on the array that returns a true value.
As per the example of the Doc, you should be able to find the valid command in with this :
let json = [{
command: "command 1",
giveRole: "884005303811702794"
}, {
command: "command 2",
giveRole : "948609651673563179"
}];
const command = json.find(element => element.command === 'command 1');
Another good way to store your commands would be to store it in an object / associative arrays. Ex:
const commands = {
command1: '884005303811702794',
command2: '948609651673563179',
}
console.log('command1', commands['command1'])
console.log('command2', commands['command2'])
That way, you could simply use the value of the command to have the role that you want.
In your situation, it seems like you want a discord bot to give a role when a message starts with "command 1" or "command 2". If you implement a keyed array, it would look something like this :
const commands = {
command1: '884005303811702794',
command2: '948609651673563179',
}
client.on('message', message => {
if (commands.hasOwnProperty(message.content)) {
target.roles.add(commands[message.content]);
}
});
CodePudding user response:
That works for console.log but how do i use it in
client.on('message', message => {
if (message.content.startsWith(json[key].command)) {
target.roles.add([json[key].giveRole]);
}
});