-
Notifications
You must be signed in to change notification settings - Fork 6
/
common.js
135 lines (117 loc) · 3.11 KB
/
common.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import jsonFile from "jsonfile";
import config from "./config.js";
let ioInstance;
let stats;
const encode = "encode";
const title = "title";
const tlc = "tlc";
const episode = "episode";
const time = "time";
const tl = "tl";
const ts = "ts";
const edit = "edit";
const qc = "qc";
export function initIo(http) {
ioInstance = require("socket.io")(http);
return ioInstance;
}
export function io() {
return ioInstance;
}
export var lastUpdated = new Date().toUTCString();
export var validCommands = ["encode", "tlc", "title", "episode", "time", "tl", "ts", "edit", "qc"];
export function getStats() {
if (!stats){
console.log("Reading existing data...".green);
const file = `${__dirname}/data.json`;
try {
stats = jsonFile.readFileSync(file);
}
catch (err) {
if (err.code === "ENOENT") {
//If no data file was found, start with dummy data
console.log("No default data file found".yellow);
console.log("Creating dummy data".yellow);
stats = {
"encode": 0,
"title": "Another show",
"episode": "5/12",
"time": "20",
"tl": "50",
"ts": 0,
"edit": "50",
"qc": "60",
"tlc": "20"
};
}
}
}
return stats;
}
export var file = `${__dirname}/data.json`;
export function saveStats() {
jsonFile.writeFileSync(file, getStats());
console.log("Saving stats to disk".yellow);
}
setInterval(saveStats, 1000 * 60 * 10); // save stats every 10 minutes
export function triggerMatch(text) {
return text.substring(0, config.trigger.length) === config.trigger;
}
export function getMsg(text) {
return text.substring(config.trigger.length);
}
export function getCommand(msg) {
return msg.substring(0, msg.indexOf(" "));
}
export function getValue(msg) {
return msg.substring(msg.indexOf(" ") + 1);
}
export function newTitleTrigger(command, value) {
const tempTitle = stats["title"];
if (command === "title" || command === "episode") {
console.log("Resetting everything".yellow);
for (const key in stats) {
if (stats.hasOwnProperty(key)) {
stats[key] = 0;
ioInstance.emit("update-stats", {
"command": key,
"value": 0
});
}
}
if (command === "episode") {
stats["title"] = tempTitle;
stats["episode"] = value;
ioInstance.emit("update-stats", {
"command": "title",
"value": tempTitle
});
}
}
}
export function getIRCtoSay(command) {
if (command === "episode") {
return `Currently working on \u0002${stats[title]}\u0002 episode ${stats[episode]}`;
}
else if (command !== "title") {
return `\u0002${stats[title]}\u0002 | Episode ${stats[episode]} | ${capitalizeFirst(command)} progress @ ${stats[command]}%`;
}
else return null;
}
export function getDiscordtoSay(command) {
if (command === "episode") {
return `Currently working on **${stats[title]}** episode ${stats[episode]}`;
}
else if (command !== "title") {
return `**${stats[title]}** | Episode ${stats[episode]} | ${capitalizeFirst(command)} progress @ ${stats[command]}%`;
}
else return null;
}
function capitalizeFirst(string) {
if (string.length > 3) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
else {
return string.toUpperCase();
}
}