【发布时间】:2021-08-02 23:35:37
【问题描述】:
试图制作一个机器人,当用户点击反应时,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"
}
【问题讨论】:
-
它们最终进入内联字段是什么意思?
-
点击赞许的用户会被放入一个嵌入中,显示那里的每个人用户名投票赞许,或者如果您点击赞许,您的用户名会在该字段中弹出。
-
您遇到了什么错误?根据您的问题,我无法理解实际问题是什么
-
嵌入弹出反应弹出,但我希望它输入在频道中点击嵌入内反应的用户名。
-
i.gyazo.com/83fb7d7e810919ea77cf2abf21635546.gif 喜欢这个@mlarionov
标签: node.js discord.js