首页 > 解决方案 > Discordjs args 检查它是否是一个词

问题描述

我正在制作一个机器人来获取比特币价格并从不和谐中交换它。我想检查 args[0] 是否是一个数字,如果不是返回并发送消息。

        if (!args.length){
            return message.channel.send(`${message.author} You need to input arguments, example: -exchange 15000`);
        }


        const url = `https://blockchain.info/tobtc?currency=USD&value=${args[0]}`;

        request(url, (err, res, body) => {

标签: node.jsdiscorddiscord.js

解决方案


  • 使用args[0]而不是args.length
  • 如果你使用argsif(args)会返回一个布尔值
  • 所以你的代码应该是这样的:

if (!args[0]){
  return message.channel.send(`${message.author} You need to input arguments, example: -exchange 15000`);
}


推荐阅读