Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

179
Views
Agregar/eliminar rol de usuario dado el nombre de usuario (Discord.js)

Estoy tratando de crear una función para agregar y eliminar un rol de un usuario dado el nombre de usuario (no el ID de usuario).

Hasta ahora tengo un código que funciona con ID de usuario. Mi pregunta es ¿cómo puedo modificar este código para que funcione dado el nombre de usuario?

 const addDiscord = async (discord_username) => { const guild = client.guilds.cache.get('<guild-id>'); // copy the id of the server your bot is in and paste it in place of guild-ID. const role = guild.roles.cache.get('<role-id>'); // here we are getting the role object using the id of that role. const member = await guild.members.fetch('user-id'); // here we are getting the member object using the id of that member. This is the member we will add the role to. await member.roles.add(role); // here we just added the role to the member we got. }

Aquí está mi función que manejará la verificación de nuevos usuarios para agregar/eliminar

 const checkNew = async () => { if(toUpdate.length > 0) { // Check if there are any new users to update toUpdate.forEach(element => { addDiscord(element.new) // Remove element.old from Discord role // Add element.new to Discord role }) toUpdate = [] } }

Y aquí estoy conectándome al bot y llamando a la función checkNew() cada 10 segundos

 client.once('ready', () => { console.log('Ready!'); setInterval(checkNew, 10000) });

Actualización: intenté usar client.users.cache.find() pero devuelve indefinido

 const addDiscord = async (discord_username) => { const guild = client.guilds.cache.get('<guild-id>'); // copy the id of the server your bot is in and paste it in place of guild-ID. const role = guild.roles.cache.get('<role-id>'); // here we are getting the role object using the id of that role. console.log(discord_username) const id = client.users.cache.find(u => u.tag === discord_username) console.log(id) }

Actualización 2: se intentó implementar el código de la respuesta de Miqhtie

 const addDiscord = async (discord_username) => { const guild = client.guilds.cache.get('<server-id>'); // copy the id of the server your bot is in and paste it in place of guild-ID. const role = guild.roles.cache.get('<role-id>'); // here we are getting the role object using the id of that role. const members = await guild.members.fetch() const member = members.find((m) => m.username === '<discord-username>') console.log(member) }

Error:

 const member = members.find((m) => m.username === '<discord-username>') ^ TypeError: members.find is not a function
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

No existe un método específico para obtener un miembro del gremio por nombre de usuario, por lo que lo que tendría que hacer es buscar a todos los miembros de un gremio <guild>.members.fetch() y luego filtrarlo por un miembro cuya propiedad de nombre de usuario sea igual al nombre de usuario usted quiere.

Un ejemplo de implementación de esto sería

 const username = "Super Cool Username"; const members = await guild.members.fetch(); const member = members.find((m) => m.username === username); member.roles.cache.add(role);
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!