Tengo que ejecutar este comando todos los días a las 9 p. m., pero siempre existe la posibilidad de que mi bot apunte a uno de los dos bots que tengo en mi servidor con random(). ¿Cómo hago para que no los apunte? tal vez haya una manera de excluir a los miembros con un determinado rol, ya que tengo ambos bots con un rol para separarlos de la lista de miembros regulares.
let KillerMessage = new cron.CronJob('00 00 21 * * *', () => { //seconds, minutes, hours, month, day of month, day of week const guild = client.guilds.cache.get('ID'); const channel = guild.channels.cache.get('ID'); const user = client.users.cache.random(); //random user const exampleEmbed = new MessageEmbed() .setColor('#0099ff') .setTitle(`The BLACKENED for this trial is: ${user.username}`) .setImage(user.displayAvatarURL()) channel.send({ embeds: [exampleEmbed] }); }); KillerMessage.start();
Esto lo podemos hacer de diferentes maneras, y algunas de ellas son:
Usando el método de JavaScript filter()
:
// Get the cache users list and filter the users that aren't a bot, creating a new array with only non-bot users. let user = client.users.cache.filter(user => !user.bot).random();
Usando un bucle while :
// This loop randomizes again, until the chosen user is not a bot. let user = client.users.cache.random(); while (user.bot) user = client.users.cache.random();
let user = client.users.cache.random(); while (user.bot) user = client.users.cache.random();
Básicamente, lo que hace es intentar obtener un usuario aleatorio del caché hasta que obtiene un usuario que no es bot.