Discord.js Bot giveaway command : embedSent.reactions.get is not a function
I am trying to make a Discord.js giveaway command that send an embed, save it to the variable embedSent then collect the reactions after the TimeOut with the reactions.get()
method, but I keep getting the error TypeError: embedSent.reactions.get is not a function
Here is the part of my code :
var embed = new Discord.MessageEmbed();
embed.setColor(0x3333ff);
embed.setTitle("Nouveau Giveaway !");
embed.setDescription("**" + item + "**");
embed.addField(`Durรฉe : `, ms(ms(time), {
long: true
}), true);
embed.setFooter("Rรฉagissez ร ce message avec ๐ pour participer !");
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()
}, time);
Solution 1:
Ok, after almost 2 months, I finally figured it out. The full, working command (DiscordJS v12) :
if (command == "giveaway") {
// !giveaway {time s/m/d} {item}
const messageArray = message.content.split(" ");
if (!message.member.hasPermission(["ADMINISTRATOR"])) return message.channel.send("You don't have enough permissions to start a giveaway !")
var item = "";
var time;
var winnerCount;
for (var i = 1; i < args.length; i++) {
item += (args[i] + " ");
}
time = args[0];
if (!time) {
return message.channel.send(`Invalid duration provided`);
}
if (!item) {
item = "No title"
}
var embed = new Discord.MessageEmbed();
embed.setColor(0x3333ff);
embed.setTitle("New Giveaway !");
embed.setDescription("**" + item + "**");
embed.addField(`Duration : `, ms(ms(time), {
long: true
}), true);
embed.setFooter("React to this message with ๐ to participate !");
var embedSent = await message.channel.send(embed);
embedSent.react("๐");
setTimeout(async () => {
try{
const peopleReactedBot = await embedSent.reactions.cache.get("๐").users.fetch();
var peopleReacted = peopleReactedBot.array().filter(u => u.id !== client.user.id);
}catch(e){
return message.channel.send(`An unknown error happened during the draw of the giveaway **${item}** : `+"`"+e+"`")
}
var winner;
if (peopleReacted.length <= 0) {
return message.channel.send(`Not enough participants to execute the draw of the giveaway **${item}** :(`);
} else {
var index = Math.floor(Math.random() * peopleReacted.length);
winner = peopleReacted[index];
}
if (!winner) {
message.channel.send(`An unknown error happened during the draw of the giveaway **${item}**`);
} else {
console.log(`Giveaway ${item} won by ${winner.toString()}`)
message.channel.send(`๐ **${winner.toString()}** has won the giveaway **${item}** ! Congratulations ! ๐`);
}
}, ms(time));
}
Hope it helped some !