2017-12-07 02:11:14 +02:00
|
|
|
// @ts-ignore
|
|
|
|
const InternalConfig = require("./internal-config.json");
|
2017-09-14 15:09:43 +03:00
|
|
|
const Console = require("console");
|
|
|
|
const SimpleFileWriter = require("simple-file-writer");
|
|
|
|
|
2017-11-11 03:13:18 +02:00
|
|
|
const logWriter = new SimpleFileWriter("./console.log");
|
|
|
|
const debugLogWriter = new SimpleFileWriter("./debug.log");
|
2017-09-14 15:09:43 +03:00
|
|
|
|
|
|
|
function ask(client, textChannel, member, question) {
|
|
|
|
//return a promise which will resolve once the user next sends a message in this textChannel
|
|
|
|
return new Promise((resolve, reject) => {
|
2017-12-07 02:11:14 +02:00
|
|
|
const cancelAsk = () => {
|
|
|
|
client.removeListener("message", handler);
|
|
|
|
textChannel.send("Response to question timed out");
|
|
|
|
};
|
|
|
|
|
|
|
|
const askTimeout = setTimeout(cancelAsk, InternalConfig.askTimeout);
|
|
|
|
|
2017-09-14 15:09:43 +03:00
|
|
|
const handler = responseMessage => {
|
2017-12-07 02:11:14 +02:00
|
|
|
if (responseMessage.channel.id === textChannel.id && responseMessage.member.id === member.id) {
|
|
|
|
clearTimeout(askTimeout);
|
2017-09-14 15:09:43 +03:00
|
|
|
resolve(responseMessage);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-12-08 00:22:00 +02:00
|
|
|
client.on("message", handler);
|
2017-09-14 15:09:43 +03:00
|
|
|
|
|
|
|
textChannel.send(member.toString() + " " + question).catch(reject);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function dateLog(...args) {
|
2017-11-11 03:28:38 +02:00
|
|
|
doDateLog(Console.log, logWriter, args, "INFO");
|
2017-09-14 15:09:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function dateError(...args) {
|
2017-11-11 03:28:38 +02:00
|
|
|
doDateLog(Console.error, logWriter, args, "ERROR");
|
2017-11-11 03:13:18 +02:00
|
|
|
}
|
|
|
|
|
2017-12-03 18:04:10 +02:00
|
|
|
function dateDebugError(...args) {
|
|
|
|
doDateLog(null, null, args, "DEBUG ERROR");
|
|
|
|
}
|
|
|
|
|
2017-11-11 03:13:18 +02:00
|
|
|
function dateDebug(...args) {
|
2017-11-11 03:28:38 +02:00
|
|
|
doDateLog(null, null, args, "DEBUG");
|
2017-11-11 03:13:18 +02:00
|
|
|
}
|
|
|
|
|
2017-11-11 03:28:38 +02:00
|
|
|
function doDateLog(consoleMethod, fileWriter, args, prefix = "") {
|
|
|
|
args = formatArgs([`[${prefix}]`].concat(args));
|
2017-11-11 03:13:18 +02:00
|
|
|
|
|
|
|
if (consoleMethod !== null)
|
|
|
|
consoleMethod.apply(this, args);
|
|
|
|
|
|
|
|
if (fileWriter !== null)
|
|
|
|
fileWriter.write(formatArgsForFile(args));
|
|
|
|
|
|
|
|
debugLogWriter.write(formatArgsForFile(args));
|
2017-09-14 15:09:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function formatArgs(args) {
|
2017-11-11 03:28:38 +02:00
|
|
|
return [`[${new Date().toUTCString()}]`].concat(args);
|
2017-09-14 15:09:43 +03:00
|
|
|
}
|
|
|
|
|
2017-11-11 03:13:18 +02:00
|
|
|
function formatArgsForFile(args) {
|
|
|
|
return args.join("") + "\n";
|
|
|
|
}
|
|
|
|
|
2017-09-14 15:09:43 +03:00
|
|
|
module.exports = {
|
|
|
|
dateError,
|
|
|
|
dateLog,
|
2017-11-11 03:13:18 +02:00
|
|
|
dateDebug,
|
2017-12-03 18:04:10 +02:00
|
|
|
dateDebugError,
|
2017-09-14 15:09:43 +03:00
|
|
|
ask
|
|
|
|
};
|