Erkaloth
Erkaloth

Reputation: 33

Pick a random item from a file then send it. Discord bot

I want to create a command for my bot so that when a certain command is issued, it will get a random quote from a json file and send it in chat.
I know how to do it with an array but I don't know how to do it from a file.
EDIT:
The code I have so far:

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]);

Upvotes: 1

Views: 1791

Answers (1)

ForgetfulFellow
ForgetfulFellow

Reputation: 2632

Node.js makes it easy to load JSON files

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

From your comment, obj will be an array of Question objects that you can manipulate like any ordinary array.

Further reading at: https://www.codementor.io/codementorteam/how-to-use-json-files-in-node-js-85hndqt32

Upvotes: 1

Related Questions