/[sudobot]/branches/8.x/src/core/Client.ts
ViewVC logotype

Contents of /branches/8.x/src/core/Client.ts

Parent Directory Parent Directory | Revision Log Revision Log


Revision 577 - (show annotations)
Mon Jul 29 18:52:37 2024 UTC (8 months ago) by rakinar2
File MIME type: application/typescript
File size: 10306 byte(s)
chore: add old version archive branches (2.x to 9.x-dev)
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 { PrismaClient } from "@prisma/client";
21 import {
22 ClientOptions,
23 Collection,
24 ClientEvents as DiscordClientEvents,
25 Client as DiscordJSClient,
26 GuildEmoji
27 } from "discord.js";
28 import path from "node:path";
29 import Server from "../api/Server";
30 import type AIAutoModService from "../automod/AIAutoModService";
31 import type Antijoin from "../automod/Antijoin";
32 import type Antiraid from "../automod/Antiraid";
33 import type Antispam from "../automod/Antispam";
34 import type FileFilterService from "../automod/FileFilterService";
35 import type MessageFilter from "../automod/MessageFilter";
36 import type MessageRuleService from "../automod/MessageRuleService";
37 import type ProfileFilter from "../automod/ProfileFilter";
38 import type VerificationService from "../automod/VerificationService";
39 import type AFKService from "../services/AFKService";
40 import type AutoRoleService from "../services/AutoRoleService";
41 import type BallotManager from "../services/BallotManager";
42 import type BumpReminderService from "../services/BumpReminderService";
43 import type ChannelLockManager from "../services/ChannelLockManager";
44 import type CommandManager from "../services/CommandManager";
45 import type CommandPermissionOverwriteManager from "../services/CommandPermissionOverwriteManager";
46 import type ConfigManager from "../services/ConfigManager";
47 import type CooldownService from "../services/CooldownService";
48 import type ExtensionService from "../services/ExtensionService";
49 import type ImageRecognitionService from "../services/ImageRecognitionService";
50 import type InfractionManager from "../services/InfractionManager";
51 import type InviteTrackerService from "../services/InviteTrackerService";
52 import type KeypressHandlerService from "../services/KeypressHandlerService";
53 import type LogServer from "../services/LogServer";
54 import type LoggerService from "../services/LoggerService";
55 import type MetadataService from "../services/MetadataService";
56 import type PermissionManager from "../services/PermissionManager";
57 import type QueueManager from "../services/QueueManager";
58 import type QuickMuteService from "../services/QuickMuteService";
59 import type ReactionRoleService from "../services/ReactionRoleService";
60 import type ReportService from "../services/ReportService";
61 import type SnippetManager from "../services/SnippetManager";
62 import type StartupManager from "../services/StartupManager";
63 import type StatsService from "../services/StatsService";
64 import type TranslationService from "../services/TranslationService";
65 import type TriggerService from "../services/TriggerService";
66 import type WelcomerService from "../services/WelcomerService";
67 import { ClientEvents } from "../types/ClientEvents";
68 import { Logger } from "../utils/Logger";
69 import { developmentMode } from "../utils/utils";
70 import type Command from "./Command";
71 import DynamicLoader from "./DynamicLoader";
72 import Service from "./Service";
73 import ServiceManager from "./ServiceManager";
74 import type SurveyService from "../services/SurveyService";
75
76 class Client<R extends boolean = boolean> extends DiscordJSClient<R> {
77 public static instance: Client;
78 private readonly eventListeners = new Map<string, ((...args: unknown[]) => unknown)[]>();
79 public readonly commands = new Collection<string, Command>();
80 public readonly emojiMap = new Map<string, GuildEmoji>();
81
82 public readonly prisma = new PrismaClient({
83 errorFormat: "pretty",
84 log: developmentMode() ? ["error", "info", "query", "warn"] : ["error", "info", "warn"]
85 });
86
87 public readonly server = new Server(this);
88 public readonly dynamicLoader = new DynamicLoader(this);
89 public readonly serviceManager = new ServiceManager(this);
90
91 public readonly aliases = {
92 automod: path.resolve(__dirname, "../automod"),
93 services: path.resolve(__dirname, "../services")
94 };
95
96 static get _logger() {
97 return new Logger("system", !process.env.NO_DATETIME_LOGGING);
98 }
99
100 public readonly logger = Client._logger;
101
102 public readonly services = [
103 "@services/StartupManager",
104 "@services/ConfigManager" /* This service is manually booted by the Extension Service. */,
105 "@services/ExtensionService",
106 "@services/CommandManager",
107 "@services/InfractionManager",
108 "@services/LoggerService",
109 "@services/QueueManager",
110 "@services/WelcomerService",
111 "@services/SnippetManager",
112 "@services/ChannelLockManager",
113 "@services/PermissionManager",
114 "@services/MetadataService",
115 "@services/QuickMuteService",
116 "@services/TranslationService",
117 "@services/AutoRoleService",
118 "@services/ReactionRoleService",
119 "@services/AFKService",
120 "@services/InviteTrackerService",
121 "@services/BallotManager",
122 "@services/TriggerService",
123 "@services/BumpReminderService",
124 "@services/LogServer",
125 "@services/CooldownService",
126 "@services/KeypressHandlerService",
127 "@services/CommandPermissionOverwriteManager",
128 "@services/ReportService",
129 "@services/StatsService",
130 "@services/ImageRecognitionService",
131 "@services/SurveyService",
132
133 "@automod/MessageFilter",
134 "@automod/Antispam",
135 "@automod/Antiraid",
136 "@automod/Antijoin",
137 "@automod/ProfileFilter",
138 "@automod/FileFilterService",
139 "@automod/MessageRuleService",
140 "@automod/AIAutoModService",
141 "@automod/VerificationService"
142 ];
143
144 startupManager!: StartupManager;
145 configManager!: ConfigManager;
146 commandManager!: CommandManager;
147 infractionManager!: InfractionManager;
148 loggerService!: LoggerService;
149 messageFilter!: MessageFilter;
150 antispam!: Antispam;
151 queueManager!: QueueManager;
152 snippetManager!: SnippetManager;
153 welcomerService!: WelcomerService;
154 antiraid!: Antiraid;
155 channelLockManager!: ChannelLockManager;
156 antijoin!: Antijoin;
157 profileFilter!: ProfileFilter;
158 permissionManager!: PermissionManager;
159 metadata!: MetadataService;
160 quickMute!: QuickMuteService;
161 translator!: TranslationService;
162 autoRoleService!: AutoRoleService;
163 reactionRoleService!: ReactionRoleService;
164 afkService!: AFKService;
165 inviteTracker!: InviteTrackerService;
166 ballotManager!: BallotManager;
167 fileFilter!: FileFilterService;
168 messageRuleService!: MessageRuleService;
169 triggerService!: TriggerService;
170 aiAutoMod!: AIAutoModService;
171 extensionService!: ExtensionService;
172 bumpReminder!: BumpReminderService;
173 logServer!: LogServer;
174 cooldown!: CooldownService;
175 keypressHandler!: KeypressHandlerService;
176 verification!: VerificationService;
177 reportService!: ReportService;
178 commandPermissionOverwriteManager!: CommandPermissionOverwriteManager;
179 statsService!: StatsService;
180 imageRecognitionService!: ImageRecognitionService;
181 surveyService!: SurveyService;
182
183 constructor(options: ClientOptions) {
184 super(options);
185 Client.instance = this;
186 }
187
188 public static getLogger() {
189 return this._logger;
190 }
191
192 getService<S extends Service = Service>(name: string): S {
193 return this[name as keyof typeof this] as S;
194 }
195
196 async boot({ commands = true, events = true }: { commands?: boolean; events?: boolean } = {}) {
197 await this.serviceManager.loadServices();
198
199 if (events) {
200 this.setMaxListeners(50);
201 await this.dynamicLoader.loadEvents();
202 }
203
204 if (commands) {
205 await this.dynamicLoader.loadCommands();
206 }
207 }
208
209 async getHomeGuild() {
210 return (
211 this.guilds.cache.get(process.env.HOME_GUILD_ID) ??
212 (await this.guilds.fetch(process.env.HOME_GUILD_ID))
213 );
214 }
215
216 addEventListener<K extends keyof ClientEvents>(
217 name: K,
218 listener: (...args: ClientEvents[K]) => unknown
219 ) {
220 type Listener = (...args: DiscordClientEvents[keyof DiscordClientEvents]) => void;
221 const handlers = this.eventListeners.get(name) ?? [];
222
223 if (!this.eventListeners.has(name)) {
224 this.eventListeners.set(name, handlers);
225 }
226
227 handlers.push(listener as (...args: unknown[]) => unknown);
228 this.on(name as keyof DiscordClientEvents, listener as Listener);
229 }
230
231 removeEventListener<K extends keyof ClientEvents>(
232 name: K,
233 listener?: (...args: ClientEvents[K]) => unknown
234 ) {
235 if (!listener) {
236 this.eventListeners.delete(name);
237 this.removeAllListeners(name as keyof DiscordClientEvents);
238 return;
239 }
240
241 const handlers = this.eventListeners.get(name) ?? [];
242 const index = handlers.findIndex(handler => handler === listener);
243
244 if (index === -1) {
245 return;
246 }
247
248 const handler = handlers.splice(index, 1)[0];
249 this.off(
250 name as keyof DiscordClientEvents,
251 handler as (...args: DiscordClientEvents[keyof DiscordClientEvents]) => void
252 );
253 }
254
255 async emitWaitLocal<K extends keyof ClientEvents>(name: K, ...args: ClientEvents[K]) {
256 for (const listener of this.eventListeners.get(name) ?? []) {
257 await listener(...(args as unknown[]));
258 }
259 }
260
261 emitWait<K extends keyof ClientEvents>(name: K, ...args: ClientEvents[K]) {
262 for (const listener of this.eventListeners.get(name) ?? []) {
263 listener(...(args as unknown[]));
264 }
265 }
266 }
267
268 export default Client;

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26