node.js - 将对反应做出反应的用户添加到嵌入
问题描述
试图制作一个机器人,当用户点击反应时,discord id 会进入嵌入字段,如果他们取消点击或点击另一个表情符号,他们最终会进入该字段。这将用于投票机器人,一旦一定数量的用户单击“是”或“否”,将做出接受用户或拒绝用户的决定。有什么帮助吗?
exports.run = async (client, message, args) => {
message.delete({ timeout: 100 });
if (!args[0]) return message.reply('You need to supply the question');
let embed = new Discord.MessageEmbed()
.setTitle(args.join(' '))
.setDescription('Poll created by ' + message.author.tag)
.addField('Status', 'Voting is currently open.')
.setColor('#ffd700')
.attachFiles(new Discord.MessageAttachment('https://i.imgur.com/QUmbq9o.png', 'thumbnail.png'))
.setThumbnail('attachment://thumbnail.png')
.setFooter('Bot created by James (Rock)₇₇₇');
message.channel.send(embed).then(async msg => {
await msg.react('');
await msg.react('');
await msg.react('');
await msg.react('️');
const threshold = 6;
async function stop(result) {
collector.stop();
const newEmbed = new Discord.MessageEmbed(msg.embeds[0]);
newEmbed.title = newEmbed.title + ' [CLOSED]';
newEmbed.fields[0] = { name: 'Status', value: 'Voting is now closed.\n' + result };
newEmbed.setThumbnail('attachment://thumbnail.png');
await msg.edit(newEmbed);
msg.reactions.removeAll();
}
async function update() {
const newEmbed = new Discord.MessageEmbed(embed);
const userYes = (votes[''].size === 0)? '-' : [...votes['']];
const userNo = (votes[''].size === 0)? '-' : [...votes['']];
const userUnsure = (votes[''].size === 0)? '-' : [...votes['']];
newEmbed.addFields(
{ name: `User Yes (${votes[''].size}/${threshold})`, value: userYes, inline: true },
{ name: `User No (${votes[''].size}/${threshold})`, value: userNo, inline: true },
{ name: 'User Unsure', value: userUnsure, inline: true }
);
await msg.edit(newEmbed);
if (votes[''].size >= threshold) {
await stop('This answer is good enough to get accepted and an upvote.');
// do something
} else if (votes[''].size >= threshold) {
await stop('This answer is not good enough to get accepted and an upvote.');
// do something
}
}
const votes = {
'': new Set(),
'': new Set(),
'': new Set(),
'️': new Set()
};
update();
const collector = msg.createReactionCollector((reaction, user) => !user.bot , { dispose: true });
collector.on('collect', async (reaction, user) => {
if (['', '', '', '️'].includes(reaction.emoji.name)) {
const userReactions = msg.reactions.cache.filter(reaction => reaction.users.cache.has(user.id));
for (const userReaction of userReactions.values()) {
if (userReaction.emoji.name !== reaction.emoji.name || reaction.emoji.name === '️') {
userReaction.users.remove(user.id);
votes[userReaction.emoji.name].delete(user);
}
}
votes[reaction.emoji.name].add(user);
} else {
reaction.remove();
}
update();
});
collector.on('remove', (reaction, user) => {
votes[reaction.emoji.name].delete(user);
update();
});
});
};
module.exports.help = {
name: "poll"
}
解决方案
我稍微修改了您的代码并添加了代码来跟踪投票、编辑嵌入并检查投票何时达到阈值。
示范
代码
message.delete({ timeout: 100 });
if (!args[0]) return message.reply('You need to supply the question');
let embed = new Discord.MessageEmbed()
.setTitle(args.join(' '))
.setDescription('Poll created by ' + message.author.tag)
.addField('Status', 'Voting is currently open.')
.setColor('#ffd700')
.attachFiles(new Discord.MessageAttachment('https://i.imgur.com/QUmbq9o.png', 'thumbnail.png'))
.setThumbnail('attachment://thumbnail.png')
.setFooter('Bot created by James (Rock)₇₇₇');
message.channel.send(embed).then(async msg => {
await msg.react('');
await msg.react('');
await msg.react('');
await msg.react('️');
const threshold = 1;
async function stop(result) {
collector.stop();
const newEmbed = new Discord.MessageEmbed(msg.embeds[0]);
newEmbed.title = newEmbed.title + ' [CLOSED]';
newEmbed.fields[0] = { name: 'Status', value: 'Voting is now closed.\n' + result };
newEmbed.setThumbnail('attachment://thumbnail.png');
await msg.edit(newEmbed);
msg.reactions.removeAll();
}
async function update() {
const newEmbed = new Discord.MessageEmbed(embed);
const userYes = (votes[''].size === 0)? '-' : [...votes['']];
const userNo = (votes[''].size === 0)? '-' : [...votes['']];
const userUnsure = (votes[''].size === 0)? '-' : [...votes['']];
newEmbed.addFields(
{ name: `User Yes (${votes[''].size}/${threshold})`, value: userYes, inline: true },
{ name: `User No (${votes[''].size}/${threshold})`, value: userNo, inline: true },
{ name: 'User Unsure', value: userUnsure, inline: true }
);
await msg.edit(newEmbed);
if (votes[''].size >= threshold) {
await stop('This answer is good enough to get accepted and an upvote.');
// do something
} else if (votes[''].size >= threshold) {
await stop('This answer is not good enough to get accepted and an upvote.');
// do something
}
}
const votes = {
'': new Set(),
'': new Set(),
'': new Set(),
'️': new Set()
};
update();
const collector = msg.createReactionCollector((reaction, user) => !user.bot , { dispose: true });
collector.on('collect', async (reaction, user) => {
if (['', '', '', '️'].includes(reaction.emoji.name)) {
const userReactions = msg.reactions.cache.filter(reaction => reaction.users.cache.has(user.id));
for (const userReaction of userReactions.values()) {
if (userReaction.emoji.name !== reaction.emoji.name || reaction.emoji.name === '️') {
userReaction.users.remove(user.id);
votes[userReaction.emoji.name].delete(user);
}
}
votes[reaction.emoji.name].add(user);
} else {
reaction.remove();
}
update();
});
collector.on('remove', (reaction, user) => {
votes[reaction.emoji.name].delete(user);
update();
});
});
推荐阅读
- javascript - 如何解决依赖问题:“npm run dev”不起作用
- javascript - Puppeteer 对 args 的困惑
- symfony - 用于夹具生成的自定义代理实体
- xcode - Xcode 界面生成器显示“无选择”
- powershell - 子生成的powershell脚本完成后如何继续powershell脚本?
- python - 我正在寻找在 python 中生成列表列表的组合
- python - Django Admin自定义列表过滤不同的模型
- java - 如何解决 - 如果应用程序未打开一段时间,Onesignal 推送通知不会发送到 Android 设备?
- x11 - 为 X2Go 客户端设置一致的显示编号
- c++ - 为什么 std::basic_istream::ignore() 提取的字符比指定的多?