【问题标题】:DIscord mute votingDIScord静音投票
【发布时间】:2019-09-17 02:27:26
【问题描述】:

我正在创建一个命令 .mute @user 创建一个简单的投票,其中添加了 2 个反应 ✅ 和 ❌。一定时间后,如果 ✅ 大于 ❌,我希望机器人将用户静音。

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

编辑 1 您的代码中途工作。 赢得静音投票后的机器人崩溃,然后不起作用,但在控制台中没有错误。我必须重新启动它才能再次工作,问题出在哪里?另外,最好检查一下用户是否没有将角色静音,然后他是否没有进行投票

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

【问题讨论】:

    标签: javascript node.js discord discord.js


    【解决方案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 :)');
        }
    

    【讨论】:

    • 小提示:在收集器启动后进行反应允许在添加反应时输入,而不是等到两者都存在。
    猜你喜欢
    • 1970-01-01
    • 2020-08-02
    • 2021-04-24
    • 2019-03-19
    • 2021-05-09
    • 2021-01-12
    • 2023-03-28
    • 2019-04-03
    • 1970-01-01
    相关资源
    最近更新 更多