繁体   English   中英

Discord.js v12 赠品命令

[英]Discord.js v12 giveaway command

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

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

您不能在 if 语句中无缘无故地使用箭头 function,因为m尚未设置为任何值,您将得到undefined

要解决此问题,您需要使用您发送的消息作为消息 object,因为当您使用.send()时会返回一个 promise,其中包含消息object。此 object 具有可用于获取的属性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
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM