从文件中选择一个随机项然后发送它。 Discord bot

时间:2018-02-28 16:48:22

标签: javascript node.js file bots discord.js

我想为我的机器人创建一个命令,这样当发出某个命令时,它会从json文件中获取一个随机引用并在聊天中发送它。
我知道怎么用数组做,但我不知道如何从文件中做到这一点 修改
我到目前为止的代码:

let question = args.slice(0).join(' ');
    if (!question)
    return message.reply("Please ask a question!");
    var sayings = [ ':8ball: Absolutly.',
                    ':8ball: Absolutly not.',
                    ':8ball: It is true.',
                    ':8ball: Impossible.',
                    ':8ball: Of course.',
                    ':8ball: I do not think so.',
                    ':8ball: It is true.',
                    ':8ball: It is not true.',
                    ':8ball: I am very undoubtful of that.',
                    ':8ball: I am very doubtful of that.',
                    ':8ball: Sources point to no.',
                    ':8ball: Theories prove it.',
                    ':8ball: Reply hazy try again.',
                    ':8ball: Ask again later.',
                    ':8ball: Better not tell you now.',
                    ':8ball: Cannot predict now.',
                    ':8ball: Concentrate and ask again.'
        ];

        var result = Math.floor((Math.random() * sayings.length) + 0);
        message.channel.send(sayings[result]);

1 个答案:

答案 0 :(得分:1)

Node.js可以轻松加载JSON文件

const obj = require("../path/jsonfile.json");

从你的评论中,obj将是一个可以像任何普通数组一样操作的Question对象数组。

进一步阅读: https://www.codementor.io/codementorteam/how-to-use-json-files-in-node-js-85hndqt32