javascript - Discord 机器人计算每个用户的特定单词 Discord.js
问题描述
目前我正在制作一个机器人,它可以计算某个用户说出单词“:p”的次数,无论是在句子中还是单独使用。发生这种情况时,我希望它为变量添加 +1,以便我可以使用函数来检查变量中的数字。使用到目前为止的代码,我可以检测到单词“:p”何时被单独使用,但我无法检测到是谁发送的或何时在句子中。我尝试使用 author.id,但身份不明。(代码)
var numberofp = 0;
client.on('message', (message) => {
if(message.content == ':p') {
message.reply('Another one (:');
numberofp = numberofp + 1;
console.log(numberofp);
}
});
当我使用该变量时,它还说:numberofp 未定义。
总的来说,我需要帮助的是:
- 让机器人识别何时使用 ':p' 甚至在句子中间
- 然后让它检查它是否是某个人
- 最后,让它为变量 numberofp 添加 +1
如果您需要更多代码或信息,请询问:D
解决方案
我会说使用 quick.db 来计算特定用户的特定单词。
我将为您编写示例代码:
const db = require("quick.db");
client.on('message', (message) => {
if(message.content == ':p') {
let count = db.fetch(`countWord_${message.guild.id}_${message.author.id}`);
db.add(`countWord_${message.guild.id}_${message.author.id}`, count + 1);
}
});
`
and to see how many times specific user used it then use following code:
let count = db.fetch(`countWord_${message.guild.id}_${id of user}`);
message.channel.send(count)
推荐阅读
- javascript - 在 IntelliJ 中阻止 Typescript 自动完成
- algorithm - 在这种情况下,“给出整体运行时间”是什么意思?
- javascript - 如何将字符串转换为对象以便能够将其作为函数参数传递
- jquery-terminal - 反转 JQuery-Terminal 的流程
- elastic-stack - ELK,本地机器上的 Filebeat 设置,Logstash 不工作,伐木工人协议错误
- php - PHP表格行编号
- sql - 如何在 SAS EG 中获取行号列以简化按该唯一编号选择行?我想使用 PROC SQL 语法
- c# - 结构为 ReadOnlyMemory
表示 - node.js - 调用 Rest API 时 Node.js Discord 机器人消息为空
- racket - 球拍有点不一致