DIscord静音投票

时间:2019-04-28 03:44:07

标签: javascript node.js discord discord.js

我正在执行.mute @user命令,该命令创建一个简单的轮询,并添加了✅和reactions两个反应。一定时间后,如果✅大于i,我希望机器人使用户静音。

问题在于if(agree> disagree)不起作用,因为这两个是表情。如何让我的机器人在30秒后计算投票结果,然后根据此结果使用户静音或发送“静音投票失败”

编辑1 您的代码成功了一半。 赢得静音投票后出现Bot,但随后无法正常工作,但在控制台中没有错误。我必须重新启动才能再次工作,问题出在哪里?另外,最好检查用户是否没有将角色静音,然后再检查是否没有投票

const Discord = require('discord.js') 
const token = '' ;
const client = new Discord.Client();
const PREFIX = "."

client.on('message', async (msg) => {
  var args = msg.content.substring(PREFIX.length).split(" ");

  function wait(ms){
    var start = new Date().getTime();
    var end = start;
    while(end < start + ms) {
      end = new Date().getTime();
   }
 }


  if(msg.author.equals(client.user)) return;
  if (!msg.content.startsWith(PREFIX)) return;
  switch (args[0].toLowerCase()) {

  case "mute":
  const role = msg.guild.roles.find(r => r.name === 'Muted'); // Find Role
  if(msg.mentions.members.first().roles.has(role.id)) return 

  if(!msg.mentions.users.first()) return msg.channel.send('You need to mention somebody!'); // Check if no User was Mentioned
  const voting = new Discord.RichEmbed() // Generate Voting Embed
      .setColor('#42b34d')
      .setFooter('Mute ' + msg.mentions.users.first().tag + ' for 10m?')
      .setImage(msg.mentions.users.first().avatarURL);
  if(!role) return msg.channel.send('No Role was found, please make sure you have a muteed role!'); // Make sure there is a Role
  const agree = '✅'; // Define Emojis
  const disagree = '❌'; // Define Emojis

  const sentEmbed = await msg.channel.send(voting); // Send Embed
  const filter = (reaction, user) => (reaction.emoji.name === agree || reaction.emoji.name === disagree) && !user.bot; // Filter for Reactions
  await sentEmbed.react(agree); // React
  await sentEmbed.react(disagree); // React
  const voteStatus = await msg.channel.send('Voting started 30 seconds left'); // Send start Message and
  const collected = await sentEmbed.awaitReactions(filter, { time: 5000 }); // start Collecting Reactions
  const agreed = collected.get(agree) || { count: 1 }; // Retrieve Reactions
  const disagreed = collected.get(disagree) || { count : 1 }; // Retrieve Reactions
  const agreed_count = agreed.count - 1 ; // Count away Bot Votes
  const disagreed_count = disagreed.count - 1; // Count away Bot Votes
  voteStatus.edit('Voting endet with: ' + agreed_count + agree + ' and ' + disagreed_count + disagree); // Edit message to show Outcome
  if(agreed.count > disagreed.count) {
      await msg.guild.member(msg.mentions.users.first()).addRole(role);
      await wait(600000);
      await msg.guild.member(msg.mentions.users.first()).removeRole(role);
  }
  else {
      msg.channel.send('Mute Voting Failed :)');
  }

}


})

client.on('ready', () => {
    console.log ('Dziala');
})

client.login(token);

1 个答案:

答案 0 :(得分:1)

我通过以下操作解决了这个问题:

if(!msg.mentions.users.first()) return msg.channel.send('You need to mention somebody!'); // Check if no User was Mentioned
    const voting = new Discord.RichEmbed() // Generate Voting Embed
        .setColor('#42b34d')
        .setFooter('Mute ' + msg.mentions.users.first().tag + ' for 10m?')
        .setImage(msg.mentions.users.first().avatarURL);
    const role = msg.guild.roles.find(r => r.name === 'Muted'); // Find Role
    if(!role) return msg.channel.send('No Role was found, please make sure you have a muteed role!'); // Make sure there is a Role
    const agree = '✅'; // Define Emojis
    const disagree = '❌'; // Define Emojis

    const sentEmbed = await msg.channel.send(voting); // Send Embed
    const filter = (reaction, user) => (reaction.emoji.name === agree || reaction.emoji.name === disagree) && !user.bot; // Filter for Reactions
    await sentEmbed.react(agree); // React
    await sentEmbed.react(disagree); // React
    const voteStatus = await msg.channel.send('Voting started 30 seconds left'); // Send start Message and
    const collected = await sentEmbed.awaitReactions(filter, { time: 5000 }); // start Collecting Reactions
    const agreed = collected.get(agree) || { count: 1 }; // Retrieve Reactions
    const disagreed = collected.get(disagree) || { count : 1 }; // Retrieve Reactions
    const agreed_count = agreed.count - 1 ; // Count away Bot Votes
    const disagreed_count = disagreed.count - 1; // Count away Bot Votes
    voteStatus.edit('Voting endet with: ' + agreed_count + agree + ' and ' + disagreed_count + disagree); // Edit message to show Outcome
    if(agreed.count > disagreed.count) {
        await msg.guild.member(msg.mentions.users.first()).addRole(role);
        await wait(600000);
        await msg.guild.member(msg.mentions.users.first()).removeRole(role);
    }
    else {
        msg.channel.send('Mute Voting Failed :)');
    }