gpt4 book ai didi

javascript - 如何在一定量的 react 后使 react 消失?

转载 作者:行者123 更新时间:2023-12-04 08:07:38 24 4
gpt4 key购买 nike

我想创建一个 poll 命令,在那里我使用该命令,当 react 达到一定数量的 react 时,它( react )被删除。如果你理解我的话,看看谁赢了会很有用。
这就是我现在仅用于添加 react 的用途。

bot.on('message', msg => {
if(msg.content === "memes"){
msg.react("🤏🏾")
}
})

最佳答案

您可以使用 reaction collectors收集用户对消息的 react 。收集器有类似 maxUsers 的选项您可以使用它来设置要使用react的最大用户数。一旦到达,collector.on('end')火灾,因此您可以使用它来获取 react 的用户数量等。
您可以使用 message.reactions.removeAll(); 删除 react 。
检查下面的片段:

if (message.content === 'memes') {
const EMOJI = '🤏🏾';
const filter = (reaction, user) =>
reaction.emoji.name === EMOJI && !user.bot;
const collector = message.createReactionCollector(filter, {
// optional, the maximum time in ms the collector collects reactions
time: 10000,
// maximum number of users to react
// it includes the bot's reaction too
maxUsers: 3,
});

message.react(EMOJI);

collector.on('collect', (reaction, user) => {
// it fires every time someone reacts to the message
console.log('collected', reaction);
});

collector.on('end', async (collected, reason) => {
const reaction = collected.get(EMOJI);

if (!reaction) {
return message.channel.send('Oops, there were no reactions');
}

// it includes the bot too
const allUsers = await reaction.users.fetch();
// collection of users who reacted to the message, bot removed
const users = allUsers.filter((user) => user != client.user);

message.channel.send(
`Poll has ended because of ${reason}. Received ${users.size} reactions.`
);
message.channel.send(
`Users reacted: ${users.array().join(', ')}`
);

// remove reactions
message.reactions.removeAll();
});
}
如果你想拥有一组表情符号,你可以使用 EMOJIS数组代替:
if (message.content === 'memes') {
const EMOJIS = ['🤏🏾', '😱'];
const filter = (reaction, user) =>
EMOJIS.includes(reaction.emoji.name) && !user.bot;
const collector = message.createReactionCollector(filter, {
time: 10000,
maxUsers: 3,
});

EMOJIS.forEach((emoji) => message.react(emoji));

collector.on('collect', (reaction, user) => {
// it fires every time someone reacts to the message
});

collector.on('end', async (collected, reason) => {
if (!collected.size) {
message.reactions.removeAll();
return message.channel.send('Oops, there were no reactions');
}

// you can use the collected reactions any way you want
console.log(collected);

// remove reactions
message.reactions.removeAll();
});
}

关于javascript - 如何在一定量的 react 后使 react 消失?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66144530/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com