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

问题描述 投票:1回答:1

我想为我的机器人创建一个命令,这样当发出某个命令时,它会从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]);
javascript node.js file bots discord.js
1个回答
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

© www.soinside.com 2019 - 2024. All rights reserved.