该命令的语法是:!giveaway(多少个获胜者)(时间)(获得) 然后,您可以做出反应以获得获胜的机会,但是:问题是做出反应的用户可以赢得多次。我不知道该怎么解决
message.delete();
var item = "";
var time;
var winnerCount
let messageArray = message.content.split(" ");
for (var i = 3; i < messageArray.length; i++) {
item += (messageArray[i] + " ");
}
winnerCount = (messageArray[1]);
if(!winnerCount) return message.channel.send("You must specify the number of winners!");
time = (messageArray[2]);
if(!time) return message.channel.send("You have to specify the time of the giveaway!")
if(!item) return message.channel.send("You have to specify the prize of the giveaway!")
var embed = new Discord.RichEmbed()
.setAuthor(item)
.setDescription(`React with ? to enter! ${winnerCount} Winner(s)`)
.setFooter(`The giveaway lasts ${time} and was started at ${date.format(now, 'YYYY/MM/DD')} / ${date.format(now, 'HH:mm:ss')}!`)
var embedSent = await message.channel.send(embed);
embedSent.react("?");
setTimeout(function() {
var peopleReacted = embedSent.reactions.get("?").users.filter(user => user.id !== client.user.id).array()
if(peopleReacted.length === 0) return message.channel.send("Unfortunately, nobody took part in the giveaway! :(")
var index = Math.floor(Math.random() * peopleReacted.length)
var winners = [];
var winnerMessage = "";
for (var i = 0; i < winnerCount; i++){
winners.push(peopleReacted[index]);
}
for (var i = 0; i < winners.length; i++) {
winnerMessage += (winners[i].toString() + " ");
}
let haveHas = winners.length === 1 ? "has" : "have"
if(winnerCount < winners.length) return;
message.channel.send(winnerMessage + "" + haveHas + ` won **${item}**`);
let embed = new Discord.RichEmbed()
.setAuthor(`${item}`)
.setDescription(`Winner(s): ${winnerMessage}`)
.setFooter(`The giveaway ended!`)
embedSent.edit(embed)
}, ms(time))```
答案 0 :(得分:2)
问题在以下几行之内:
var index = Math.floor(Math.random() * peopleReacted.length)
var winners = [];
var winnerMessage = "";
for (var i = 0; i < winnerCount; i++){
winners.push(peopleReacted[index]);
}
我建议将获奖者推向一个阵营。选择新的获胜者时,检查它是否已存在于数组中,如果不存在,则按,否则画出新的获胜者。
没有javacript专家,但是类似:
var index = Math.floor(Math.random() * peopleReacted.length)
var winners = [];
var winnerMessage = "";
for (var i = 0; i < winnerCount; i++){
if(! winners.includes(index)) winners.push(peopleReacted[index]);
else i--; // Don't progress the loop if no winner is selected.
}
答案 1 :(得分:0)
正如Dexirian所说的here,
问题在以下几行之内:
var index = Math.floor(Math.random() * peopleReacted.length);
var winners = [];
var winnerMessage = "";
for (var i = 0; i < winnerCount; i++){
winners.push(peopleReacted[index]);
}
我建议将获奖者推向一个阵营。选择新的获胜者时,检查它是否已存在于数组中,如果不存在,则按,否则画出新的获胜者。
我想补充一点,提到您只选择一个索引并重新使用它,因此每次都将同一个人添加到winners
中。我将执行以下操作:
var winners = [];
var winnerMessage = "";
if(peopleReacted.length < winnerCount){
winners = peopleReacted; // Add everyone to the list of winners if the number of participants is less than or equal to the number winners
}
else{
let index = 0;
for(var i = 0; i < winnerCount; i++){
index = Math.floor(Math.random() * (peopleReacted.length - 1)); // Subtract 1 from peopleReacted.length to prevent an Array Out of Bounds exception
if(!winners.includes(peopleReacted[index])) winners.push(peopleReacted[index]);
else i--; // Don't progress the loop if no winner is selected.
}
}
那应该做到。让我知道您是否有任何疑问!
答案 2 :(得分:0)
var index = Math.floor(Math.random() * peopleReacted.length) var winners = []; var winnerMessage = ""; for (var i = 0; i < winnerCount; i++){ winners.push(peopleReacted[index]); }
由于只定义一次索引,因此winners
循环的每次迭代都将同一用户推入for
数组。
每次确定一个新的获胜者,然后从选择池中删除所选的获胜者,以免再次被选中。
if (peopleReacted.length < winnerCount) { // Make sure there's enough users, not just 0
return message.channel.send("Unfortunately, not enough users took part in the giveaway! :(")
.catch(console.error);
}
const winners = [];
const winnerMessage = "";
for (let i = 0; i < winnerCount; i++) {
const index = Math.floor(Math.random() * peopleReacted.length);
winners.push(peopleReacted[index]); // Pick a randomly selected user as a winner
peopleReacted.splice(index, 1); // Remove that user from the pool to be selected from
}