generated from RainyXeon/Mutsuki
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ClearSlash.mjs
116 lines (96 loc) · 2.88 KB
/
ClearSlash.mjs
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
import { REST } from "discord.js";
import { Routes } from "discord-api-types/v10";
import fs from "fs";
import _ from "lodash";
import { config } from "dotenv";
import { load } from "js-yaml";
config();
class ClearSlash {
constructor() {
this.execute();
}
async execute() {
const configData = this.ConfigData;
const rest = new REST({ version: "10" }).setToken(configData.bot.TOKEN);
const client = await rest.get(Routes.user());
rest
.put(Routes.applicationCommands(client.id), { body: [] })
.then(() => console.log("Successfully deleted all application commands."))
.catch(console.error);
}
get ConfigData() {
const yaml_files = this.YAMLPraseServices("./app.yml");
let doc;
const res = load(yaml_files);
doc = res;
if (process.env.DOCKER_COMPOSE_MODE) {
// Change lavalink data
const lavalink_changedata = doc.lavalink.NODES[0];
lavalink_changedata.url = String(process.env.NODE_URL);
lavalink_changedata.name = String(process.env.NODE_URL);
lavalink_changedata.auth = String(process.env.NODE_AUTH);
lavalink_changedata.secure = false;
// Change bot data
const bot_chagedata = doc.bot;
bot_chagedata.TOKEN = String(process.env.TOKEN);
// Change db data
const db_chnagedata = doc.features.DATABASE;
if (db_chnagedata.driver == "mongodb") {
db_chnagedata.config.uri = String(process.env.MONGO_URI);
}
}
return doc;
}
YAMLPraseServices(path) {
const boolean = ["true", "false", "null", "undefined"];
const line = this.readline(path);
const res_array = [];
for (let i = 0; i < line.length; i++) {
var element = line[i];
var re = /\${(.*?)\}/;
if (re.exec(element) !== null || re.exec(element)) {
const extract = re.exec(element);
if (
process.env[extract[1]] &&
boolean.includes(process.env[extract[1]].trim().toLowerCase())
) {
const boolean_prase_res = this.parseBoolean(process.env[extract[1]]);
res_array.push(
_.replace(element, extract[0], String(boolean_prase_res))
);
} else {
res_array.push(
_.replace(element, extract[0], process.env[extract[1]])
);
}
} else {
res_array.push(element);
}
}
return res_array.join("\r\n");
}
parseBoolean(value) {
if (typeof value === "string") {
value = value.trim().toLowerCase();
}
switch (value) {
case "true":
return true;
case "null":
return "null";
case "undefined":
return undefined;
default:
return false;
}
}
readline(path) {
const res_array = [];
const res = fs.readFileSync(path, "utf-8");
res.split(/\r?\n/).forEach(function (line) {
res_array.push(line);
});
return res_array;
}
}
new ClearSlash();