[英]Discord.js v13 check if button already has been pressed
is there a way using collectors/interactionCreate to detect if a user already pressed a specific button on a specific message?有没有办法使用收集器/交互创建来检测用户是否已经按下了特定消息上的特定按钮? One way I tried doing it was to save it in an array like this我尝试这样做的一种方法是将它保存在这样的数组中
const alreadyPressed = []
const filter = m => m.customId === "No" && m.messageId === interaction.messageId && m.user.id === interaction.user.id;
collector = embedMsg.createMessageComponentCollector({filter: filter, time: 10000});//86400000
collector.on('collect', async i => {
client.logger("collector1 collected")
client.logger(alreadyPressed.indexOf(i.user.id, i.messageId))
if (alreadyPressed.indexOf(i.user.id, i.messageId)) {
return i.reply({ content: `You've already voted ${i.user.username}!`, ephemeral: true })
} else {
i.reply({ content: `Thank you for your vote ${i.user.username}!`, ephemeral: true });
alreadyPressed.push(i.user.id, i.messageId)
}
});
collector.on('end', collected => {
client.logger(collected.size)
// if (collected.size < 10) {
// }
delete alreadyPressed[0]
delete alreadyPressed[1]
});
but the bot always ends up responding with "You've already voted" so what would be a viable way of checking this?但机器人总是以“你已经投票”结束回应,那么检查这个的可行方法是什么?
as MrMythical said, Array#indexOf()
will just return -1 since it can't find it, I tried this out a little bit and found out that using and storing the values in an array is probably what your looking for.正如 MrMythical 所说, Array#indexOf()
只会返回 -1 因为它找不到它,我尝试了一下,发现在数组中使用和存储值可能是你要找的。 I put them together too since putting them seperate is not needed then I use .find()
to search the array if it matches the users id and the id of the message that the button is on.我也将它们放在一起,因为不需要将它们分开然后我使用.find()
来搜索数组,如果它与用户 ID 和按钮所在消息的 ID 匹配。 This would be a viable way of doing it这将是一种可行的方法
const alreadyPressed = []
const filter = m => m.customId === "No"
collector = embedMsg.createMessageComponentCollector({filter: filter, time: 10000});//86400000
collector.on('collect', async i => {
if (!!alreadyPressed.find(id => {
return id.ID === i.user.id+i.message.id
})) {
i.reply({ content: `You've already voted ${i.user.username}!`, ephemeral: true })
} else {
i.reply({ content: `Thank you for your vote ${i.user.username}!`, ephemeral: true });
alreadyPressed.push({ID: i.user.id+i.message.id})
}
});
collector.on('end', collected => {
client.logger(collected.size)
// if (collected.size < 10) {
// }
delete alreadyPressed
});
You are using Array#indexOf()
which returns -1
if it doesn't find it (JavaScript sees it as a truthy value).您正在使用Array#indexOf()
如果找不到它则返回-1
(JavaScript 将其视为真实值)。 I would also recommend you putting the values into an object or its own array (when pushing)我还建议您将值放入 object 或其自己的数组中(推送时)
const alreadyPressed = []
// ...
collector.on('collect', async i => {
// logs
if (alreadyPressed.includes({userID: i.user.id, messageID: i.messageId})) {
return i.reply({ content: `You've already voted ${i.user.username}!`, ephemeral: true })
} else {
i.reply({ content: `Thank you for your vote ${i.user.username}!`, ephemeral: true });
alreadyPressed.push({userID: i.user.id, messageID: i.messageId})
}
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.