简体   繁体   English

discord.js bot 数学无法正常工作

[英]discord.js bot math not working right

So with my bot when someone purges a channel I want to add one to the purge amount they have then split the first value off so it doesn't purge their command, However when I try to add 1 to the value of the argument passed it just puts the number behind the value... Any help, I' really crappy at JS so ye...因此,对于我的机器人,当有人清除通道时,我想在清除量中添加一个,然后他们将第一个值分开,这样它就不会清除他们的命令,但是,当我尝试将 1 添加到传递给它的参数的值时只是将数字放在值后面......任何帮助,我对JS真的很糟糕所以你......

if (args[1]){
    var messagecount = args[1];
    var messagetotal = Math.floor(messagecount + 1); // Adding 1 to passed argument
    console.log(messagetotal); // Outputting to console, Console is reading the argument with 1 on the end EX: arg: 2 | output: 21
//  message.channel.fetchMessages({limit: messagecount}).then(messages => message.channel.bulkDelete(messages).then(messages => message.channel.send("Purged `" + messages.size + "` messages.")));
} else {
//  message.channel.send("Usage: !purge (Number)");
}

I don't know that the exact content of args[1] is, but regarding your explanation of the bug, it seems that the value is a number formatted as a string.我不知道args[1]的确切内容是什么,但是关于您对错误的解释,该值似乎是一个格式化为字符串的数字。

The difference:区别:

String字符串

args[1] = "1";
console.log(args[1] + 1);

Output: "11"输出: "11"

Integer整数

args[1] = 1;
console.log(args[1] + 1);

Output: 2输出: 2

If you add a number to a string, it will be appended and not added mathematically.如果将数字添加到字符串中,它将被追加而不是数学添加。

To format your numeric string to an integer, replace要将数字字符串格式化为整数,请替换

var messagecount = args[1];

through通过

var messagecount = parseInt(args[1]);

const math = require('mathjs');

if (args.length > 0)
          if (!message.member.hasPermission("MANAGE_MESSAGES"))
            message.reply('You do not have permission to use this command.')
          else {
            var amount = args.join(' '),
            total = math.evaluate(`${amount}+1`);
            message.channel.bulkDelete(total).catch(console.error);
          }
        else
          message.reply('You did not type an amount for me to delete.')
      break;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM