1 |
rakinar2 |
577 |
/** |
2 |
|
|
* This file is part of SudoBot. |
3 |
|
|
* |
4 |
|
|
* Copyright (C) 2021-2023 OSN Developers. |
5 |
|
|
* |
6 |
|
|
* SudoBot is free software; you can redistribute it and/or modify it |
7 |
|
|
* under the terms of the GNU Affero General Public License as published by |
8 |
|
|
* the Free Software Foundation, either version 3 of the License, or |
9 |
|
|
* (at your option) any later version. |
10 |
|
|
* |
11 |
|
|
* SudoBot is distributed in the hope that it will be useful, but |
12 |
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
|
|
* GNU Affero General Public License for more details. |
15 |
|
|
* |
16 |
|
|
* You should have received a copy of the GNU Affero General Public License |
17 |
|
|
* along with SudoBot. If not, see <https://www.gnu.org/licenses/>. |
18 |
|
|
*/ |
19 |
|
|
|
20 |
|
|
import { Snowflake } from "discord.js"; |
21 |
|
|
import { Response } from "express"; |
22 |
|
|
import { existsSync } from "fs"; |
23 |
|
|
import fs, { rm } from "fs/promises"; |
24 |
|
|
import path from "path"; |
25 |
|
|
import tar from "tar"; |
26 |
|
|
import { z } from "zod"; |
27 |
|
|
import Client from "../core/Client"; |
28 |
|
|
import EventListener from "../core/EventListener"; |
29 |
|
|
import { Extension } from "../core/Extension"; |
30 |
|
|
import Service from "../core/Service"; |
31 |
|
|
import type { ClientEvents } from "../types/ClientEvents"; |
32 |
|
|
import { ExtensionInfo } from "../types/ExtensionInfo"; |
33 |
|
|
import { log, logDebug, logError, logInfo, logWarn } from "../utils/Logger"; |
34 |
|
|
import { cache } from "../utils/cache"; |
35 |
|
|
import { downloadFile } from "../utils/download"; |
36 |
|
|
import { request, sudoPrefix, wait } from "../utils/utils"; |
37 |
|
|
|
38 |
|
|
export const name = "extensionService"; |
39 |
|
|
|
40 |
|
|
const guildIdResolvers = [ |
41 |
|
|
{ |
42 |
|
|
events: ["applicationCommandPermissionsUpdate"], |
43 |
|
|
resolver: ([data]: ClientEvents["applicationCommandPermissionsUpdate"]) => data.guildId |
44 |
|
|
}, |
45 |
|
|
{ |
46 |
|
|
events: [ |
47 |
|
|
"autoModerationActionExecution", |
48 |
|
|
"autoModerationRuleCreate", |
49 |
|
|
"autoModerationRuleDelete", |
50 |
|
|
"autoModerationRuleUpdate" |
51 |
|
|
], |
52 |
|
|
resolver: ([data]: ClientEvents[ |
53 |
|
|
| "autoModerationActionExecution" |
54 |
|
|
| "autoModerationRuleCreate" |
55 |
|
|
| "autoModerationRuleDelete" |
56 |
|
|
| "autoModerationRuleUpdate"]) => data?.guild.id ?? undefined |
57 |
|
|
}, |
58 |
|
|
{ |
59 |
|
|
events: [ |
60 |
|
|
"messageCreate", |
61 |
|
|
"normalMessageCreate", |
62 |
|
|
"normalMessageDelete", |
63 |
|
|
"normalMessageUpdate", |
64 |
|
|
"messageDelete", |
65 |
|
|
"messageUpdate", |
66 |
|
|
"interactionCreate" |
67 |
|
|
], |
68 |
|
|
resolver: ([data]: ClientEvents[ |
69 |
|
|
| "messageCreate" |
70 |
|
|
| "messageDelete" |
71 |
|
|
| "messageUpdate" |
72 |
|
|
| "interactionCreate" |
73 |
|
|
| "normalMessageCreate" |
74 |
|
|
| "normalMessageUpdate" |
75 |
|
|
| "normalMessageDelete"]) => data?.guild?.id ?? data?.guildId ?? undefined |
76 |
|
|
}, |
77 |
|
|
{ |
78 |
|
|
events: ["messageDeleteBulk"], |
79 |
|
|
resolver: ([data]: ClientEvents["messageDeleteBulk"]) => data.first()?.guildId ?? undefined |
80 |
|
|
}, |
81 |
|
|
{ |
82 |
|
|
events: ["channelCreate", "channelDelete", "channelUpdate", "channelPinsUpdate"], |
83 |
|
|
resolver: ([data]: ClientEvents["channelCreate" | "channelDelete" | "channelUpdate" | "channelPinsUpdate"]) => |
84 |
|
|
data.isDMBased() ? undefined : data.guildId |
85 |
|
|
}, |
86 |
|
|
{ |
87 |
|
|
events: ["emojiCreate", "emojiDelete", "emojiUpdate"], |
88 |
|
|
resolver: ([data]: ClientEvents["emojiCreate" | "emojiDelete" | "emojiUpdate"]) => data?.guild?.id ?? undefined |
89 |
|
|
}, |
90 |
|
|
{ |
91 |
|
|
events: ["messageReactionAdd", "messageReactionRemove", "messageReactionRemoveEmoji"], |
92 |
|
|
resolver: ([data]: ClientEvents["messageReactionAdd" | "messageReactionRemove" | "messageReactionRemoveEmoji"]) => |
93 |
|
|
data?.message.guildId ?? undefined |
94 |
|
|
}, |
95 |
|
|
{ |
96 |
|
|
events: ["messageReactionRemoveAll"], |
97 |
|
|
resolver: ([data]: ClientEvents["messageReactionRemoveAll"]) => data?.guildId ?? undefined |
98 |
|
|
}, |
99 |
|
|
{ |
100 |
|
|
events: ["guildAuditLogEntryCreate", "guildMembersChunk", "threadListSync"], |
101 |
|
|
resolver: ([, data]: ClientEvents["guildAuditLogEntryCreate" | "guildMembersChunk" | "threadListSync"]) => |
102 |
|
|
data.id ?? undefined |
103 |
|
|
}, |
104 |
|
|
{ |
105 |
|
|
events: ["guildAvailable", "guildCreate", "guildDelete", "guildUpdate", "guildUnavailable", "guildIntegrationsUpdate"], |
106 |
|
|
resolver: ([data]: ClientEvents[ |
107 |
|
|
| "guildAvailable" |
108 |
|
|
| "guildCreate" |
109 |
|
|
| "guildUpdate" |
110 |
|
|
| "guildUnavailable" |
111 |
|
|
| "guildIntegrationsUpdate"]) => data.id ?? undefined |
112 |
|
|
}, |
113 |
|
|
{ |
114 |
|
|
events: [ |
115 |
|
|
"guildBanAdd", |
116 |
|
|
"guildBanRemove", |
117 |
|
|
"guildMemberAdd", |
118 |
|
|
"guildMemberRemove", |
119 |
|
|
"guildMemberUpdate", |
120 |
|
|
"guildMemberAvailable", |
121 |
|
|
"inviteCreate", |
122 |
|
|
"inviteDelete", |
123 |
|
|
"roleCreate", |
124 |
|
|
"roleDelete" |
125 |
|
|
], |
126 |
|
|
resolver: ([data]: ClientEvents[ |
127 |
|
|
| "guildBanAdd" |
128 |
|
|
| "guildBanRemove" |
129 |
|
|
| "guildMemberAdd" |
130 |
|
|
| "guildMemberRemove" |
131 |
|
|
| "guildMemberUpdate" |
132 |
|
|
| "guildMemberAvailable" |
133 |
|
|
| "inviteCreate" |
134 |
|
|
| "inviteDelete" |
135 |
|
|
| "roleCreate" |
136 |
|
|
| "roleDelete"]) => data.guild?.id ?? undefined |
137 |
|
|
}, |
138 |
|
|
{ |
139 |
|
|
events: [ |
140 |
|
|
"guildScheduledEventCreate", |
141 |
|
|
"guildScheduledEventDelete", |
142 |
|
|
"guildScheduledEventUserAdd", |
143 |
|
|
"guildScheduledEventUserRemove" |
144 |
|
|
], |
145 |
|
|
resolver: ([data]: ClientEvents[ |
146 |
|
|
| "guildScheduledEventCreate" |
147 |
|
|
| "guildScheduledEventDelete" |
148 |
|
|
| "guildScheduledEventUserAdd" |
149 |
|
|
| "guildScheduledEventUserRemove"]) => data.guild?.id ?? data.guildId ?? undefined |
150 |
|
|
}, |
151 |
|
|
{ |
152 |
|
|
events: ["guildScheduledEventUpdate"], |
153 |
|
|
resolver: ([data]: ClientEvents["guildScheduledEventUpdate"]) => data?.guild?.id ?? data?.guildId ?? undefined |
154 |
|
|
}, |
155 |
|
|
{ |
156 |
|
|
events: ["presenceUpdate", "roleUpdate", "stageInstanceUpdate", "stickerUpdate", "threadUpdate", "voiceStateUpdate"], |
157 |
|
|
resolver: ([data, data2]: ClientEvents[ |
158 |
|
|
| "presenceUpdate" |
159 |
|
|
| "roleUpdate" |
160 |
|
|
| "stageInstanceUpdate" |
161 |
|
|
| "threadUpdate" |
162 |
|
|
| "voiceStateUpdate"]) => data?.guild?.id ?? data2.guild?.id ?? undefined |
163 |
|
|
}, |
164 |
|
|
{ |
165 |
|
|
events: ["stageInstanceDelete", "stageInstanceCreate", "stickerCreate", "stickerDelete", "threadCreate", "threadDelete"], |
166 |
|
|
resolver: ([data]: ClientEvents[ |
167 |
|
|
| "stageInstanceDelete" |
168 |
|
|
| "stageInstanceCreate" |
169 |
|
|
| "stickerCreate" |
170 |
|
|
| "stickerDelete" |
171 |
|
|
| "threadCreate" |
172 |
|
|
| "threadDelete"]) => data?.guild?.id ?? undefined |
173 |
|
|
}, |
174 |
|
|
{ |
175 |
|
|
events: ["threadMemberUpdate"], |
176 |
|
|
resolver: ([data, data2]: ClientEvents["threadMemberUpdate"]) => |
177 |
|
|
data?.guildMember?.guild.id ?? data2?.guildMember?.guild.id ?? undefined |
178 |
|
|
}, |
179 |
|
|
{ |
180 |
|
|
events: ["typingStart", "webhookUpdate"], |
181 |
|
|
resolver: ([data]: ClientEvents["typingStart" | "webhookUpdate"]) => data.guild?.id ?? undefined |
182 |
|
|
}, |
183 |
|
|
{ |
184 |
|
|
events: ["command"], |
185 |
|
|
resolver: ([, , , data]: ClientEvents["command"]) => data.guildId ?? undefined |
186 |
|
|
}, |
187 |
|
|
{ |
188 |
|
|
events: [ |
189 |
|
|
"cacheSweep", |
190 |
|
|
"debug", |
191 |
|
|
"error", |
192 |
|
|
"warn", |
193 |
|
|
"invalidated", |
194 |
|
|
"ready", |
195 |
|
|
"shardReady", |
196 |
|
|
"shardDisconnect", |
197 |
|
|
"shardError", |
198 |
|
|
"shardReconnecting", |
199 |
|
|
"shardResume" |
200 |
|
|
], |
201 |
|
|
resolver: () => null |
202 |
|
|
} |
203 |
|
|
] as Array<{ |
204 |
|
|
events: ReadonlyArray<keyof ClientEvents>; |
205 |
|
|
resolver: Resolver; |
206 |
|
|
}>; |
207 |
|
|
|
208 |
|
|
type Resolver = (args: ClientEvents[keyof ClientEvents]) => Snowflake | null | undefined; |
209 |
|
|
|
210 |
|
|
function getGuildIdResolversMap() { |
211 |
|
|
const map = new Map<keyof ClientEvents, Resolver>(); |
212 |
|
|
|
213 |
|
|
for (const guildIdResolver of guildIdResolvers) { |
214 |
|
|
for (const event of guildIdResolver.events) { |
215 |
|
|
if (map.has(event)) { |
216 |
|
|
logWarn("Overlapping Guild ID Resolvers detected: ", event); |
217 |
|
|
logWarn("This seems to be an internal bug. Please report this issue to the developers."); |
218 |
|
|
} |
219 |
|
|
|
220 |
|
|
map.set(event, guildIdResolver.resolver); |
221 |
|
|
} |
222 |
|
|
} |
223 |
|
|
|
224 |
|
|
return map; |
225 |
|
|
} |
226 |
|
|
|
227 |
|
|
const extensionMetadataSchema = z.object({ |
228 |
|
|
main: z.string().optional(), |
229 |
|
|
commands: z.string().optional(), |
230 |
|
|
services: z.string().optional(), |
231 |
|
|
events: z.string().optional(), |
232 |
|
|
language: z.enum(["typescript", "javascript"]).optional(), |
233 |
|
|
main_directory: z.string().optional(), |
234 |
|
|
build_command: z.string().optional(), |
235 |
|
|
resources: z.string().optional(), |
236 |
|
|
name: z.string().optional(), |
237 |
|
|
description: z.string().optional(), |
238 |
|
|
id: z.string({ required_error: "Extension ID is required" }), |
239 |
|
|
icon: z.string().optional(), |
240 |
|
|
readmeFileName: z.string().default("README.md") |
241 |
|
|
}); |
242 |
|
|
|
243 |
|
|
export default class ExtensionService extends Service { |
244 |
|
|
private readonly extensionIndexURL = |
245 |
|
|
"https://raw.githubusercontent.com/onesoft-sudo/sudobot/main/extensions/.extbuilds/index.json"; |
246 |
|
|
protected readonly extensionsPath = process.env.EXTENSIONS_DIRECTORY; |
247 |
|
|
protected readonly guildIdResolvers = getGuildIdResolversMap(); |
248 |
|
|
|
249 |
|
|
private readonly downloadProgressStreamEOF = "%"; |
250 |
|
|
|
251 |
|
|
async boot() { |
252 |
|
|
if (!this.extensionsPath || !existsSync(this.extensionsPath)) { |
253 |
|
|
logDebug("No extensions found"); |
254 |
|
|
await this.initializeConfigService(); |
255 |
|
|
return; |
256 |
|
|
} |
257 |
|
|
|
258 |
|
|
const extensionsIndex = path.join(this.extensionsPath, "index.json"); |
259 |
|
|
|
260 |
|
|
if (existsSync(extensionsIndex)) { |
261 |
|
|
await this.loadExtensionsFromIndex(extensionsIndex); |
262 |
|
|
return; |
263 |
|
|
} |
264 |
|
|
|
265 |
|
|
await this.loadExtensions(); |
266 |
|
|
} |
267 |
|
|
|
268 |
|
|
async onInitializationComplete(extensions: Extension[]) { |
269 |
|
|
await this.client.configManager.registerExtensionConfig(extensions); |
270 |
|
|
return this.initializeConfigService(); |
271 |
|
|
} |
272 |
|
|
|
273 |
|
|
initializeConfigService() { |
274 |
|
|
return this.client.configManager.manualBoot(); |
275 |
|
|
} |
276 |
|
|
|
277 |
|
|
async loadExtensionsFromIndex(extensionsIndex: string) { |
278 |
|
|
const { extensions } = JSON.parse(await fs.readFile(extensionsIndex, "utf-8")); |
279 |
|
|
const loadInfoList = []; |
280 |
|
|
const extensionInitializers = []; |
281 |
|
|
|
282 |
|
|
for (const { entry, commands, events, name, services, id } of extensions) { |
283 |
|
|
logInfo("Loading extension initializer (cached): ", name); |
284 |
|
|
const loadInfo = { |
285 |
|
|
extensionPath: entry, |
286 |
|
|
commands, |
287 |
|
|
events, |
288 |
|
|
extensionName: name, |
289 |
|
|
services, |
290 |
|
|
extensionId: id, |
291 |
|
|
extension: null as unknown as Extension |
292 |
|
|
}; |
293 |
|
|
|
294 |
|
|
loadInfoList.push(loadInfo); |
295 |
|
|
loadInfo.extension = await this.loadExtensionInitializer(loadInfo); |
296 |
|
|
extensionInitializers.push(loadInfo.extension); |
297 |
|
|
} |
298 |
|
|
|
299 |
|
|
await this.onInitializationComplete(extensionInitializers); |
300 |
|
|
|
301 |
|
|
for (const loadInfo of loadInfoList) { |
302 |
|
|
await this.loadExtension(loadInfo); |
303 |
|
|
} |
304 |
|
|
} |
305 |
|
|
|
306 |
|
|
async loadExtensions() { |
307 |
|
|
if (!this.extensionsPath) { |
308 |
|
|
return; |
309 |
|
|
} |
310 |
|
|
|
311 |
|
|
const extensions = await fs.readdir(this.extensionsPath); |
312 |
|
|
const loadInfoList = []; |
313 |
|
|
const extensionInitializers = []; |
314 |
|
|
|
315 |
|
|
for (const extensionName of extensions) { |
316 |
|
|
const extensionDirectory = path.resolve(this.extensionsPath, extensionName); |
317 |
|
|
const isDirectory = (await fs.lstat(extensionDirectory)).isDirectory(); |
318 |
|
|
|
319 |
|
|
if (!isDirectory || extensionName === ".extbuilds") { |
320 |
|
|
continue; |
321 |
|
|
} |
322 |
|
|
|
323 |
|
|
logInfo("Loading extension: ", extensionName); |
324 |
|
|
const metadataFile = path.join(extensionDirectory, "extension.json"); |
325 |
|
|
|
326 |
|
|
if (!existsSync(metadataFile)) { |
327 |
|
|
logError(`Extension ${extensionName} does not have a "extension.json" file!`); |
328 |
|
|
process.exit(-1); |
329 |
|
|
} |
330 |
|
|
|
331 |
|
|
const parseResult = extensionMetadataSchema.safeParse( |
332 |
|
|
JSON.parse(await fs.readFile(metadataFile, { encoding: "utf-8" })) |
333 |
|
|
); |
334 |
|
|
|
335 |
|
|
if (!parseResult.success) { |
336 |
|
|
logError(`Error parsing extension metadata for extension ${extensionName}`); |
337 |
|
|
logError(parseResult.error); |
338 |
|
|
continue; |
339 |
|
|
} |
340 |
|
|
|
341 |
|
|
const { |
342 |
|
|
main_directory = "./build", |
343 |
|
|
commands = `./${main_directory}/commands`, |
344 |
|
|
events = `./${main_directory}/events`, |
345 |
|
|
services = `./${main_directory}/services`, |
346 |
|
|
main = `./${main_directory}/index.js`, |
347 |
|
|
id |
348 |
|
|
} = parseResult.data; |
349 |
|
|
|
350 |
|
|
const loadInfo = { |
351 |
|
|
extensionName, |
352 |
|
|
extensionId: id, |
353 |
|
|
extensionPath: path.join(extensionDirectory, main), |
354 |
|
|
commandsDirectory: path.join(extensionDirectory, commands), |
355 |
|
|
eventsDirectory: path.join(extensionDirectory, events), |
356 |
|
|
servicesDirectory: path.join(extensionDirectory, services), |
357 |
|
|
extension: null as unknown as Extension |
358 |
|
|
}; |
359 |
|
|
|
360 |
|
|
loadInfo.extension = await this.loadExtensionInitializer(loadInfo); |
361 |
|
|
loadInfoList.push(loadInfo); |
362 |
|
|
extensionInitializers.push(loadInfo.extension); |
363 |
|
|
} |
364 |
|
|
|
365 |
|
|
await this.onInitializationComplete(extensionInitializers); |
366 |
|
|
|
367 |
|
|
for (const loadInfo of loadInfoList) { |
368 |
|
|
await this.loadExtension(loadInfo); |
369 |
|
|
} |
370 |
|
|
} |
371 |
|
|
|
372 |
|
|
async loadExtensionInitializer({ |
373 |
|
|
extensionName, |
374 |
|
|
extensionId, |
375 |
|
|
extensionPath |
376 |
|
|
}: { |
377 |
|
|
extensionPath: string; |
378 |
|
|
extensionName: string; |
379 |
|
|
extensionId: string; |
380 |
|
|
}) { |
381 |
|
|
logDebug("Attempting to load extension initializer: ", extensionName, extensionId); |
382 |
|
|
const { default: ExtensionClass }: { default: new (client: Client) => Extension } = await import(extensionPath); |
383 |
|
|
return new ExtensionClass(this.client); |
384 |
|
|
} |
385 |
|
|
|
386 |
|
|
async loadExtension({ |
387 |
|
|
commandsDirectory, |
388 |
|
|
eventsDirectory, |
389 |
|
|
commands, |
390 |
|
|
events, |
391 |
|
|
extensionName, |
392 |
|
|
services, |
393 |
|
|
servicesDirectory, |
394 |
|
|
extensionId, |
395 |
|
|
extension |
396 |
|
|
}: LoadInfo) { |
397 |
|
|
logDebug("Attempting to load extension: ", extensionName, extensionId); |
398 |
|
|
|
399 |
|
|
const commandPaths = await extension.commands(); |
400 |
|
|
const eventPaths = await extension.events(); |
401 |
|
|
const servicePaths = await extension.services(); |
402 |
|
|
|
403 |
|
|
if (servicePaths === null) { |
404 |
|
|
if (servicesDirectory) { |
405 |
|
|
if (existsSync(servicesDirectory)) { |
406 |
|
|
await this.client.serviceManager.loadServiceFromDirectory(servicesDirectory); |
407 |
|
|
} |
408 |
|
|
} else if (services) { |
409 |
|
|
for (const servicePath of services) { |
410 |
|
|
await this.client.serviceManager.loadService(servicePath); |
411 |
|
|
} |
412 |
|
|
} |
413 |
|
|
} else { |
414 |
|
|
for (const servicePath of servicePaths) { |
415 |
|
|
await this.client.serviceManager.loadService(servicePath); |
416 |
|
|
} |
417 |
|
|
} |
418 |
|
|
|
419 |
|
|
if (commandPaths === null) { |
420 |
|
|
if (commandsDirectory) { |
421 |
|
|
if (existsSync(commandsDirectory)) { |
422 |
|
|
await this.client.dynamicLoader.loadCommands(commandsDirectory); |
423 |
|
|
} |
424 |
|
|
} else if (commands) { |
425 |
|
|
for (const commandPath of commands) { |
426 |
|
|
await this.client.dynamicLoader.loadCommand(commandPath); |
427 |
|
|
} |
428 |
|
|
} |
429 |
|
|
} else { |
430 |
|
|
for (const commandPath of commandPaths) { |
431 |
|
|
await this.client.dynamicLoader.loadCommand(commandPath); |
432 |
|
|
} |
433 |
|
|
} |
434 |
|
|
|
435 |
|
|
if (eventPaths === null) { |
436 |
|
|
if (eventsDirectory) { |
437 |
|
|
if (existsSync(eventsDirectory)) { |
438 |
|
|
await this.loadEvents(extensionName, eventsDirectory); |
439 |
|
|
} |
440 |
|
|
} else if (events) { |
441 |
|
|
for (const eventPath of events) { |
442 |
|
|
await this.loadEvent(extensionName, eventPath); |
443 |
|
|
} |
444 |
|
|
} |
445 |
|
|
} else { |
446 |
|
|
for (const eventPath of eventPaths) { |
447 |
|
|
await this.loadEvent(extensionName, eventPath); |
448 |
|
|
} |
449 |
|
|
} |
450 |
|
|
} |
451 |
|
|
|
452 |
|
|
async loadEvents(extensionName: string, directory: string) { |
453 |
|
|
const files = await fs.readdir(directory); |
454 |
|
|
|
455 |
|
|
for (const file of files) { |
456 |
|
|
const filePath = path.join(directory, file); |
457 |
|
|
const isDirectory = (await fs.lstat(filePath)).isDirectory(); |
458 |
|
|
|
459 |
|
|
if (isDirectory) { |
460 |
|
|
await this.loadEvents(extensionName, filePath); |
461 |
|
|
continue; |
462 |
|
|
} |
463 |
|
|
|
464 |
|
|
if ((!file.endsWith(".ts") && !file.endsWith(".js")) || file.endsWith(".d.ts")) { |
465 |
|
|
continue; |
466 |
|
|
} |
467 |
|
|
|
468 |
|
|
await this.loadEvent(extensionName, filePath); |
469 |
|
|
} |
470 |
|
|
} |
471 |
|
|
|
472 |
|
|
async loadEvent(extensionName: string, filePath: string) { |
473 |
|
|
const { default: Event }: { default: new (client: Client) => EventListener<keyof ClientEvents> } = await import(filePath); |
474 |
|
|
const event = new Event(this.client); |
475 |
|
|
this.client.addEventListener(event.name, this.wrapHandler(extensionName, event.name, event.execute.bind(event))); |
476 |
|
|
} |
477 |
|
|
|
478 |
|
|
wrapHandler<K extends keyof ClientEvents>( |
479 |
|
|
extensionName: string, |
480 |
|
|
eventName: K, |
481 |
|
|
handler: (...args: ClientEvents[K]) => unknown, |
482 |
|
|
bail?: boolean |
483 |
|
|
) { |
484 |
|
|
return async (...args: ClientEvents[K]) => { |
485 |
|
|
const guildId: Snowflake | null | undefined = this.guildIdResolvers.get(eventName)?.(args); |
486 |
|
|
|
487 |
|
|
if (guildId === undefined) { |
488 |
|
|
logError("Invalid event or failed to fetch guild: ", eventName); |
489 |
|
|
return; |
490 |
|
|
} |
491 |
|
|
|
492 |
|
|
if (guildId !== null && !this.isEnabled(extensionName, guildId)) { |
493 |
|
|
log("Extension isn't enabled in this guild: ", guildId); |
494 |
|
|
return; |
495 |
|
|
} |
496 |
|
|
|
497 |
|
|
logInfo("Running: " + eventName + " [" + extensionName + "]"); |
498 |
|
|
|
499 |
|
|
try { |
500 |
|
|
return await handler(...args); |
501 |
|
|
} catch (e) { |
502 |
|
|
logError(`Extension error: the extension '${extensionName}' seems to cause this exception`); |
503 |
|
|
logError(e); |
504 |
|
|
|
505 |
|
|
if (bail) { |
506 |
|
|
return; |
507 |
|
|
} |
508 |
|
|
} |
509 |
|
|
}; |
510 |
|
|
} |
511 |
|
|
|
512 |
|
|
isEnabled(extensionName: string, guildId: Snowflake) { |
513 |
|
|
const { disabled_extensions, enabled } = this.client.configManager.config[guildId]?.extensions ?? {}; |
514 |
|
|
const { default_mode } = this.client.configManager.systemConfig.extensions ?? {}; |
515 |
|
|
log(default_mode, enabled); |
516 |
|
|
return (enabled === undefined ? default_mode === "enable_all" : enabled) && !disabled_extensions?.includes(extensionName); |
517 |
|
|
} |
518 |
|
|
|
519 |
|
|
private async fetchExtensionMetadata() { |
520 |
|
|
this.client.logger.debug("Fetching extension list metadata"); |
521 |
|
|
|
522 |
|
|
const [response, error] = await request({ |
523 |
|
|
method: "GET", |
524 |
|
|
url: this.extensionIndexURL |
525 |
|
|
}); |
526 |
|
|
|
527 |
|
|
if (error || !response || response.status !== 200) { |
528 |
|
|
return [null, error] as const; |
529 |
|
|
} |
530 |
|
|
|
531 |
|
|
return [response.data as Record<string, ExtensionInfo | undefined>, null] as const; |
532 |
|
|
} |
533 |
|
|
|
534 |
|
|
public async getExtensionMetadata(id: string) { |
535 |
|
|
const [data, error] = await cache("extension-index", () => this.fetchExtensionMetadata(), { |
536 |
|
|
ttl: 120_000, |
537 |
|
|
invoke: true |
538 |
|
|
}); |
539 |
|
|
|
540 |
|
|
return error ? ([null, error] as const) : ([data?.[id], null] as const); |
541 |
|
|
} |
542 |
|
|
|
543 |
|
|
private writeStream(stream: Response | undefined | null, data: string) { |
544 |
|
|
if (!stream) { |
545 |
|
|
return; |
546 |
|
|
} |
547 |
|
|
|
548 |
|
|
return new Promise<void>(resolve => { |
549 |
|
|
if (!stream.write(data)) { |
550 |
|
|
stream.once("drain", resolve); |
551 |
|
|
} else { |
552 |
|
|
process.nextTick(resolve); |
553 |
|
|
} |
554 |
|
|
}); |
555 |
|
|
} |
556 |
|
|
|
557 |
|
|
async fetchAndInstallExtension(id: string, stream?: Response) { |
558 |
|
|
if (!this.extensionsPath) { |
559 |
|
|
const errorMessage = |
560 |
|
|
"E: Extensions directory is not set. Please set the EXTENSIONS_DIRECTORY environment variable.\n"; |
561 |
|
|
await this.writeStream(stream, errorMessage); |
562 |
|
|
return [null, errorMessage]; |
563 |
|
|
} |
564 |
|
|
|
565 |
|
|
await this.writeStream(stream, `Fetching metadata for extension ${id}...\n`); |
566 |
|
|
const [extension, metadataError] = await this.getExtensionMetadata(id); |
567 |
|
|
|
568 |
|
|
if (metadataError || !extension) { |
569 |
|
|
await this.writeStream(stream, `E: Failed to fetch metadata of extension: ${id}\n`); |
570 |
|
|
return [null, metadataError] as const; |
571 |
|
|
} |
572 |
|
|
|
573 |
|
|
await this.writeStream( |
574 |
|
|
stream, |
575 |
|
|
`Retrieving ${extension.name} (${extension.version}) from the SudoBot Extension Repository (SER)...\n` |
576 |
|
|
); |
577 |
|
|
await wait(100); |
578 |
|
|
await this.writeStream(stream, this.downloadProgressStreamEOF); |
579 |
|
|
await wait(100); |
580 |
|
|
await this.writeStream(stream, "\n"); |
581 |
|
|
|
582 |
|
|
try { |
583 |
|
|
const { filePath } = await downloadFile({ |
584 |
|
|
url: extension.tarballs[0].url, |
585 |
|
|
path: sudoPrefix("tmp", true), |
586 |
|
|
name: `${extension.id}-${extension.version}.tar.gz`, |
587 |
|
|
axiosOptions: { |
588 |
|
|
method: "GET", |
589 |
|
|
responseType: "stream", |
590 |
|
|
onDownloadProgress: async progressEvent => { |
591 |
|
|
if (!progressEvent.total) { |
592 |
|
|
return; |
593 |
|
|
} |
594 |
|
|
|
595 |
|
|
const percentCompleted = Math.floor((progressEvent.loaded / progressEvent.total) * 100); |
596 |
|
|
await this.writeStream(stream, `${percentCompleted}\n`); |
597 |
|
|
} |
598 |
|
|
} |
599 |
|
|
}); |
600 |
|
|
|
601 |
|
|
await wait(100); |
602 |
|
|
await this.writeStream(stream, this.downloadProgressStreamEOF); |
603 |
|
|
await wait(100); |
604 |
|
|
await this.writeStream(stream, "\n"); |
605 |
|
|
|
606 |
|
|
await this.installExtension(filePath, extension, stream); |
607 |
|
|
|
608 |
|
|
try { |
609 |
|
|
await rm(filePath, { force: true }); |
610 |
|
|
} catch (error) { |
611 |
|
|
this.client.logger.error(error); |
612 |
|
|
await this.writeStream(stream, `W: Failed to clean download caches for extension: ${id}\n`); |
613 |
|
|
} |
614 |
|
|
} catch (error) { |
615 |
|
|
this.client.logger.error(error); |
616 |
|
|
|
617 |
|
|
await this.writeStream(stream, this.downloadProgressStreamEOF); |
618 |
|
|
await this.writeStream(stream, "\n"); |
619 |
|
|
await this.writeStream(stream, `E: Failed to retrieve extension: ${id}\n`); |
620 |
|
|
} |
621 |
|
|
} |
622 |
|
|
|
623 |
|
|
private async installExtension(filePath: string, extension: ExtensionInfo, stream?: Response) { |
624 |
|
|
if (!this.extensionsPath) { |
625 |
|
|
return; |
626 |
|
|
} |
627 |
|
|
|
628 |
|
|
await this.writeStream(stream, `Preparing to unpack ${extension.name} (${extension.version})...\n`); |
629 |
|
|
const extensionTmpDirectory = sudoPrefix(`tmp/${extension.id}-${extension.version}`, true); |
630 |
|
|
await this.writeStream(stream, `Unpacking ${extension.name} (${extension.version})...\n`); |
631 |
|
|
|
632 |
|
|
try { |
633 |
|
|
await tar.x({ |
634 |
|
|
file: filePath, |
635 |
|
|
cwd: extensionTmpDirectory |
636 |
|
|
}); |
637 |
|
|
} catch (error) { |
638 |
|
|
this.client.logger.error(error); |
639 |
|
|
await this.writeStream(stream, `E: Unable to unpack extension: ${extension.id}\n`); |
640 |
|
|
return; |
641 |
|
|
} |
642 |
|
|
|
643 |
|
|
await this.writeStream(stream, `Setting up ${extension.name} (${extension.version})...\n`); |
644 |
|
|
const extensionDirectory = path.join(this.extensionsPath, extension.shortName); |
645 |
|
|
|
646 |
|
|
try { |
647 |
|
|
await fs.rename(path.join(extensionTmpDirectory, `${extension.shortName}-${extension.version}`), extensionDirectory); |
648 |
|
|
} catch (error) { |
649 |
|
|
this.client.logger.error(error); |
650 |
|
|
await this.writeStream(stream, `E: Failed to set up extension: ${extension.id}\n`); |
651 |
|
|
return; |
652 |
|
|
} |
653 |
|
|
|
654 |
|
|
await this.writeStream(stream, "Cleaning up caches and temporary files...\n"); |
655 |
|
|
|
656 |
|
|
try { |
657 |
|
|
await rm(extensionTmpDirectory, { force: true, recursive: true }); |
658 |
|
|
} catch (error) { |
659 |
|
|
this.client.logger.error(error); |
660 |
|
|
await this.writeStream(stream, `W: Failed to clean up temporary files for extension: ${extension.id}\n`); |
661 |
|
|
} |
662 |
|
|
|
663 |
|
|
// TODO: Load extension automatically |
664 |
|
|
} |
665 |
|
|
} |
666 |
|
|
|
667 |
|
|
type LoadInfo = ( |
668 |
|
|
| { |
669 |
|
|
extensionPath: string; |
670 |
|
|
commandsDirectory: string; |
671 |
|
|
eventsDirectory: string; |
672 |
|
|
servicesDirectory: string; |
673 |
|
|
extensionName: string; |
674 |
|
|
extensionId: string; |
675 |
|
|
commands?: never; |
676 |
|
|
events?: never; |
677 |
|
|
services?: never; |
678 |
|
|
} |
679 |
|
|
| { |
680 |
|
|
extensionPath: string; |
681 |
|
|
commandsDirectory?: never; |
682 |
|
|
eventsDirectory?: never; |
683 |
|
|
servicesDirectory?: never; |
684 |
|
|
commands: string[]; |
685 |
|
|
events: string[]; |
686 |
|
|
services: string[]; |
687 |
|
|
extensionName: string; |
688 |
|
|
extensionId: string; |
689 |
|
|
} |
690 |
|
|
) & { |
691 |
|
|
extension: Extension; |
692 |
|
|
}; |