Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

88
Vistas
Follow an array based on the variable

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".

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

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"));

about 4 years ago · Juan Pablo Isaza Denunciar

0

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]);
  }
});
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda