所以我正在为 Slack 开发一个机器人,它每周都会提到一个随机用户。我到了机器人确实做到这一点的地步,但我不确定如何避免重复。正如您在下面的屏幕截图中看到的那样,显示了重复的名称:
这是我的代码:
// Handle events related to the websocket connection to Slack
controller.on('rtm_open', function (bot) {
console.log('** The RTM api just connected!');
// Get all users
bot.api.users.list({}, function(err, list){
// If list and members exist
if (list && list.members) {
list.members = list.members.filter(function(member) {
return (member.real_name != undefined && member.real_name != "" && member.real_name != null && member.real_name != "slackbot");
});
// console.log(list.members);
var previousRandomNumber = -1;
// Every X amount of milliseconds
var interval = setInterval(function() {
var members = list.members;
// Generating random number from 0 to how many members exist (exclusive) in the member list
var randomNumber = Math.floor(Math.random() * members.length);
console.log('1) previous: ' + previousRandomNumber + ' randomNumber ' + randomNumber);
if (previousRandomNumber == randomNumber) {
randomNumber = Math.floor(Math.random() * members.length);
}
console.log('2) previous: ' + previousRandomNumber + ' randomNumber ' + randomNumber);
previousRandomNumber = randomNumber;
// Get random name
var randomName = members[randomNumber].name;
//console.log(randomName);
// If random name is null or undefined, reshuffle.
if (randomName == null || randomName == "" || randomName == undefined) {
console.log('user has been deleted');
// clearInterval(interval);
} else {
console.log('actual person - ' + randomName);
// Configuring webhook and sending message to channel
bot.configureIncomingWebhook({url: 'https://hooks.slack.com/services/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'});
bot.sendWebhook({
text: '@' + randomName + ", you've been selected for #snapshot! ",
channel: '#test',
},function(err,res) {
// handle error
});
}
}, 3000); // 604800000 ms = 1 week
console.log("message");
}
})
});
有谁知道我如何以某种方式拼接数组中已经提到的名称?