discord.js - 获取用户在频道中提及的次数
问题描述
我正在尝试计算用户在频道中被提及的次数。我的代码是:
message.channel.messages.fetch()
.then(messages => console.log(`${messages.filter(m => m.mentions.users[1] === 'msg.author').size} messages`))
.catch(console.error);
当我在一个频道中运行此程序时,我在该频道的历史记录中有 27 次提及。这回来了0 messages
。
我该如何做到这一点?
解决方案
错误在于您的过滤器,
m.mentions.users
是由用户 id 映射的集合,当您执行 [1] 时,它会检查 id 为 1 的用户,然后将其检查到字符串msg.author
message.channel.messages.fetch()
.then(messages => {
//.has() checks if the collection includes the key
const size = messages.filter(m => m.mentions.users.has(message.author.id)).size;
console.log(size, "messages");
});
.catch(console.error);
推荐阅读
- reactjs - 如何从选择中调度一个动作
- salesforce - 网络 RTC - Salesforce
- azure - 如何从 Azure Function App 调用 Azure API 管理端点
- scripting - AHK 脚本末尾是否需要“return”语句?
- python-3.x - 在 Python 中逐年创建一系列季度
- python - Yahoo_fin 在用户输入后不断获得实时价格
- c# - 注入程序集引用/w Mono Cecil 不起作用
- c++ - 删除析构函数的类被认为是可复制的?
- postgresql - 将变量传递给 DO 循环
- c# - NSwag 为每个控制器单独的类