Stasio
Stasio

Reputation: 21

DIscord mute voting

I'm making a command .mute @user that creates a simple poll with 2 reactions added ✅ and ❌. After certain time i want the bot to mute user if ✅'s is more than ❌.

The problem is that if(agree > disagree) doesn't work because those two are an emotes. How can i make my bot count the results of voting after 30 seconds and then based on this either mute an user or send "Mute Voting Failed"

EDIT 1 Your code works halfway. Bot after won mute voting crashes and then doesn't work but in console there are no errors. I have to restart it to work again where is the problem? Also It would be nice to check if the user doesnt have the role muted and then if he has not do the voting

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

Upvotes: 0

Views: 1681

Answers (1)

PLASMA chicken
PLASMA chicken

Reputation: 2785

I solved this by doing the following:

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 :)');
    }

Upvotes: 2

Related Questions