-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.ts
83 lines (71 loc) · 2.03 KB
/
app.ts
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
import { CommandStorage } from "@/storage/commands";
import { Client } from "discord.js";
import { Logger } from "./lib/logger";
import { CommandHandler } from "./lib/handlers/commands";
import { EventHandler } from "./lib/handlers/events";
import { intents } from "./intents";
let bot = new Client({
intents: intents,
});
export class App {
private client = bot;
private commands = new CommandStorage();
private logger = new Logger();
constructor({ bot_token }: { bot_token: string }) {
try {
this.login(bot_token);
} catch (error) {
throw error;
} finally {
this.init();
}
};
private init() {
this.ready();
this.commandListener();
this.eventListener();
this.error();
};
private login(token: string) {
this.client.login(token);
};
private ready() {
this.client.on("ready", (cli) => {
this.logger.system(`Logged in as ${cli.user.tag}!`);
});
};
private async eventListener() {
try {
this.logger.system("Loading and preparing all events...");
await new EventHandler({ client: this.client }).register();
} catch (error) {
throw error;
}
}
private async commandListener() {
try {
this.logger.system("Loading and syncing all slash commands...");
await new CommandHandler().register();
} catch (error) {
throw error;
}
this.client.on("interactionCreate", async (interaction) => {
try {
if (!interaction.isCommand()) return;
const name = interaction.commandName;
const cmd = await this.commands.getCommand(name);
if (!cmd) return;
const commandFile = require(cmd.path);
const cmdClass = new commandFile.default();
await cmdClass.run(interaction);
} catch (err) {
console.error(err);
}
});
}
private error() {
this.client.on("error", (error) => {
throw error;
});
};
};