首页 > 解决方案 > 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 未定义。

总的来说,我需要帮助的是:

  1. 让机器人识别何时使用 ':p' 甚至在句子中间
  2. 然后让它检查它是否是某个人
  3. 最后,让它为变量 numberofp 添加 +1

如果您需要更多代码或信息,请询问:D

标签: javascriptdiscord.js

解决方案


我会说使用 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)

推荐阅读