-
Notifications
You must be signed in to change notification settings - Fork 14
/
publishInteractions.js
335 lines (290 loc) · 12.6 KB
/
publishInteractions.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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
require("./other/patchConsoleLog");
const { plsParseArgs } = require('plsargs');
const argv = plsParseArgs(process.argv.slice(2));
const chillout = require("chillout");
const { makeSureFolderExists, sleep } = require("stuffs");
const path = require("path");
const readdirRecursive = require("recursive-readdir");
const config = require("./config");
const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v9');
const Discord = require('discord.js');
globalThis.Underline = {
config,
Interaction: require('./types/Interaction'),
Event: require('./types/Event'),
ChatInput: require("./types/ChatInput"),
MessageAction: require("./types/MessageAction"),
UserAction: require("./types/UserAction"),
SelectMenu: require("./types/SelectMenu"),
Button: require("./types/Button"),
Locale: require("./types/Locale"),
Modal: require("./types/Modal"),
};
globalThis.Enums = {
ChannelType: Discord.ChannelType,
MessageType: Discord.MessageType,
ApplicationCommandOptionType: Discord.ApplicationCommandOptionType,
ActivityType: Discord.ActivityType,
AuditLogOptionsType: Discord.AuditLogOptionsType,
InteractionType: Discord.InteractionType,
ComponentType: Discord.ComponentType,
ButtonStyle: Discord.ButtonStyle,
TextInputStyle: Discord.TextInputStyle
}
let ogLangs = ["da", "de", "en-GB", "en-US", "es-ES", "fr", "hr", "it", "lt", "hu", "nl", "no", "pl", "pt-BR", "ro", "fi", "sv-SE", "vi", "tr", "cs", "el", "bg", "ru", "uk", "hi", "th", "zh-CN", "ja", "zh-TW", "ko"];
let interactionTypes = { "ChatInput": 1, "Message": 3, "User": 2 };
let optionTypes = { "SubCommand": 1, "SubCommandGroup": 2, "String": 3, "Integer": 4, "Boolean": 5, "User": 6, "Channel": 7, "Role": 8, "Mentionable": 9, "Number": 10, "Attachment": 11 };
async function getLocaleFilePaths() {
let localesPath = path.resolve("./locales");
await makeSureFolderExists(localesPath);
let VLocaleFiles = await readdirRecursive(localesPath);
VLocaleFiles = VLocaleFiles.filter(i => {
let state = path.basename(i).startsWith("-");
return !state;
});
return VLocaleFiles;
}
/** @type {[import("./types/Locale").LocaleString, import("./types/Locale").Locale][]} */
const locales = [];
(async () => {
await chillout.forEach(await getLocaleFilePaths(), (localeFile) => {
let locale = require(localeFile);
if (locale._type != "locale") return;
locales.push([locale.locale, locale]);
})
let dcInters = [];
let isClearMode = argv.get(0) == "guild" ? argv.get(2) == "clear" : (argv.get(0) == "global" ? argv.get(1) == "clear" : false);
let publishMode = argv.get(0) == "guild" ? "guild" : argv.get(0) == "global" ? "global" : null;
let publishSpecialType = publishMode == "guild" ? (isClearMode ? "guildOnly" : (argv.get(2) || "guildOnly")) : "globalOnly";
console.log(publishSpecialType)
if (!publishMode) {
console.error(`Geçersiz paylaşım modu! Geçerli modlar: guild, global`);
console.error(`Kullanım örneği: node publishInteractions.js guild <guildId> [clear]`);
console.error(`Kullanım örneği: node publishInteractions.js global [clear]`);
return process.exit(1);
}
if (!isClearMode) {
let interactionsFolder = path.resolve("./interactions");
await makeSureFolderExists(interactionsFolder);
/** @type {(import("./types/Interaction"))[]} */
let uInters = [];
console.info("Interaksiyon dosyaları okunuyor..")
let interactionFilePaths = await readdirRecursive(interactionsFolder);
interactionFilePaths = interactionFilePaths.filter(i => {
let state = path.basename(i).startsWith("-");
return !state;
});
let plFilePaths = await readdirRecursive(path.resolve("./plugins"));
let plInteractionsPaths = plFilePaths.filter(i => {
if (!i.match(new RegExp(`plugins\\${path.sep}(.|[şçğüiÇŞİĞÜIıöÖ])*\\${path.sep}interactions`))?.length) return false;
if (i.match(new RegExp(`plugins\\${path.sep}-(.|[şçğüiÇŞİĞÜIıöÖ])*\\${path.sep}interactions`))?.length) return false;
let state = path.basename(i).startsWith("-");
return !state;
});
interactionFilePaths = [...interactionFilePaths, ...plInteractionsPaths];
await chillout.forEach(interactionFilePaths, (interactionFilePath) => {
/** @type {import("./types/Interaction")} */
let uInter = require(interactionFilePath);
if (uInter?._type != "interaction") return;
if (!uInter.publishType) uInter.publishType = "globalOnly";
if (!((uInter.publishType == "all" && ["guildOnly", "globalOnly"].includes(publishSpecialType)) || publishSpecialType == uInter.publishType)) {
console.warn(`Interaksiyon "${uInter.actionType == "ChatInput" ? `/${uInter.name.join(" ")}` : `${uInter.name[0]}`}" dönüştürülme listesine eklenmedi çünkü interaksiyon paylaşılma tipi(${uInter.publishType}) ile paylaşma modu(${publishMode}) uyumsuz!`);
return;
}
console.info(`Interaksiyon "${uInter.actionType == "ChatInput" ? `/${uInter.name.join(" ")}` : `${uInter.name[0]}`}" dönüştürülme listesine eklendi!`);
uInters.push(uInter);
});
console.info("Interaksiyonlar discordun anlayacağı dile dönüştürülüyor..")
uInters = uInters.sort((a, b) => a.name.length - b.name.length)
dcInters = uInters.reduce((all, current) => {
switch (current.name.length) {
case 1: {
let localeData = findLocales(current.name);
all.push({
type: interactionTypes[current.actionType] ?? current.actionType,
name: current.name[0],
description: current.description,
defaultMemberPermissions: current.defaultPermission ? null : ["Administrator"],
dmPermission: !current.guildOnly,
options: current.options,
nameLocalizations: localeData.names(0),
descriptionLocalizations: localeData.descriptions,
});
break;
}
case 2: {
let baseItem = all.find((i) => {
return i.name == current.name[0] && (interactionTypes[i.type] ?? i.type) == (interactionTypes[current.actionType] ?? current.actionType)
});
let localeData = findLocales(current.name);
if (!baseItem) {
all.push({
type: interactionTypes[current.actionType] ?? current.actionType,
name: current.name[0],
nameLocalizations: localeData.names(0),
descriptionLocalizations: localeData.descriptions,
description: current.description,
defaultMemberPermissions: current.defaultPermission ? null : ["Administrator"],
dmPermission: !current.guildOnly,
options: [
{
type: 1,
description: current.description,
name: current.name[1],
nameLocalizations: localeData.names(1),
descriptionLocalizations: localeData.descriptions,
options: current.options
}
]
});
} else {
baseItem.options.push({
type: 1,
description: current.description,
name: current.name[1],
nameLocalizations: localeData.names(1),
descriptionLocalizations: localeData.descriptions,
options: current.options
})
}
break;
}
case 3: {
let level1Item = all.find((i) => {
return i.name == current.name[0] && (interactionTypes[i.type] ?? i.type) == (interactionTypes[current.actionType] ?? current.actionType)
});
let localeData = findLocales(current.name);
if (!level1Item) {
all.push({
type: interactionTypes[current.actionType] ?? current.actionType,
name: current.name[0],
defaultMemberPermissions: current.defaultPermission ? null : ["Administrator"],
dmPermission: !current.guildOnly,
nameLocalizations: localeData.names(0),
descriptionLocalizations: localeData.descriptions,
description: current.description,
options: [
{
type: 2,
name: current.name[1],
nameLocalizations: localeData.names(1),
descriptionLocalizations: localeData.descriptions,
description: current.description,
options: [
{
type: 1,
description: current.description,
name: current.name[2],
options: current.options,
nameLocalizations: localeData.names(2),
descriptionLocalizations: localeData.descriptions,
}
]
}
]
});
} else {
let level2Item = level1Item.options.find(i => {
return i.name == current.name[1] && (interactionTypes[i.type] ?? i.type) == 2
});
if (!level2Item) {
level1Item.options.push({
type: 2,
name: current.name[1],
nameLocalizations: localeData.names(1),
descriptionLocalizations: localeData.descriptions,
description: current.description,
options: [
{
type: 1,
description: current.description,
name: current.name[2],
options: current.options,
nameLocalizations: localeData.names(2),
descriptionLocalizations: localeData.descriptions,
}
]
})
} else {
level2Item.options.push({
type: 1,
description: current.description,
name: current.name[2],
options: current.options,
nameLocalizations: localeData.names(2),
descriptionLocalizations: localeData.descriptions,
})
}
}
}
break;
}
return all;
}, []);
function fixOptions(inter = {}) {
inter.type = optionTypes[inter.type] ?? inter.type;
if (inter?.options) {
inter.options.map(fixOptions)
return inter;
};
return inter;
}
for (let i = 0; i < dcInters.length; i++) {
dcInters[i] = fixOptions(dcInters[i]);
}
require("util").inspect(dcInters, false, 9999, true)
// return;
dcInters = dcInters.map(i => Discord.ApplicationCommandManager.transformCommand(i));
} else {
console.info("Hiçbir interaksiyon okunmadı, bütün var olanlar temizlenicek...");
}
const rest = new REST({ version: "9" }).setToken(config.clientToken);
console.info("Hesap bilgileri alınıyor!");
/** @type {import("discord-api-types/rest/v9/user").RESTGetAPIUserResult} */
const me = await rest.get(Routes.user());
console.info(`Hesap bilgileri alındı! ${me.username}#${me.discriminator} (${me.id})`);
console.info(`İnteraksiyonlar discorda gönderiliyor!`);
switch (publishMode) {
case "guild": {
let guildId = argv.get(1);
console.info(`Paylaşma modu: sunucu (${guildId})`);
await rest.put(Routes.applicationGuildCommands(me.id, guildId), { body: dcInters });
console.info(`Paylaşılan komutların gelmesi 3-5 saniye kadar sürebilir.`);
break;
}
case "global": {
console.info(`Paylaşma modu: global`);
await rest.put(Routes.applicationCommands(me.id), { body: dcInters });
console.info(`Paylaşılan komutların gelmesi 1 saat kadar sürebilir. Eğer hemen gelmesini istiyorsanız botunuzu sunucunuzdan atıp geri alabilirsiniz.`);
break;
}
}
console.info(`İnteraksiyonlar paylaşıldı!`);
})();
/**
* @param {string[]} commandName
*/
function findLocales(commandName) {
let commandNameJoined = commandName.join(" ");
let descriptions = {};
let names = {};
for (let i = 0; i < locales.length; i++) {
const [localeString, locale] = locales[i];
let commandLocale = locale.commands.find(i => i.originalName.join(" ") == commandNameJoined);
if (commandLocale) {
let aliases = ogLangs.filter(i => i.startsWith(localeString));
for (let j = 0; j < aliases.length; j++) {
const alias = aliases[j];
descriptions[alias] = commandLocale.description;
names[alias] = commandLocale.name;
}
}
}
return {
descriptions,
names(idx) {
return Object.fromEntries(Object.entries(names).map(i => [i[0], i[1][idx]]));
}
};
}