-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathinit.js
More file actions
206 lines (186 loc) · 4.58 KB
/
init.js
File metadata and controls
206 lines (186 loc) · 4.58 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
const { Client, GatewayIntentBits } = require("discord.js");
const path = require("path");
const yaml = require("yaml");
const { QuickDB } = require("quick.db");
const fs = require("fs");
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMembers,
],
});
const configFile = fs.readFileSync("./config.yml", "utf8");
let localeFile = "";
try {
localeFile = fs.readFileSync("./locale.yml", "utf8");
} catch {
console.log("No locale.yml found, proceeding with config.yml only.");
}
function isObject(item) {
return item && typeof item === "object" && !Array.isArray(item);
}
function deepMerge(target, source) {
if (Array.isArray(target) && Array.isArray(source)) {
if (
target.length > 0 &&
isObject(target[0]) &&
target[0].id !== undefined
) {
const result = [...target];
for (const sourceItem of source) {
if (!isObject(sourceItem) || sourceItem.id === undefined) {
result.push(sourceItem);
continue;
}
const targetIndex = result.findIndex((t) => t.id === sourceItem.id);
if (targetIndex !== -1) {
result[targetIndex] = deepMerge(result[targetIndex], sourceItem);
} else {
result.push(sourceItem);
}
}
return result;
}
return source;
}
if (isObject(target) && isObject(source)) {
const result = { ...target };
for (const key in source) {
if (isObject(source[key]) || Array.isArray(source[key])) {
if (key in target) {
result[key] = deepMerge(target[key], source[key]);
} else {
result[key] = source[key];
}
} else {
result[key] = source[key];
}
}
return result;
}
return source;
}
const configData = yaml.parse(configFile) || {};
const localeData = (localeFile ? yaml.parse(localeFile) : {}) || {};
globalThis.config = deepMerge(configData, localeData);
if (config.dbPath === undefined) {
config.dbPath = path.join(__dirname, "data");
} else if (config.dbPath?.includes("{root}")) {
config.dbPath = config.dbPath.replace("{root}", __dirname);
}
const dataDir = path.resolve(config.dbPath);
if (!config.silentStartup) {
console.log(`Using data directory: ${dataDir}`);
}
if (!fs.existsSync(dataDir)) {
fs.mkdirSync(dataDir, { recursive: true });
}
const mainDB = new QuickDB({ filePath: path.join(dataDir, "main.sqlite") });
const ticketsDB = new QuickDB({
filePath: path.join(dataDir, "tickets.sqlite"),
});
const blacklistDB = new QuickDB({
filePath: path.join(dataDir, "blacklist.sqlite"),
});
(async function () {
const defaultKeys = [
{ key: "totalTickets", value: 1 },
{ key: "openTickets", value: 0 },
{ key: "totalClaims", value: 0 },
{ key: "totalReviews", value: 0 },
{ key: "ratings", value: [] },
{ key: "totalMessages", value: 0 },
{ key: "ticketCreators", value: [] },
];
await Promise.all(
defaultKeys.map(async ({ key, value }) => {
if (!(await mainDB.has(key))) {
await mainDB.set(key, value);
}
}),
);
})();
// Extract information from the config.yml to properly setup the ticket categories
const ticketCategories = [];
config.TicketCategories.forEach((category) => {
const {
id,
name,
nameEmoji,
categoryID,
closedCategoryID,
support_role_ids,
permissions,
pingRoles,
ping_role_ids,
ghostPingRoles,
textContent,
creatorRoles,
buttonEmoji,
buttonLabel,
buttonStyle,
menuEmoji,
menuLabel,
menuDescription,
embedTitle,
color,
description,
ticketName,
ticketTopic,
slowmode,
useCodeBlocks,
modal,
modalTitle,
questions,
} = category;
const extractedQuestions = questions.map((question) => {
const { label, placeholder, style, required, minLength, maxLength } =
question;
return {
label,
placeholder,
style,
required,
minLength,
maxLength,
};
});
ticketCategories[id] = {
name,
nameEmoji,
categoryID,
closedCategoryID,
support_role_ids,
permissions,
pingRoles,
ping_role_ids,
ghostPingRoles,
textContent,
creatorRoles,
buttonEmoji,
buttonLabel,
buttonStyle,
menuEmoji,
menuLabel,
menuDescription,
embedTitle,
color,
description,
ticketName,
ticketTopic,
slowmode,
useCodeBlocks,
modal,
modalTitle,
questions: extractedQuestions,
};
});
module.exports = {
client,
ticketCategories,
mainDB,
ticketsDB,
blacklistDB,
};