【问题标题】:Discord.js v12 giveaway commandDiscord.js v12 赠品命令
【发布时间】:2021-04-18 16:13:06
【问题描述】:

所以基本上我有这个很好用的赠品命令,但如果你在你设置的时间完成后对表情符号做出反应,它只会发送“没有足够的人对我做出反应以抽出获胜者,它不会选择获胜者”不知道为什么会这样。

client.on('message', async message =>{
  if(message.content.toLowerCase().startsWith(prefix + 'giveaway')) {
    if(!message.member.hasPermission("MANAGE_MESSAGES")) 
return message.channel.send('You cant use this command sice youre missing `manage_messages` perm')
    let args = message.content.substring(prefix.length).split(" ")
    let time = args[1]
    if(!time) return message.channel.send('You did not specify your time');
    if(
      !args[1].endsWith("d") &&
      !args[1].endsWith("h") &&
      !args[1].endsWith("m") &&
      !args[1].endsWith("s")
    )
    return message.channel.send("You need to use d (days), h (hours), m (minutes), or s (seconds)")

    let gchannel = message.mentions.channels.first();
    if(!gchannel) return message.channel.send("I cant find that channel in the server.")
    let prize = args.slice(3).join(" ")
    if(!prize) return message.channel.send('What is the prize?')

    message.delete()
    gchannel.send(`:tada: **New Giveaway** :tada:`)
    const newEmbed = new Discord.MessageEmbed()
    .setTitle('New Giveaway')
    .setColor("RANDOM")
    .setDescription(`React with :tada: to enter the giveaway. 
\nHosted By: **${message.author}** \nTime: **${time}**\nPrize: **${prize}**`)
    .setFooter(`Will end in ${time}`)
    let reaction = await gchannel.send(newEmbed)
    reaction.react("????")
    setTimeout(() => {
      if ((m) => m.reaction.cache.get("????").count <= 0) {
        return message.channel.send("Not enough people reacted for me to draw a winner")
      }
      let winner = (m) => m.reaction.cache.get("????").users.cache.filter((u) => !u.bot).random();
      gchannel.send(`Congratulations ${winner} You just won the **${prize}**!`
      );
    }, ms(args[1]));
  }
})

【问题讨论】:

  • m 来自哪里?你不能那样做。您需要获取带有反应的实际消息对象 (reaction)
  • 我该怎么做?
  • 您已经在reaction 变量中拥有它
  • idk 改变什么或改变什么
  • 好吧,.send() 返回一个包含 Message 对象的 Promise,该对象具有 reactions 属性

标签: javascript discord discord.js


【解决方案1】:

你不能在你的 if 语句中无缘无故地使用箭头函数,因为m 没有被设置为任何东西,你会得到undefined

要解决此问题,您需要使用您发送的消息作为您的 Message 对象,因为当您使用 .send() 时,会返回一个 promise,其中包含一个 Message 对象。然后,此对象具有属性reactions,可用于获取对消息的反应。

例如:

if (reaction.reactions.cache.get("?").count <= 0) {
    return message.channel.send("Not enough people reacted for me to draw a winner")
}

但是,我建议改用 ReactionCollector,因为您可以设置过滤器和时间,然后触发事件,您可以收听并获取等的大小。

const filter = (reaction, user) => {
    return reaction.emoji.name === '?';
};

const collector = reaction.createReactionCollector(filter, { time: 15000 });
// time is in ms (so 15000ms is 15 seconds)

collector.on('collect', (reaction, user) => {
    console.log(`Collected ? from ${user.tag}`);
});

collector.on('end', collected => {
    console.log(`Collected ${collected.size} items`);
    // collected.size will only contain the number of ? on the message, as the collector had a filter applied to it
});

【讨论】:

    猜你喜欢
    • 2020-03-17
    • 2021-03-10
    • 2020-11-06
    • 2020-09-17
    • 2021-01-01
    • 2021-02-18
    • 1970-01-01
    • 2021-01-18
    • 2021-04-14
    相关资源
    最近更新 更多