-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLeo.js
387 lines (332 loc) · 10.3 KB
/
Leo.js
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
import discord, { Options } from "discord.js";
import { User } from "./database.js";
import utils from "./utils.js";
import PackageSearch from "./classes/PackageSearch.js";
import ReputationManager from "./classes/ReputationManager.js";
import Greeter from "./classes/Greeter.js";
import Database from "better-sqlite3";
import PollManager from "./classes/PollManager.js";
const { Client, Message } = discord;
/**
* @typedef {import("discord.js").Client} Client
* @typedef {import("discord.js").Message} Message
* @typedef {import("discord.js").MessageReaction} MessageReaction
* @typedef {import("discord.js").User} DiscordUser
* @typedef {import("sequelize").Sequelize} Sequelize
*/
/**
* The League of Extraordinary Foundry VTT Developers Discord Bot
*
* @class Leo
*/
export default class Leo {
/**
* Creates an instance of Leo.
*
* @param {LeoConfig} config - The configuration file data for this instance of Leo
* @param {Sequelize} sql - A reference to the sqlite ORM
* @param {Client} client - A reference to the Discord.js client
* @memberof Leo
*/
constructor(config, sql, client) {
/** @type {LeoConfig} */
this.config = config;
/** @type {Sequelize} */
this.sql = sql;
/** @type {Client} */
this.client = client;
this.reputation = new ReputationManager(this);
this.packages = new PackageSearch(this);
this.greeter = new Greeter(this);
this.polls = new PollManager(this);
}
/**
* Initialize required members and structures for Leo
*
* @memberof Leo
*/
async init() {
await this.makeBackup();
await User.init(this.sql);
await this.reputation.init();
await this.packages.init();
await this.createListeners();
await this.client.login(this.config.token);
await this.polls.init();
}
/**
* Wraps fetching the remaining data for a partial Discord.js structure
* in a condition and error handling.
*
* @param {object} data - Some Discord.js object
* @return {boolean} True if the partial was fetches successfully
* @memberof Leo
*/
async fetchPartial(data) {
if (data.partial) {
try { await data.fetch(); } catch (error) {
console.error(`Something went wrong when fetching partial ${data.id}: `, error);
return false;
}
}
return true;
}
/**
* Fires once when the bot is ready.
*
* @memberof Leo
*/
async onReady() {
console.log("🦁 Leo ready!");
}
/**
* Receives message creation events, and delegates handling them.
*
* Ignores bots.
*
* Eats errors.
*
* @param {Message} message
* @return {void}
* @memberof Leo
*/
async onMessageCreate(message) {
// if (message.author.bot) return;
utils.debug(message);
try {
await Promise.all([
this.reputation.handleMessage(message),
this.greeter.handleMessage(message)
]);
} catch (e) { console.error(e); }
}
/**
* Receives message reaction creation events, and delegates handling them.
*
* Eats errors.
*
* @param {MessageReaction} reaction - The reaction that was created
* @param {DiscordUser} user - The user who reacted
* @return {void}
* @memberof Leo
*/
async onMessageReactionAdd(reaction, user) {
if (!await this.fetchPartial(reaction)) return;
utils.debug(reaction, user);
try{
await Promise.all([
this.reputation.handleReaction(reaction, user)
]);
} catch(e) { console.error(e); }
}
/**
* Delegates /slash command interaction handling.
*
* @param {Interaction} interaction
* @memberof Leo
*/
async onInteractionCreate(interaction) {
//utils.debug(interaction);
//console.log(JSON.stringify(interaction));
try {
await Promise.all([
this.reputation.handleInteraction(interaction),
this.packages.handleInteraction(interaction),
this.polls.handleInteraction(interaction),
]);
} catch (e) { console.error(e); }
}
/**
* Registers the event handlers for this bot
*
* @memberof Leo
*/
async createListeners() {
this.client.once("ready", this.onReady.bind(this));
this.client.on("messageCreate", this.onMessageCreate.bind(this));
this.client.on("messageReactionAdd", this.onMessageReactionAdd.bind(this));
// this.client.on("guildMemberAdd", this.greeter.handleMessage.bind(this.greeter));
this.client.ws.on('INTERACTION_CREATE', this.onInteractionCreate.bind(this));
this.client.on("interactionCreate", this.onInteractionCreate.bind(this));
}
async updateUser(id, user) {
if (!user) user = await this.client.users.fetch(id);
await User.upsert({
id, name: user.username,
discriminator: user.discriminator
});
}
/* async fetchUserInfo(id) {
// Default in case the user is not findable
let info = {
id, name: "User-not-found",
discriminator: "0000", tag: "User-not-found#0000"
};
let user, db;
// Check the users cache first
let cache = this.client.users.cache.get(id);
if (cache) info = {
id, name: cache.username,
discriminator: cache.discriminator, tag: cache.tag
}
else {
// If it's not in the cache, check the database
db = await User.findOne({ where: { id: id } });
console.log(db);
if (db) info = {
id, name: db.name,
discriminator: db.discriminator,
tag: `${db.name}#${db.discriminator}`
}
else {
// If it's not in the database, fetch it from Discord
user = await this.client.users.fetch(id);
if (user) info = {
id, name: user.username,
discriminator: user.discriminator, tag: user.tag
}
}
}
// Update the database, use the cache or user data if it was already loaded
// Do not await this, it can happen in the background.
this.updateUserInfo(id, cache || user ? info : null);
return info;
}
*/
/* async updateUserInfo(id, info) {
// If new data was given, insert it
if (info) return await User.upsert(info);
// Otherwise fetch the data
const user = await this.client.users.fetch(id);
// If no data was found, stop.
if (!user) return;
// Insert the new data
return await User.upsert({
id, name: user.username,
discriminator: user.discriminator
});
}
*/
/**
* Sends a response to an interaction.
*
* Optionally returns the message that was sent in response tot he interaction.
*
* @param {Interaction} interaction - The interaction to respond to
* @param {InteractionResponse} data - The response data
* @param {boolean} awaitResponse - When true, the response message will be fetched and returned
* @return {Promise<Message>|Promise<null>} The message that was sent in response
* @memberof Leo
*/
async respond(interaction, data, awaitResponse=false, update=false) {
this.cleanData(data);
// Send the response
await this.client.api.interactions(interaction.id, interaction.token)
.callback.post({ data: { type: update ? 7 : 4, data: data } });
if (!awaitResponse) return null;
// Get the channel of the interaction
let channel = this.client.channels.fetch(interaction.channel_id);
// Fetch the response message by using PATCH on the special endpoint.
// This is not the "correct" way to do this, but the API is not mature yet.
// Nevertheless, this will return the message data.
let response = this.client.api
.webhooks(interaction.application_id, interaction.token, "messages", "@original")
.patch({ data: {} });
[channel, response] = await Promise.all([channel, response]);
// Construct a Discord.js Message object around the message data, and return
return new Message(this.client, response, channel);
}
/**
* Checks each field in the response, and ensures they do not exceed their maximum length.
*
* Does not protect against too many embeds, too many fields, or embeds with
* greater than 6000 total characters.
*
* @param {InteractionResponse} data - The response data
* @memberof Leo
*/
cleanData(data) {
const contentMax = 2000;
const titleMax = 256; const authorMax = 256; const descrMax = 2048;
const footerMax = 2048; const nameMax = 256; const valueMax = 1024;
if (data.content?.length > contentMax)
data.content = data.content.substring(0, contentMax - 3) + "...";
data.embeds?.forEach(embed => {
console.log(embed);
embed.title = embed.title?.clamp(titleMax, "...");
if (embed.author)
embed.author.name = embed.author.name?.clamp(authorMax, "...");
embed.description = embed.description?.clamp(descrMax, "...");
if (embed.footer)
embed.footer.text = embed.footer.text?.clamp(footerMax, "...");
embed.fields?.forEach(field => {
field.name = field.name?.clamp(nameMax, "...");
field.value = field.value?.clamp(valueMax, "...");
});
});
}
/**
* Create a backup of the database.
*
* @return {void}
* @memberof Leo
*/
async makeBackup() {
if (!this.config.backup) return;
console.log("Starting Backup...");
console.time("Backup completed in");
const backupPath =
// dir/prefix.date.db
`${this.config.backup.dir}/${this.config.backup.prefix}.${new Date(Date.now()).toISOString()}.db`;
const db = new Database(this.config.database);
try {
const backup = await db.backup(backupPath);
utils.debug("Backup Created: ", backupPath, backup);
}
catch (e) {
console.error(e);
}
finally {
db.close();
console.timeEnd("Backup completed in");
}
}
/**
* Runs whenever the process exits, closes the database connection
* and logs Leo out of Discord (shows him as offline).
*
* @param {string} signal - The exit signal/code/error
* @return {void}
* @memberof Leo
*/
handleExit(signal) {
if (this.exiting) return; // Don't run this more than once
else this.exiting = true;
console.log(`\nReceived ${signal}`);
console.log(`Disconnecting Leo!`);
this.client.destroy();
this.sql.close();
console.log("Bye! 🦁");
process.exit();
}
}
/* Leftovers from earlier development not yet re-factored
// A command to make Leo send a message in a certain channel,
// Useless without better permissions system
async function handleSayCommand(interaction) {
utils.debug(client.channels);
const channel = interaction.data.options.find(o => o.name == "channel")?.value;
const message = interaction.data.options.find(o => o.name == "message")?.value;
utils.debug(`
Sending message on channel: ${channel}
> ${message}
`);
await client.api.interactions(interaction.id, interaction.token).callback.post({data: {
type: 4,
data: {
content: `Sending message to ${channel}`
}
}});
const ch = await client.channels.fetch(channel);
ch.send(message);
}*/