我想获取对特定消息做出反应的 id
用户并将他们推送到变量 players
,我已经 console.log()
多件事寻找解决方案,用过google,问过朋友,没有成功。我希望我能找到一个有足够经验的人来帮助我找到答案或另一个问题来回答我这个小问题。
let players = [message.author.id]
message.react('?')
const filter = (reaction, user) => reaction.emoji.name === '?'
message.awaitReactions(filter, { time: 20000 })
.then(collected => {
console.log(collected.user.id)
players.push(collected.user.id)
})
.catch(console.error);
答案 0 :(得分:0)
检查此文档:r aggregate dataframe: some columns unchanged, some columns aggregated
const userReactions = message.reactions.cache.filter(reaction => reaction.users.cache.has(userId));
try {
for (const reaction of userReactions.values()) {
await reaction.users.remove(userId);
}
} catch (error) {
console.error('Failed to remove reactions.');
}
您可以使用此示例,但您可以执行以下操作,而不是删除 userId:
const userReactions = message.reactions.cache.filter(reaction => reaction.users.cache.has(userId));
try {
players.push(userId)
} catch (error) {
console.error('Failed to push reaction');
}
答案 1 :(得分:0)
awaitReactions
返回 Collection
的 MessageReaction
。因此 collected.user
将是未定义的。
您必须从 MessageReaction
获取第一个 Collection
并访问其 users
属性,过滤掉机器人的 ID。
// Creating a filter.
const Filter = (reaction, user) => reaction.emoji.name === "?";
// Reacting to the message.
await message.react("?");
// Awaiting reactions.
message.awaitReactions(Filter, { time: 6000 }).then((collected) => {
const reaction = collected.first();
// Making sure that at least one user reacted to the message
if (reaction) {
const players = reaction.users.cache.filter((user) => user.id !== client.user.id);
console.log(`Users that reacted: ${players.map((user) => user.username).join(", ")}`);
} else {
console.log("No one reacted to this message.");
}
});
如果您收到诸如 The 'await' operator can only be used within an async method.
之类的错误,您必须使您的方法异步。
示例:
client.on("message", async (message) => {
// Code
});
答案 2 :(得分:0)
试试这个:
const filter = (reaction, user) => reaction.emoji.name === '?'
message.awaitReactions(filter, { time: 20000 })
.then(collected => {
collected.first().users.cache.forEach(u => players.push(u.id))
})
.catch(console.error);
我无法测试。请告诉我您可能遇到的任何错误。