【问题标题】:Count how many times a word has been sent by a person with discord.js使用 discord.js 计算一个单词发送了多少次
【发布时间】:2021-06-10 22:10:35
【问题描述】:

所以我正在尝试制作一个机器人来计算有人说有趣的次数并将其存储在 .json 文件中我可以让它设置 .json 并说有人说过一次但它不会高于 1。提前谢谢你,这是我的代码

const Discord = require("discord.js");
const bot = new Discord.Client();
const prefix = "!";
const token = "token";
const fs = require("fs");
Discord.Client.msg = require ("./msgs.json");

bot.on("ready", () =>{
    console.log(`${bot.user.username} online`);
});

bot.on("message", msg => {
    if(msg.author.bot) return;

    const say = msg.content.toLowerCase()

    if(say === "are you online?"){
        msg.reply("I am online and ready to be a funny");
    }
    if(say === "v"){
        msg.reply("is stupid @everyone");
    }
    else if (say === "funny"){

        if(![msg.author.username]){
            Discord.Client.msg [msg.author.username] = {
                funnycount: 1
            }
        }
        else{
            Discord.Client.msg [msg.author.username] = {
                funnycount: +1
            }
            msg.reply("it worked");
        }
        fs.writeFile ("./msgs.json", JSON.stringify (Discord.Client.msg, null, 4), err => {
            if (err )throw err;
        });
    }
});

bot.on("message", msg =>{
    if(!msg.content.startsWith(prefix) || msg.author.bot) return;

    const args = msg.content.slice(prefix.length).split(" ");
    const command = args.shift().toLowerCase();

    if(command == "ping"){
        msg.channel.send("pong!");
    }
});

bot.login(token);

【问题讨论】:

    标签: javascript discord discord.js


    【解决方案1】:

    这部分代码是问题:

    Discord.Client.msg [msg.author.username] = {
      funnycount: +1
    }
    

    +1 不会将值加 1,它只是将 1 加到无,这将始终返回 1。相反,您需要获取当前值并加 1。您可以通过将代码更改为:

    Discord.Client.msg [msg.author.username] = {
      funnycount: Discord.Client.msg[msg.author.username].funnycount++
    }
    

    使用 ++ 将值增加 1。

    【讨论】:

    • 我忘记添加 Discord.Client.msg 以检查if(!Discord.Client.msg[msg.author.username]){ Discord.Client.msg [msg.author.username] = { nwordcount: 1 }
    猜你喜欢
    • 2020-05-02
    • 2021-06-25
    • 1970-01-01
    • 1970-01-01
    • 2019-11-28
    • 2011-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多