[英]Limit user reaction to message
I'm building a simple poll bot for Discord in JavaScript, right now I'm trying to implement max number of reactions per user to a message.我正在用 JavaScript 为 Discord 构建一个简单的轮询机器人,现在我正在尝试实现每个用户对消息的最大反应数。
For example , suppose we have the following options for a poll question:例如,假设我们有以下投票问题选项:
The Question?
问题?
Option A
选项 A
Option B
选项 B
Option C
选项 C
Option D
选项 D
Option E
选项 E
Each "option" is a reaction to the message given from the bot, I want to make sure that a user cannot react to more than 3
of those options.每个“选项”都是对机器人给出的消息的反应,我想确保用户不能对超过
3
个选项做出反应。
messageReactionAdd
listener and then when the user reacted for the 4th time
, remove the last reaction, sending him a message like "You've already voted 3 times
, please remove a reaction to vote again".messageReactionAdd
监听器,然后当用户4th time
反应4th time
,删除最后一个反应,给他发送一条消息“你已经投票3 times
,请删除一个反应再投票”。 Could someone give me some insight on this?有人可以让我对此有所了解吗?
EDIT编辑
Code used to send messages:用于发送消息的代码:
Embed = new Discord.MessageEmbed()
.setColor(0x6666ff)
.setTitle(question)
.setDescription(optionsList);
message.channel.send(Embed).then(messageReaction => {
for (var i = 0; i < options.length; i++){
messageReaction.react(emojiAlphabet[i][0]);
}
message.delete().catch(console.error);
});
Try this:尝试这个:
const {Collection} = require('discord.js')
// the messages that users can only react 3 times with
const polls = new Set()
// Collection<Message, Collection<User, number>>: stores how many times a user has reacted on a message
const reactionCount = new Collection()
// when you send a poll add the message the bot sent to the set:
polls.add(message)
client.on('messageReactionAdd', (reaction, user) => {
// edit: so that this does not run when the bot reacts
if (user.id === client.user.id) return
const {message} = reaction
// only do the following if the message is one of the polls
if (polls.has(message)) {
// if message hasn't been added to collection add it
if (!reactionCount.get(message)) reactionCount.set(message, new Collection())
// reaction counts for this message
const userCount = reactionCount.get(message)
// add 1 to the user's reaction count
userCount.set(user, (userCount.get(user) || 0) + 1)
if (userCount.get(user) > 3) {
reaction.users.remove(user)
// <@!id> mentions the user (using their nickname if they have one)
message.channel.send(`<@!${user.id}>, you've already voted 3 times, please remove a reaction to vote again.`)
}
}
})
client.on('messageReactionRemove', (reaction, user) => {
// edit: so that this does not run when the bot reacts
if (user.id === client.user.id) return
const {message} = reaction
const userCount = reactionCount.get(message)
// subtract 1 from user's reaction count
if (polls.has(message)) userCount.set(user, reactionCount.get(message).get(user) - 1)
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.