• Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses and challenges
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

120
Views
Discord.js v13: how do I stop bot from targeting itself when trying to get a random member's profile picture?

I have this command to execute every day at 9 pm, but there is always a chance that my bot targets one of the two bots I have on my server with random(). How do I make it so it doesn't target them? perhaps there is a way to exclude members with a certain role since I have both bots with a role to separate them from the regular member list.

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();
9 months ago · Juan Pablo Isaza
2 answers
Answer question

0

We can do this in different ways, and some of them are:

Using the filter() JavaScript method:

// 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();

Using a while loop:

// 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();
9 months ago · Juan Pablo Isaza Report

0

let user = client.users.cache.random();

while (user.bot) user = client.users.cache.random();

Basically what this does is it tries to get a random user from cache until it gets a non-bot user.

9 months 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 job Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2023 PeakU Inc. All Rights Reserved.