-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathutils.ts
More file actions
413 lines (331 loc) · 12.1 KB
/
utils.ts
File metadata and controls
413 lines (331 loc) · 12.1 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
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
import crypto from "crypto";
import { CacheType, ChatInputCommandInteraction, EmbedBuilder, GuildMember, PermissionFlagsBits } from "discord.js";
import { getChannels, getConfig } from "./config.js";
import { Dictionary } from "./dictionary.js";
const fcRegex = new RegExp(/[0-9]{4}-[0-9]{4}-[0-9]{4}/);
const pidRegex = new RegExp(/^\d+$/);
const config = getConfig();
const urlBase = `http://${config.wfcServer}:${config.wfcPort}`;
let currentColor = 0;
const colors = [
0xf38ba8,
0xfab387,
0xf9e2af,
0xa6e3a1,
0x89b4fa,
0xb4befe,
];
export function getColor() {
currentColor++;
if (currentColor >= colors.length)
currentColor = 0;
return colors[currentColor];
}
// Takes a string that's either an fc or pid and returns a pid
export function resolvePidFromString(fcOrPid: string) {
if (fcOrPid.includes("-"))
return parseInt(fcOrPid.replace(/-/g, ""), 10) >>> 0;
else
return parseInt(fcOrPid);
}
// Checks if friendCode or Pid is correct
export function validateID(fcOrPid: string): [boolean, string | null] {
if (fcOrPid == "")
return [false, "Empty fc or pid"];
if (fcOrPid.match(pidRegex))
return [true, null];
if (!fcOrPid.match(fcRegex))
return [false, "Invalid Format"];
// For FCs, check if they can convert to a pid and then back to the FC.
// Sometimes the conversion mangles the FC, in which case it's invalid.
const mangled = pidToFc(resolvePidFromString(fcOrPid));
const ret = fcOrPid == mangled;
return [ret, ret ? null : `Valid Format, but the FC would have been mangled to ${mangled}`];
}
export function pidToFc(pid: number) {
if (pid == 0)
return "0000-0000-0000";
else {
try {
const buffer = new Uint8Array(8);
// buffer is pid in little endian, followed by RMCJ in little endian
buffer[0] = pid >> 0;
buffer[1] = pid >> 8;
buffer[2] = pid >> 16;
buffer[3] = pid >> 24;
buffer[4] = ("J").charCodeAt(0); // the reversed online relevant game id
buffer[5] = ("C").charCodeAt(0);
buffer[6] = ("M").charCodeAt(0);
buffer[7] = ("R").charCodeAt(0);
const md5 = crypto.createHash("md5").update(buffer).digest();
let fc = ((BigInt(md5.at(0)! >> 1) << 32n) | BigInt(pid)).toString();
if (fc.length < 12)
fc = "0".repeat(12 - fc.length) + fc;
return `${fc.slice(0, 4)}-${fc.slice(4, 8)}-${fc.slice(8, 12)}`;
}
catch {
return "0000-0000-0000";
}
}
}
export function plural(count: number, text: string) {
return count == 1 ? text : text + "s";
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export async function makeRequest(route: string, method: string, data?: object): Promise<[boolean, any]> {
const url = urlBase + route;
try {
const response = await fetch(url, {
method: method,
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${config.wfcSecret}`
},
body: data ? JSON.stringify(data) : null
});
const rjson = await response.json();
if (response.ok && !rjson.Error)
return [true, rjson];
else {
console.error(`Failed to make request ${url}, response: ${rjson ? rjson.Error : "no error message provided"}`);
return [false, rjson];
}
}
catch (error) {
console.error(`Failed to make request ${url}, error: ${error}`);
return [false, { error: error }];
}
}
interface SendEmbedOpt {
name: string,
value: string,
hidden?: boolean,
}
export interface WiiLinkUser {
ProfileId: number,
UserId: number,
GsbrCode: string,
NgDeviceId: number[],
Email: string,
UniqueNick: string,
FirstName: string,
LastName: string,
Restricted: boolean,
RestrictedDeviceId: number,
BanReason: string,
OpenHost: boolean,
LastIPAddress: string,
LastInGameSn: string,
Csnum: string[],
DiscordID: string;
BanModerator: string,
BanReasonHidden: string,
BanIssued: string,
BanExpires: string,
}
export async function sendEmbedLog(interaction: ChatInputCommandInteraction<CacheType>, action: string, fc: string, user: WiiLinkUser, opts: SendEmbedOpt[], hideMiiName = false, noPublicEmbed = false) {
const miiName = user.LastInGameSn != "" ? user.LastInGameSn : "Unknown";
const member = interaction.member as GuildMember | null;
const thumbnail = getMiiImageURL(fc);
const privEmbed = new EmbedBuilder()
.setColor(getColor())
.setTitle(`${action.charAt(0).toUpperCase() + action.slice(1)} performed by ${member?.displayName ?? "Unknown"}`)
.addFields(
{ name: "Server", value: interaction.guild!.name },
{ name: "Moderator", value: `<@${member?.id ?? "Unknown"}>` },
{ name: "Friend Code", value: fc },
{ name: "Mii Name", value: miiName },
{ name: "IP", value: user.LastIPAddress != "" ? user.LastIPAddress : "Unknown" }
)
.setTimestamp();
privEmbed.setThumbnail(thumbnail);
console.log(thumbnail);
if (opts)
privEmbed.addFields(...opts);
await getChannels().logs.send({ embeds: [privEmbed] });
await interaction.reply({ content: `Successful ${action} performed on friend code "${fc}"` });
if (noPublicEmbed)
return;
const pubEmbed = new EmbedBuilder()
.setColor(getColor())
.setTitle(`${action.charAt(0).toUpperCase() + action.slice(1)} performed by moderator`)
.addFields(
{ name: "Friend Code", value: fc },
{ name: "Mii Name", value: hideMiiName ? "\\*\\*\\*\\*\\*" : miiName }
)
.setTimestamp();
if (!hideMiiName)
pubEmbed.setThumbnail(thumbnail);
if (opts) {
const filtered = opts.filter((opt) => !opt["hidden"]);
pubEmbed.addFields(...filtered);
}
await getChannels().publicLogs.send({ embeds: [pubEmbed] });
}
export function fmtHex(n: number): string {
let ret = n.toString(16).toUpperCase();
if (ret.length <= 4)
ret = "0".repeat(4 - ret.length) + ret;
else if (ret.length < 8)
ret = "0".repeat(8 - ret.length) + ret;
return "0x" + ret;
}
export function fmtTimeSpan(diff: number): string {
const days = Math.floor(diff / (60 * 60 * 24));
diff -= days * (60 * 60 * 24);
const hours = Math.floor(diff / (60 * 60));
diff -= hours * (60 * 60);
const mins = Math.floor(diff / (60));
diff -= mins * (60);
const seconds = Math.floor(diff);
return `${days} Days, ${hours} Hours, ${mins} Minutes, ${seconds} Seconds`;
}
export function resolveModRestrictPermission() {
return (PermissionFlagsBits as Dictionary<bigint>)[config.modRestrictPerm] as bigint;
}
const idRegex = new RegExp(/^\d+$/);
function fmtDeviceID(deviceIDs: number[]) {
if (!deviceIDs)
return "null";
let ret = "";
for (let i = 0; i < deviceIDs.length; i++) {
const deviceID = deviceIDs[i];
switch (deviceID) {
case 0x02000001:
ret += deviceID + " (internal)";
break;
case 0x403ac68:
ret += deviceID + " (dolphin)";
break;
case 0x0204cef9:
case 0x038c864b:
case 0x040e3f97:
case 0x04cb7515:
case 0x066deb49:
case 0x06bcc32d:
case 0x06d0437a:
case 0x089120c8:
case 0x0a305428:
case 0x0a447b97:
case 0x0a1e97cf: // Thanks gab
case 0x0e19d5ed:
case 0x0e31482b:
case 0x2428a8cb:
case 0x247dd10b:
ret += deviceID + " (leaked)";
break;
default:
ret += deviceID;
}
ret += (i + 1 == deviceIDs.length ? "" : ", ");
}
return ret;
}
export function createUserEmbed(user: WiiLinkUser, priv: boolean): EmbedBuilder {
const fc = pidToFc(user.ProfileId);
const embed = new EmbedBuilder()
.setColor(getColor())
.setTitle(`Player info for friend code ${fc}`)
.setThumbnail(getMiiImageURL(fc))
.setTimestamp();
console.log(getMiiImageURL(fc));
let issuedDate = Date.parse(user.BanIssued);
let expiresDate = Date.parse(user.BanExpires);
let banLengthStr = null;
let expiredBan = false;
if (!isNaN(expiresDate) && !isNaN(expiresDate)) {
issuedDate = Math.round(issuedDate / 1000);
expiresDate = Math.round(expiresDate / 1000);
if (expiresDate < Date.now() / 1000) {
expiredBan = true;
user.Restricted = false;
}
banLengthStr = fmtTimeSpan(expiresDate - issuedDate);
}
embed.addFields(
{ name: "Profile ID", value: `${user.ProfileId}` },
{ name: "Mii Name", value: `${user.LastInGameSn}` },
{ name: "Open Host", value: `${user.OpenHost}` },
{ name: "Banned", value: `${user.Restricted}${expiredBan ? " (Expired)" : ""}` },
{ name: "Discord ID", value: user.DiscordID.length != 0 ? `<@${user.DiscordID}>` : "None Linked" }
);
if (user.Restricted || expiredBan) {
if (priv) {
let banModerator;
if (!user.BanModerator || user.BanModerator == "" || user.BanModerator == "admin")
banModerator = "Unknown";
else if (user.BanModerator.match(idRegex))
banModerator = `<@${user.BanModerator}>`;
else
banModerator = user.BanModerator;
embed.addFields({ name: "Ban Moderator", value: `${banModerator}` });
}
embed.addFields({ name: "Ban Reason", value: `${user.BanReason}` });
if (priv) {
embed.addFields({
name: "Hidden Reason",
value: `${user.BanReasonHidden && user.BanReasonHidden.length != 0 ? user.BanReasonHidden : "None"}`
});
}
embed.addFields(
{ name: "Ban Issued", value: `<t:${issuedDate}:F>` },
{ name: "Ban Expires", value: `<t:${expiresDate}:F>` },
{ name: "Ban Length", value: `${banLengthStr ?? "Unknown"}` },
);
}
if (priv) {
const csnums = user.Csnum?.join(", ") ?? "null";
embed.addFields(
{ name: "User ID", value: `${user.UserId}` },
{ name: "Gsbr Code", value: `${user.GsbrCode}` },
{ name: "NG Device IDs", value: `${fmtDeviceID(user.NgDeviceId)}` },
{ name: "Email", value: `${user.Email}` },
{ name: "Unique Nick", value: `${user.UniqueNick}` },
{ name: "First Name", value: `${user.FirstName}` },
{ name: "Last Name", value: `${user.LastName}` },
{ name: "Last IP Address", value: `${user.LastIPAddress}` },
{ name: "IP Info", value: `https://ipinfo.io/${user.LastIPAddress}` },
{ name: "Console Serial Numbers", value: `${csnums.length <= 1024 ? csnums : "Too many Serial Numbers!"}` },
);
}
return embed;
}
export async function haste(body: string): Promise<[number, string, string]> {
const res = await fetch("https://paste.ppeb.me/documents", {
method: "POST",
body: body,
});
if (!res.ok)
return [res.status, "", res.statusText];
const key = (await res.json()).key;
return [200, `https://paste.ppeb.me/${key}`, ""];
}
export function throwInline(err: string) {
throw new Error(err);
}
export async function queryJson(url: string) {
const response = await fetch(url);
if (!response.ok) {
console.error(`Unable to fetch groups, status code: ${response.status}`);
return null;
}
const json = await response.json();
if (!json) {
console.error(`Invalid response from ${url}, unable to populate groups!`);
return null;
}
return json;
}
export function wrapTryCatch(fn: () => void) {
return () => {
try {
fn();
}
catch (e) {
console.error(e);
}
};
}
export function getMiiImageURL(fc: string) {
return config.miiEndPoint.replace("{fc}", fc);
}