1 |
/* |
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 { Awaitable } from "discord.js"; |
21 |
import { Router } from "express"; |
22 |
import { lstat, readdir } from "node:fs/promises"; |
23 |
import path, { basename, dirname } from "node:path"; |
24 |
import Controller from "../api/Controller"; |
25 |
import { EventListenerInfo } from "../decorators/GatewayEventListener"; |
26 |
import { ClientEvents } from "../types/ClientEvents"; |
27 |
import { AnyFunction, Class, DefaultExport } from "../types/Utils"; |
28 |
import { log, logInfo } from "../utils/Logger"; |
29 |
import type Client from "./Client"; |
30 |
import Command from "./Command"; |
31 |
import EventListener from "./EventListener"; |
32 |
import Service from "./Service"; |
33 |
|
34 |
class DynamicLoader extends Service { |
35 |
protected readonly eventHandlers = new WeakMap<object, Record<keyof ClientEvents, AnyFunction[]>>(); |
36 |
|
37 |
private async iterateDirectoryRecursively(root: string, rootArray?: string[]) { |
38 |
const filesAndDirectories = await readdir(root); |
39 |
const files: string[] = []; |
40 |
|
41 |
for (const file of filesAndDirectories) { |
42 |
const filepath = path.resolve(root, file); |
43 |
const stat = await lstat(filepath); |
44 |
|
45 |
if (stat.isDirectory()) { |
46 |
await this.iterateDirectoryRecursively(filepath, rootArray ?? files); |
47 |
continue; |
48 |
} |
49 |
|
50 |
(rootArray ?? files).push(filepath); |
51 |
} |
52 |
|
53 |
return files; |
54 |
} |
55 |
|
56 |
async loadControllers(router: Router) { |
57 |
const eventListenerFiles = await this.iterateDirectoryRecursively(path.resolve(__dirname, "../api/controllers")); |
58 |
|
59 |
for (const file of eventListenerFiles) { |
60 |
if ((!file.endsWith(".ts") && !file.endsWith(".js")) || file.endsWith(".d.ts")) { |
61 |
continue; |
62 |
} |
63 |
|
64 |
await this.loadController(file, router); |
65 |
} |
66 |
} |
67 |
|
68 |
async loadController(filepath: string, router: Router) { |
69 |
const { default: ControllerClass }: DefaultExport<Class<Controller, [Client]>> = await import(filepath); |
70 |
const controller = new ControllerClass(this.client); |
71 |
this.client.server.loadController(controller, ControllerClass, router); |
72 |
logInfo("Loaded Controller: ", ControllerClass.name); |
73 |
} |
74 |
|
75 |
async loadEvents() { |
76 |
const eventListenerFiles = await this.iterateDirectoryRecursively(path.resolve(__dirname, "../events")); |
77 |
|
78 |
for (const file of eventListenerFiles) { |
79 |
if ((!file.endsWith(".ts") && !file.endsWith(".js")) || file.endsWith(".d.ts")) { |
80 |
continue; |
81 |
} |
82 |
|
83 |
await this.loadEvent(file); |
84 |
} |
85 |
} |
86 |
|
87 |
async loadEvent(filepath: string) { |
88 |
const { default: EventListenerClass }: DefaultExport<Class<EventListener, [Client]>> = await import(filepath); |
89 |
const listener = new EventListenerClass(this.client); |
90 |
this.client.addEventListener(listener.name, listener.execute.bind(listener)); |
91 |
logInfo("Loaded Event: ", listener.name); |
92 |
} |
93 |
|
94 |
async loadServiceFromDirectory(servicesDirectory = path.resolve(__dirname, "../services")) { |
95 |
const commandFiles = await this.iterateDirectoryRecursively(servicesDirectory); |
96 |
|
97 |
for (const file of commandFiles) { |
98 |
if ((!file.endsWith(".ts") && !file.endsWith(".js")) || file.endsWith(".d.ts")) { |
99 |
continue; |
100 |
} |
101 |
|
102 |
await this.client.serviceManager.loadService(file); |
103 |
} |
104 |
} |
105 |
|
106 |
flattenCommandGroups() { |
107 |
const groups = this.client.configManager.systemConfig.commands.groups; |
108 |
const groupNames = Object.keys(groups); |
109 |
|
110 |
if (groupNames.length === 0) { |
111 |
return null; |
112 |
} |
113 |
|
114 |
const flatten: Record<string, string> = {}; |
115 |
|
116 |
for (const groupName of groupNames) { |
117 |
for (const commandName of groups[groupName]) { |
118 |
flatten[commandName] = groupName; |
119 |
} |
120 |
} |
121 |
|
122 |
return flatten; |
123 |
} |
124 |
|
125 |
async loadCommands( |
126 |
commandsDirectory = path.resolve(__dirname, "../commands"), |
127 |
loadMetadata: boolean = true, |
128 |
filter?: (path: string, name: string) => Awaitable<boolean> |
129 |
) { |
130 |
const commandFiles = await this.iterateDirectoryRecursively(commandsDirectory); |
131 |
const groups = this.flattenCommandGroups(); |
132 |
|
133 |
for (const file of commandFiles) { |
134 |
if ((!file.endsWith(".ts") && !file.endsWith(".js")) || file.endsWith(".d.ts")) { |
135 |
continue; |
136 |
} |
137 |
|
138 |
if (filter && !(await filter(file, path.basename(file)))) { |
139 |
continue; |
140 |
} |
141 |
|
142 |
await this.loadCommand(file, loadMetadata, groups); |
143 |
} |
144 |
} |
145 |
|
146 |
async loadCommand(filepath: string, loadMetadata = true, groups: Record<string, string> | null = null) { |
147 |
const { default: CommandClass }: DefaultExport<Class<Command, [Client]>> = await import(filepath); |
148 |
const command = new CommandClass(this.client); |
149 |
const previousCommand = this.client.commands.get(command.name); |
150 |
let aliasGroupSet = false; |
151 |
|
152 |
if (loadMetadata && previousCommand) { |
153 |
await this.unloadEventsFromMetadata(previousCommand); |
154 |
} |
155 |
|
156 |
this.client.commands.set(command.name, command); |
157 |
|
158 |
for (const alias of command.aliases) { |
159 |
this.client.commands.set(alias, command); |
160 |
|
161 |
if (groups?.[alias] && !aliasGroupSet) { |
162 |
command.group = groups?.[alias]; |
163 |
aliasGroupSet = true; |
164 |
} |
165 |
} |
166 |
|
167 |
if (!aliasGroupSet || groups?.[command.name]) { |
168 |
command.group = groups?.[command.name] ?? basename(dirname(filepath)); |
169 |
} |
170 |
|
171 |
if (loadMetadata) { |
172 |
await this.loadEventsFromMetadata(command); |
173 |
} |
174 |
|
175 |
logInfo("Loaded Command: ", command.name); |
176 |
} |
177 |
|
178 |
async loadEventsFromMetadata(object: object, accessConstructor = true) { |
179 |
const finalObject = accessConstructor ? object.constructor : object; |
180 |
const metadata = |
181 |
Symbol.metadata in finalObject |
182 |
? (finalObject[Symbol.metadata] as { eventListeners?: EventListenerInfo[] }) |
183 |
: { |
184 |
eventListeners: Reflect.getMetadata("event_listeners", (finalObject as { prototype: object }).prototype) |
185 |
}; |
186 |
|
187 |
const handlerData = this.eventHandlers.get(object) ?? ({} as Record<keyof ClientEvents, AnyFunction[]>); |
188 |
|
189 |
for (const listenerInfo of metadata.eventListeners ?? []) { |
190 |
const callback = object[listenerInfo.methodName as unknown as keyof typeof object] as AnyFunction; |
191 |
const handler = callback.bind(object); |
192 |
handlerData[listenerInfo.event as keyof typeof handlerData] ??= [] as AnyFunction[]; |
193 |
handlerData[listenerInfo.event as keyof typeof handlerData].push(handler); |
194 |
|
195 |
this.client.addEventListener(listenerInfo.event as keyof ClientEvents, handler); |
196 |
} |
197 |
|
198 |
this.eventHandlers.set(object, handlerData); |
199 |
|
200 |
if (metadata.eventListeners) { |
201 |
log(`Registered ${metadata.eventListeners?.length ?? 0} event listeners`); |
202 |
} |
203 |
} |
204 |
|
205 |
async unloadEventsFromMetadata(object: object) { |
206 |
const handlerData = this.eventHandlers.get(object) ?? ({} as Record<keyof ClientEvents, AnyFunction[]>); |
207 |
let count = 0; |
208 |
|
209 |
for (const event in handlerData) { |
210 |
for (const callback of handlerData[event as keyof typeof handlerData]) { |
211 |
this.client.removeEventListener( |
212 |
event as keyof ClientEvents, |
213 |
callback as (...args: ClientEvents[keyof ClientEvents]) => unknown |
214 |
); |
215 |
} |
216 |
|
217 |
count += handlerData[event as keyof typeof handlerData].length; |
218 |
} |
219 |
|
220 |
log(`Unloaded ${count} event listeners`); |
221 |
} |
222 |
} |
223 |
|
224 |
export default DynamicLoader; |