-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbot.ts
192 lines (167 loc) · 5.45 KB
/
bot.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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import {
Client,
Constants,
Guild,
Interaction,
Message,
MessageReaction,
PartialMessageReaction,
PartialUser,
RateLimitData,
User,
} from 'discord.js';
import {
CommandHandler,
GuildJoinHandler,
GuildLeaveHandler,
MessageHandler,
ReactionHandler,
} from './events';
import { JobService, Logger } from './services';
import { PartialUtils } from './utils';
let Config = require('../config/config.json');
let Debug = require('../config/debug.json');
let Logs = require('../lang/logs.json');
export class Bot {
private ready = false;
constructor(
private token: string,
private client: Client,
private guildJoinHandler: GuildJoinHandler,
private guildLeaveHandler: GuildLeaveHandler,
private messageHandler: MessageHandler,
private commandHandler: CommandHandler,
private reactionHandler: ReactionHandler,
private jobService: JobService
) {}
public async start(): Promise<void> {
this.registerListeners();
await this.login(this.token);
}
private registerListeners(): void {
this.client.on(Constants.Events.CLIENT_READY, () => this.onReady());
this.client.on(
Constants.Events.SHARD_READY,
(shardId: number, unavailableGuilds: Set<string>) =>
this.onShardReady(shardId, unavailableGuilds)
);
this.client.on(Constants.Events.GUILD_CREATE, (guild: Guild) => this.onGuildJoin(guild));
this.client.on(Constants.Events.GUILD_DELETE, (guild: Guild) => this.onGuildLeave(guild));
this.client.on(Constants.Events.MESSAGE_CREATE, (msg: Message) => this.onMessage(msg));
this.client.on(Constants.Events.INTERACTION_CREATE, (intr: Interaction) =>
this.onInteraction(intr)
);
this.client.on(
Constants.Events.MESSAGE_REACTION_ADD,
(messageReaction: MessageReaction | PartialMessageReaction, user: User | PartialUser) =>
this.onReaction(messageReaction, user)
);
this.client.on(Constants.Events.RATE_LIMIT, (rateLimitData: RateLimitData) =>
this.onRateLimit(rateLimitData)
);
}
private async login(token: string): Promise<void> {
try {
await this.client.login(token);
} catch (error) {
Logger.error(Logs.error.clientLogin, error);
return;
}
}
private async onReady(): Promise<void> {
let userTag = this.client.user?.tag;
Logger.info(Logs.info.clientLogin.replaceAll('{USER_TAG}', userTag));
if (!Debug.dummyMode.enabled) {
this.jobService.start();
}
this.ready = true;
Logger.info(Logs.info.clientReady);
}
private onShardReady(shardId: number, _unavailableGuilds: Set<string>): void {
Logger.setShardId(shardId);
}
private async onGuildJoin(guild: Guild): Promise<void> {
if (!this.ready || Debug.dummyMode.enabled) {
return;
}
try {
await this.guildJoinHandler.process(guild);
} catch (error) {
Logger.error(Logs.error.guildJoin, error);
}
}
private async onGuildLeave(guild: Guild): Promise<void> {
if (!this.ready || Debug.dummyMode.enabled) {
return;
}
try {
await this.guildLeaveHandler.process(guild);
} catch (error) {
Logger.error(Logs.error.guildLeave, error);
}
}
private async onMessage(msg: Message): Promise<void> {
if (
!this.ready ||
(Debug.dummyMode.enabled && !Debug.dummyMode.whitelist.includes(msg.author.id))
) {
return;
}
msg = await PartialUtils.fillMessage(msg);
if (!msg) {
return;
}
try {
await this.messageHandler.process(msg);
} catch (error) {
Logger.error(Logs.error.message, error);
}
}
private async onInteraction(intr: Interaction): Promise<void> {
if (
!intr.isCommand() ||
!this.ready ||
(Debug.dummyMode.enabled && !Debug.dummyMode.whitelist.includes(intr.user.id))
) {
return;
}
try {
await this.commandHandler.process(intr);
} catch (error) {
Logger.error(Logs.error.command, error);
}
}
private async onReaction(
msgReaction: MessageReaction | PartialMessageReaction,
reactor: User | PartialUser
): Promise<void> {
if (
!this.ready ||
(Debug.dummyMode.enabled && !Debug.dummyMode.whitelist.includes(reactor.id))
) {
return;
}
msgReaction = await PartialUtils.fillReaction(msgReaction);
if (!msgReaction) {
return;
}
reactor = await PartialUtils.fillUser(reactor);
if (!reactor) {
return;
}
try {
await this.reactionHandler.process(
msgReaction,
msgReaction.message as Message,
reactor
);
} catch (error) {
Logger.error(Logs.error.reaction, error);
}
}
private async onRateLimit(rateLimitData: RateLimitData): Promise<void> {
if (rateLimitData.timeout >= Config.logging.rateLimit.minTimeout * 1000) {
Logger.error(Logs.error.apiRateLimit, rateLimitData);
}
}
}