-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule.ts.dis
174 lines (149 loc) · 4.31 KB
/
module.ts.dis
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
import { importx } from "@discordx/importer";
import { Module } from "../../core/Module.js";
import { ModuleState, type ModuleMetadata } from "../../types/index.js";
import packageJson from "./package.json" with { type: "json" };
import translations from "./src/locales/en.json" with { type: "json" };
import { createLocale } from "../../utils/index.js";
import { config } from "dotenv";
import { dirname } from "dirname-filename-esm";
import { resolve } from "path";
import { CacheQueueService } from "#services/index.js";
import { bot } from "#bot.js";
// Load environment variables from .env file
config({ path: resolve(dirname(import.meta), ".env") });
/**
* Discord bot module that handles bot initialization and lifecycle
*/
export default class BotModule extends Module {
public databaseModule!: typeof import("../database/module.js").module;
public queueService!: CacheQueueService;
constructor() {
super();
this.logger.debug("Creating BotModule instance");
}
public readonly metadata: ModuleMetadata = {
name: packageJson.name,
version: packageJson.version,
description: packageJson.description,
dependencies: ["database"],
priority: 90,
};
public locale = createLocale<typeof translations>(
packageJson.name,
);
// Define module exports with a clear interface
public readonly exports = {
getBot: () => {
this.logger.debug("Accessing bot instance through exports");
return bot;
},
getClient: () => {
this.logger.debug("Accessing bot client through exports");
return bot!.client;
},
} as const;
/**
* Initialize the bot module
*/
protected async onInitialize(): Promise<void> {
try {
await this.locale.load();
await this.locale.setLanguage(process.env.BOT_LOCALE || "en");
bot.logger = this.logger!;
bot.locale = this.locale!;
this.connectToModule();
await bot.initialize();
} catch (error: unknown) {
this.logger.error("Failed to initialize bot module:", error);
this.handleError("initialization", error);
}
}
/**
* Start the bot module
*/
protected async onStart(): Promise<void> {
try {
await this.loadCommands();
await this.startBot();
} catch (error) {
this.logger.error("Failed to start bot module", error);
throw error;
}
}
/**
* Stop the bot module
*/
protected async onStop(): Promise<void> {
try {
await this.stopBot();
} catch (error) {
this.logger.error("Failed to stop bot module", error);
throw error;
}
}
/**
* Load bot commands and events
*/
private async loadCommands(): Promise<void> {
try {
if (process.env.LOG_LEVEL === "debug") {
this.logger.debug("Loading bot commands and events...");
}
// Use importx to load all commands and events
await importx(`${dirname(import.meta)}/src/{events,commands}/**/*.{ts,js}`);
if (process.env.LOG_LEVEL === "debug") {
this.logger.debug("Bot commands and events loaded successfully");
}
} catch (error) {
this.logger.error("Failed to load bot commands", error);
throw error;
}
}
/**
* Connect to module
*/
private connectToModule(): void {
if (!this.moduleManager) {
this.logger.warn("ModuleManager not available, cannot connect to module");
return;
}
this.databaseModule =
this.moduleManager.getModule<
typeof import("../database/module.js").module
>("database")!;
if (this.databaseModule?.exports?.getQueueService?.()) {
bot!.databaseModule = this.databaseModule
this.queueService = this.databaseModule.exports.getQueueService() as unknown as CacheQueueService;
this.logger.info({
message: "Модуль базы данных успешно подключен",
moduleState: ModuleState.INITIALIZED,
});
} else {
this.logger.warn(
"Could not connect to database module or it has no queue service",
);
this.queueService = new CacheQueueService();
}
}
/**
* Start the Discord bot
*/
private async startBot(): Promise<void> {
if (!process.env.DISCORD_TOKEN) {
throw new Error(this.locale.t("messages.bot.token.error"));
}
await bot.start(process.env.DISCORD_TOKEN);
}
/**
* Stop the Discord bot
*/
private async stopBot(): Promise<void> {
await bot.destroy();
this.logger.info({
message: this.locale.t("messages.bot.status.stopped"),
moduleState: ModuleState.STOPPED,
});
}
}
// Export a singleton instance
export const module = new BotModule();