从文件中选择一个随机项目然后发送。不和谐机器人
Pick a random item from a file then send it. Discord bot
我想为我的机器人创建一个命令,以便在发出某个命令时,它会从 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]);
Node.js 可以轻松加载 JSON 个文件
const obj = require("../path/jsonfile.json");
根据您的评论,obj
将是一个问题对象数组,您可以像处理任何普通数组一样操作这些对象。
进一步阅读:
https://www.codementor.io/codementorteam/how-to-use-json-files-in-node-js-85hndqt32
我想为我的机器人创建一个命令,以便在发出某个命令时,它会从 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]);
Node.js 可以轻松加载 JSON 个文件
const obj = require("../path/jsonfile.json");
根据您的评论,obj
将是一个问题对象数组,您可以像处理任何普通数组一样操作这些对象。
进一步阅读: https://www.codementor.io/codementorteam/how-to-use-json-files-in-node-js-85hndqt32