Discord.js v13:在尝试获取随机成员的个人资料图片时,如何阻止机器人以自身为目标?
Discord.js v13: how do I stop bot from targeting itself when trying to get a random member's profile picture?
我要在每天晚上 9 点执行此命令,但我的机器人总是有可能使用 random() 将我服务器上的两个机器人之一作为目标。我该怎么做才能使其不针对他们?也许有一种方法可以排除具有特定角色的成员,因为我有两个机器人都具有将它们与常规成员列表分开的角色。
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();
let user = client.users.cache.random();
while (user.bot) user = client.users.cache.random();
基本上,它的作用是尝试从缓存中获取随机用户,直到获得 non-bot 用户。
我们可以通过不同的方式做到这一点,其中一些是:
使用 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();
使用 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();
我要在每天晚上 9 点执行此命令,但我的机器人总是有可能使用 random() 将我服务器上的两个机器人之一作为目标。我该怎么做才能使其不针对他们?也许有一种方法可以排除具有特定角色的成员,因为我有两个机器人都具有将它们与常规成员列表分开的角色。
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();
let user = client.users.cache.random();
while (user.bot) user = client.users.cache.random();
基本上,它的作用是尝试从缓存中获取随机用户,直到获得 non-bot 用户。
我们可以通过不同的方式做到这一点,其中一些是:
使用 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();
使用 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();