javascript - Discord.js 机器人赠品命令 : embedSent. Reactions.get 不是函数

标签 javascript bots discord

我正在尝试制作一个 Discord.js 赠送命令来发送嵌入,将其保存到变量 embedSent,然后使用 reactions.get() 方法收集超时后的 react ,但我不断收到错误 TypeError: embedSent.reactions.get is not a function 这是我的代码的一部分:

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

最佳答案

好吧,差不多两个月后,我终于想通了。完整的工作命令(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));
}

希望对大家有帮助!

关于javascript - Discord.js 机器人赠品命令 : embedSent. Reactions.get 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62086666/

相关文章:

python - 如何获取发消息人的头像url?

javascript - 使用 XSLT 或任何其他方法将 XML 转换为 HTML 表

javascript - 取消选中所有(自动生成的)复选框

C# LUIS Chatbot 从 LuisResult 中提取所有意图

python - 如何在 Telegram Bot 中管理更多用户?

javascript - 需要帮助制作 Java 脚本 Discord 音乐机器人

python - 我的 Python Discord 机器人中出现 'RuntimeWarning: coroutine ' 函数 1' was never awaited' 错误

python - 如何检查 discord.py 中是否缺少必需的参数

javascript - Angular : Variable template inside directive

php - <?php bp_activity_id() ?> 在 buddypress 中,有人达标了吗?