2020-01-02 22:56:50 +00:00
|
|
|
const chalk = require("chalk"),
|
|
|
|
fs = require("fs");
|
2019-11-23 19:06:31 +00:00
|
|
|
|
|
|
|
// Defines the function of this module
|
|
|
|
const MODULE_FUNCTION = "handle_console",
|
|
|
|
|
|
|
|
// Base path for module folder creation and navigation
|
2020-01-02 22:56:50 +00:00
|
|
|
BASE_PATH = "/consoleHelper";
|
|
|
|
|
|
|
|
// Only ran on startup so using sync functions is fine
|
|
|
|
// Makes the folders for files of the module
|
|
|
|
if (!fs.existsSync(__dirname + BASE_PATH)) {
|
|
|
|
fs.mkdirSync(__dirname + BASE_PATH);
|
|
|
|
console.log(`[consoleHelper] Made consoleHelper module folder`);
|
|
|
|
}
|
2020-01-04 12:14:45 +00:00
|
|
|
// Creates the consoleHelper config file
|
2020-01-02 22:56:50 +00:00
|
|
|
if (!fs.existsSync(__dirname + BASE_PATH + "/config.json")) {
|
2020-03-26 16:25:25 +00:00
|
|
|
fs.writeFileSync(__dirname + BASE_PATH + "/config.json", `{\n\t"24hour":true\n}`);
|
2020-01-02 22:56:50 +00:00
|
|
|
console.log(`[consoleHelper] Made consoleHelper config file`);
|
|
|
|
}
|
|
|
|
|
2020-01-04 12:27:12 +00:00
|
|
|
// Load in config
|
2020-01-02 22:56:50 +00:00
|
|
|
const config = require(__dirname + BASE_PATH + "/config.json");
|
2019-11-23 19:06:31 +00:00
|
|
|
|
|
|
|
module.exports = {
|
2020-01-04 12:27:12 +00:00
|
|
|
// Prints a bit of information to the console
|
2019-11-23 19:06:31 +00:00
|
|
|
printInfo:function(emoji, s) {
|
|
|
|
console.log(chalk.green(`[${this.getTime24()}] `)+chalk.bgGreen(chalk.black(" INFO "))+` ${emoji} ${s}`);
|
|
|
|
},
|
|
|
|
|
2020-01-04 12:27:12 +00:00
|
|
|
// Prints a warning to the console
|
2019-11-23 19:06:31 +00:00
|
|
|
printWarn:function(emoji, s) {
|
|
|
|
console.warn(chalk.green(`[${this.getTime24()}] `)+chalk.bgYellow(chalk.black(" WARN "))+` ${emoji} ${s}`);
|
|
|
|
},
|
|
|
|
|
2020-01-04 12:27:12 +00:00
|
|
|
// Prints an error to the console
|
2019-11-23 19:06:31 +00:00
|
|
|
printError:function(emoji, s) {
|
|
|
|
console.error(chalk.green(`[${this.getTime24()}] `)+chalk.bgRed(chalk.black(" ERROR "))+` ${emoji} ${s}`);
|
|
|
|
},
|
|
|
|
|
|
|
|
getTime24:function() {
|
|
|
|
const time = new Date();
|
2020-01-04 12:14:45 +00:00
|
|
|
// Check if the user wants 24 hour or 12 hour time
|
2020-01-02 22:56:50 +00:00
|
|
|
if (config["24hour"]) {
|
2020-01-04 12:14:45 +00:00
|
|
|
// User wants 24 hour time, leave it as it is.
|
2020-01-02 22:56:50 +00:00
|
|
|
return `${correctValue(time.getHours())}:${correctValue(time.getMinutes())}:${correctValue(time.getSeconds())}`;
|
|
|
|
} else {
|
2020-01-04 12:14:45 +00:00
|
|
|
// User wants 12 hour time, process it for that.
|
2020-01-02 22:56:50 +00:00
|
|
|
return this.t2412(`${correctValue(time.getHours())}:${correctValue(time.getMinutes())}:${correctValue(time.getSeconds())}`);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2020-01-04 12:27:12 +00:00
|
|
|
// Function for converting 24 hour to 12 hour time
|
2020-01-02 22:56:50 +00:00
|
|
|
t2412:function(inp) {
|
2020-01-04 12:14:45 +00:00
|
|
|
// Check what time it is, AM or PM.
|
2020-01-02 22:56:50 +00:00
|
|
|
if (parseInt(inp.split(":")[0]) > 11) {
|
2020-01-04 12:14:45 +00:00
|
|
|
// It's over 11 so it's PM
|
2020-01-02 22:56:50 +00:00
|
|
|
const i = inp.split(":");
|
|
|
|
let i1 = parseInt(i[0]) - 12;
|
|
|
|
if (i1 == 0) i1 = 12;
|
|
|
|
return i1 + ":" + i[1] + " PM";
|
|
|
|
} else {
|
2020-01-04 12:14:45 +00:00
|
|
|
// It's lower than 12 so it's AM
|
2020-01-02 22:56:50 +00:00
|
|
|
const i = inp.split(":");
|
|
|
|
let i1 = parseInt(i[0]);
|
|
|
|
if (i1 == 0) i1 = 12;
|
|
|
|
return i1 + ":" + i[1] + " AM";
|
|
|
|
}
|
2019-11-23 19:06:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports.MOD_FUNC = MODULE_FUNCTION;
|
|
|
|
|
2020-01-04 12:14:45 +00:00
|
|
|
// Date returns numbers without a 0 in front of them of course so this adds them.
|
2019-11-23 19:06:31 +00:00
|
|
|
function correctValue(i) {
|
|
|
|
if (i < 10) {
|
|
|
|
return "0"+i;
|
|
|
|
} else {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|