javascript - DIScord静音投票
问题描述
我正在创建一个命令 .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);
解决方案
我通过执行以下操作解决了这个问题:
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 :)');
}
推荐阅读
- php - php 代码的行为根据位置不同(表中的索引)
- python - Pygame Surface 非顺序更新
- reactjs - 在 React 中处理服务器端受保护路由的正确方法?
- reactjs - React 事件处理函数在不使用 bind() 的情况下工作
- swift4.1 - Swift 4.1 中的“.addingPercentEncoding”有什么问题?
- java - 带有页面大小检测的 itext 7 html 到 pdf
- go - 在没有接收器的情况下,是否可以将数据打开的缓冲通道保留下来?
- rest - PowerBuilder 如何连接到 REST API?
- c++ - openmp Linux中的分段错误
- python - Python 的 Queue.PriorityQueue 不是自动排序的吗?