-
Notifications
You must be signed in to change notification settings - Fork 1
/
util_functions.ts
736 lines (724 loc) · 20.6 KB
/
util_functions.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
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
import Discord, {
ColorResolvable,
MessageActionRow,
MessageButton,
MessageSelectMenu,
Snowflake,
} from 'discord.js';
import parse_duration from 'parse-duration';
import node_fetch from 'node-fetch';
import * as Types from './types.js';
import { Defer } from './defer.js';
import { PrismaClient } from '@prisma/client';
import { RawGuildData, RawMessageData } from 'discord.js/typings/rawDataTypes';
const prisma = new PrismaClient();
import nodefetch from 'node-fetch';
/**
* Get a random int in a range
*/
export function randomIntFromInterval(min: number, max: number): number {
return Math.floor(Math.random() * (max - min + 1) + min);
}
/**
* Convert string to simple embed
*/
export function desc_embed(text: string): { embeds: Discord.MessageEmbed[] } {
return {
embeds: [
new Discord.MessageEmbed().setDescription(text).setColor(COLORS.decorate),
],
};
}
/**
* Truncate a string to a certain length, adding an ellipsis if necessary
*/
export function truncate(str: string, length: number): string {
if (str.length <= length) return str;
return str.substring(0, length - 3) + '...';
}
/**
* Stringify an object into a string
*/
export const COLORS = {
success: '#1dbb4f' as ColorResolvable,
warning: '#d8ae2b' as ColorResolvable,
tip: '#397cd1' as ColorResolvable,
error: '#e74d4d' as ColorResolvable,
decorate: '#ff5894' as ColorResolvable,
};
/**
* Create an embed showing various message types
* @param {string} text - The embed's description
* @param {string?} title - (Optional) The embed's title
*/
export function embed(
text: string,
type: 'success' | 'warning' | 'tip' | 'error',
title?: string
): { embeds: Discord.MessageEmbed[] } {
return {
embeds: [
new Discord.MessageEmbed()
.setTitle(
title == undefined
? {
success: 'Success!',
warning: 'Warning!',
tip: 'Tip!',
error: 'Error!',
}[type]
: title
)
.setDescription(text)
.setColor(
{
success: '#1dbb4f',
warning: '#d8ae2b',
tip: '#397cd1',
error: COLORS.error,
}[type] as Discord.ColorResolvable
),
],
};
}
/**
* Get a random item from an array
*/
export function randArrayItem<T>(items: Array<T>): T {
return items[~~(items.length * Math.random())];
}
export async function schedule_event(
event: unknown,
time: string
): Promise<void> {
let ptime = parse_duration(time, 's') || 10;
if (ptime < 10) ptime = 10;
await prisma.timerevents.create({
data: {
timestamp: Math.round(Date.now() / 1000) + ptime,
event: JSON.stringify(event),
},
});
}
export function shuffle<T>(a: Array<T>): Array<T> {
for (let i = a.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[a[i], a[j]] = [a[j], a[i]];
}
return a;
}
export async function awaitMessageComponent(
msg: Discord.Message,
options:
| Discord.AwaitMessageComponentOptions<Discord.MessageComponentInteraction>
| undefined
): Promise<Discord.MessageComponentInteraction | undefined> {
try {
return await msg.awaitMessageComponent(options);
} catch (err) {
return undefined;
}
}
export async function awaitMessageComponentByAuthor(
msg: Discord.Message,
options: {
time?: number;
filter?: (m: Discord.MessageComponentInteraction) => boolean;
author: Snowflake;
}
): Promise<Discord.MessageComponentInteraction | undefined> {
return new Promise((resolve, reject) => {
(async () => {
const f = (i: Discord.Interaction) => {
const b = i as Discord.MessageComponentInteraction;
if (
i.user.id === options.author &&
(options?.filter == null || options.filter(b))
) {
msg.client.removeListener('interaction', f);
resolve(b);
} else if (i.user.id !== options.author) {
b.reply({
embeds: [
new Discord.MessageEmbed()
.setTitle('Interaction Failed')
.setDescription(
'This interaction can only be used by the person who triggered it'
)
.setColor(COLORS.error),
],
ephemeral: true,
});
} else {
b.reply({
embeds: [
new Discord.MessageEmbed()
.setTitle('Interaction Failed')
.setDescription(
'This interaction failed the bot-enforced filter'
)
.setColor(COLORS.error),
],
ephemeral: true,
});
}
};
msg.client.on('interaction', f);
if (options?.time)
setTimeout(() => {
resolve(undefined);
}, options.time);
})();
});
}
export async function confirm(message: Discord.Message): Promise<boolean> {
const deferred = await Defer.add({
type: 'SendCancelledMessage',
channel: message.channel.id,
});
deferred.cancelIn(20000);
const row = new MessageActionRow().addComponents(
new MessageButton()
.setLabel('Cancel')
.setStyle('SUCCESS')
.setCustomId('cancel'),
new MessageButton()
.setLabel('Confirm')
.setStyle('DANGER')
.setCustomId('confirm')
.setDisabled(true)
);
const msg = await message.channel.send({
embeds: [
new Discord.MessageEmbed()
.setTitle('Click Confirm when it is enabled to confirm')
.setColor(COLORS.warning),
],
components: [row],
});
setTimeout(async () => {
row.components[1].setDisabled(false);
msg.edit({
components: [row],
});
}, 3000);
const interaction = await awaitMessageComponentByAuthor(msg, {
time: 10000,
author: message.author.id,
});
if (interaction) interaction.deferUpdate();
if (interaction?.customId == 'confirm') {
msg.edit({
embeds: [
new Discord.MessageEmbed()
.setTitle('Confirmed')
.setColor(COLORS.success),
],
components: [],
});
await deferred.cancel();
return true;
} else {
msg.edit({
embeds: [
new Discord.MessageEmbed()
.setTitle('Confirmation Failed')
.setColor(COLORS.error),
],
components: [],
});
await deferred.cancel();
return false;
}
}
export async function embed_options(
title: string,
options: string[],
set: string[],
message: EMessage,
time?: number
): Promise<number | null> {
const deferred = await Defer.add({
type: 'SendCancelledMessage',
channel: message.channel.id,
});
deferred.cancelIn(time || 15000);
const n_options = [];
for (let i = 0; i < options.length; i++) {
if (isNaN(parseInt(set[i]))) {
n_options.push({
label: options[i],
value: set[i],
emoji: set[i],
md: set[i] + ' ' + options[i],
});
} else {
n_options.push({
label: options[i],
value: set[i],
emoji: message.client.emojis.cache.get(set[i] as Snowflake),
md: '<:emoji:' + set[i] + '>' + ' ' + options[i],
});
}
}
const msg = await message.dbReply({
embeds: [
new Discord.MessageEmbed().setTitle(title).setColor(COLORS.decorate),
//.setDescription(n_options.join('\n')),
],
components: [
new MessageActionRow().addComponents(
new MessageSelectMenu()
.setPlaceholder('Nothing selected...')
.setCustomId('menu')
.addOptions(n_options)
),
],
});
const interaction = await awaitMessageComponentByAuthor(msg, {
time: time || 15000,
author: message.author.id,
});
if (interaction?.isSelectMenu() && interaction.values.length > 0) {
interaction.deferUpdate();
msg.edit({
embeds: [
new Discord.MessageEmbed()
.setTitle(title)
.setDescription(n_options[set.indexOf(interaction.values[0])].md)
.setColor(COLORS.success),
],
components: [],
});
await deferred.cancel();
if (interaction.isSelectMenu()) {
return set.indexOf(interaction.values[0]);
} else {
throw new BotError('bot', 'Invalid component type');
}
} else {
msg.edit({
embeds: [
new Discord.MessageEmbed().setTitle('Cancelled').setColor(COLORS.error),
],
components: [],
});
await deferred.cancel();
return null;
}
}
export async function cleanPings(
text: string,
guild: Discord.Guild
): Promise<string> {
let cleaned = text
.split('@everyone')
.join('@everyone')
.split('@here')
.join('@here');
const role_pings = [...cleaned.matchAll(/<@&[0-9]+>/g)];
for (const ping of role_pings) {
const pinged_role = guild.roles.cache.get(
ping.toString().replace('<@&', '').replace('>', '') as Snowflake
);
if (!pinged_role?.mentionable)
cleaned = cleaned.replace(
ping.toString(),
// eslint-disable-next-line no-irregular-whitespace
`@${pinged_role ? pinged_role.name : 'deleted-role'}`
);
}
return cleaned;
}
export function assertHasPerms(
guild: Discord.Guild,
perms: Array<Discord.PermissionResolvable>
): void {
for (const perm of perms) {
if (!guild.me?.permissions.has(perm))
throw new BotError(
'user',
`ModBot needs the ${perm} permission to do this`
);
}
}
export function warnIfNoPerms(
msg: Discord.Message,
perms: Array<Discord.PermissionResolvable>
): void {
for (const perm of perms) {
if (!msg.guild?.me?.permissions.has(perm))
msg.channel.send(
desc_embed(
`ModBot should have the ${perm} permission for best results, continuing anyways`
)
);
}
}
export class BotError extends Error {
type: string;
message: string;
name: string;
constructor(type: 'user' | 'bot', message: string) {
// Pass remaining arguments (including vendor specific ones) to parent constructor
super();
// Maintains proper stack trace for where our error was thrown (only available on V8)
if (Error.captureStackTrace) {
Error.captureStackTrace(this, BotError);
}
this.name = 'BotError';
// Custom debugging information
this.type = type;
this.message = message;
}
}
export async function ask(
question: string,
time: number,
msg: EMessage
): Promise<string> {
const deferred = await Defer.add({
type: 'SendCancelledMessage',
channel: msg.channel.id,
});
deferred.cancelIn(time);
await msg.dbReply(question);
const sb_name = await msg.channel.awaitMessages({
max: 1,
time: time,
filter: (m) => m.author.id == msg.author.id,
});
await deferred.cancel();
if (![...sb_name.values()].length) throw new BotError('user', 'Timed out');
if ([...[...sb_name.values()][0].attachments.values()].length)
throw new BotError(
'user',
'Attachments are not supported. If you want to add an image, use a link to it'
);
return [...sb_name.values()][0].content;
}
export async function askOrNone(
question: string,
time: number,
msg: EMessage
): Promise<string | undefined> {
const deferred = await Defer.add({
type: 'SendCancelledMessage',
channel: msg.channel.id,
});
deferred.cancelIn(time);
return new Promise((resolve, reject) => {
(async () => {
let addedEmoji = false;
const repm = await msg.dbReply({
content: question,
components: [
new MessageActionRow().addComponents(
new MessageButton()
.setLabel('Cancel')
.setStyle('DANGER')
.setCustomId('cancel')
),
],
});
awaitMessageComponentByAuthor(repm, {
time: time,
author: msg.author.id,
}).then((reactions) => {
console.log(reactions);
if (reactions) {
reactions.deferUpdate();
addedEmoji = true;
deferred.cancel();
resolve('');
}
});
const sb_name = await msg.channel.awaitMessages({
max: 1,
time: time,
filter: (m) => m.author.id == msg.author.id,
});
await deferred.cancel();
if (![...sb_name.values()].length && !addedEmoji) {
reject(new BotError('user', 'Timed out'));
return;
}
if (!addedEmoji) resolve([...sb_name.values()][0].content);
})();
});
}
const stringVars = {
botName: 'ModBot',
};
export function fillStringVars(text: string): string {
const regex = /__.*__/g;
const found = text.match(regex);
if (found)
for (const item of found) {
if (found.toString() === '__botName__')
text = text.replace(
item,
stringVars[item.replace('__', '').replace('__', '') as 'botName']
);
}
return text;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const { Structures } = (await import('discord.js')) as any;
export class EGuild extends Discord.Guild {
constructor(client: Discord.Client, guild: RawGuildData) {
super(client, guild);
}
get starboard(): Promise<{
channel: string;
server: string;
stars: number;
} | null> {
return prisma.starboards.findFirst({
where: {
server: this.id,
},
});
}
get hasPluralKit(): boolean {
return !!this.members.cache.get('466378653216014359');
}
}
Structures.extend('Guild', () => {
return EGuild;
});
export class EMessage extends Discord.Message {
constructor(client: Discord.Client, message: RawMessageData) {
super(client, message);
}
async isPoll(): Promise<boolean> {
return !!(await Types.Poll.query().where('message', this.id)).length;
}
ask(question: string, time: number): Promise<string> {
return ask(question, time, this);
}
askOrNone(question: string, time: number): Promise<string | undefined> {
return askOrNone(question, time, this);
}
async dbReply(
content: string | Discord.MessagePayload | Discord.MessageOptions
): Promise<Discord.Message> {
if (!this.guild)
throw new BotError('bot', 'dbReply was called outside of a guild');
const bmsg = await this.channel.send(content);
await Types.BotMessage.query().insert({
guild: this.guild.id,
channel: this.channel.id,
message: this.id,
botMessage: bmsg.id,
});
return bmsg;
}
async getPluralKitSender(): Promise<undefined | Discord.GuildMember> {
try {
if (!this.guild) return undefined;
return this.guild.members.cache.get(
(
(await (
await node_fetch('https://api.pluralkit.me/v1/msg/' + this.id)
).json()) as any
).sender
);
} catch (e) {
return undefined;
}
}
async getAnonSender(): Promise<null | Discord.GuildMember | undefined> {
await sleep(200);
const tmp = await prisma.anonmessages.findFirst({
where: {
id: this.id,
},
});
return tmp
? this.guild?.members.cache.get(tmp.user as Snowflake)
: undefined;
}
async isPluralKitMessage(): Promise<boolean> {
if (this.webhookId && (this.guild as EGuild | undefined)?.hasPluralKit) {
return !!(await this.getPluralKitSender());
} else {
return false;
}
}
async isAnonMessage(): Promise<boolean> {
if (this.webhookId) {
return !!(await this.getAnonSender());
} else {
return false;
}
}
async getRealMember(): Promise<Discord.GuildMember | undefined | null> {
if (this.webhookId) {
const anonsender = await this.getAnonSender();
if (anonsender) {
return anonsender;
}
}
if (this.webhookId && (this.guild as EGuild | undefined)?.hasPluralKit) {
return await this.getPluralKitSender();
} else {
return this.member;
}
}
}
Structures.extend('Message', () => {
return EMessage;
});
function sleep(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
export function chunk<T>(arr: T[], len: number): T[][] {
const chunks = [];
let i = 0;
const n = arr.length;
while (i < n) {
chunks.push(arr.slice(i, (i += len)));
}
return chunks;
}
// Make BigInt serializable
// eslint-disable-next-line @typescript-eslint/no-redeclare, no-unused-vars
interface BigInt {
/** Convert to BigInt to string form in JSON.stringify */
toJSON: () => string;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(BigInt.prototype as any)['toJSON'] = function () {
return this.toString();
};
export interface ChatGPTMessage {
role: 'user' | 'assistant' | 'system';
content: string;
}
export interface ChatGPTQueryOptions {
defaultOnFailure?: string;
model?: 'gpt-3.5-turbo' | 'gpt-4';
temp?: number;
}
export async function queryChatGPT(
chat: ChatGPTMessage[],
options: ChatGPTQueryOptions = {}
): Promise<ChatGPTMessage> {
try {
const responses = (await (
await nodefetch('https://api.openai.com/v1/chat/completions', {
headers: {
Authorization: 'Bearer ' + process.env.OPENAI_KEY,
'Content-Type': 'application/json',
},
method: 'POST',
body: JSON.stringify({
model: options.model || 'gpt-3.5-turbo',
messages: chat,
temperature: options.temp,
}),
})
).json()) as any;
const response = responses.choices[0].message as ChatGPTMessage;
if (
(!response || !response.content) &&
options.defaultOnFailure !== undefined
)
return {
role: 'assistant',
content: options.defaultOnFailure,
};
return response;
} catch (e) {
if (options.defaultOnFailure !== undefined)
return {
role: 'assistant',
content: options.defaultOnFailure,
};
else throw e;
}
}
export type ChatGPTStreamingDelta =
| ChatGPTStreamingDeltaContent
| ChatGPTStreamingDeltaError;
export interface ChatGPTStreamingDeltaContent {
choices: {
delta: {
content?: string;
role?: 'user' | 'assistant' | 'system';
};
// eslint-disable-next-line @typescript-eslint/naming-convention
finish_reason: 'null' | 'stop';
}[];
created: number;
idd: string;
model: string;
object: 'chat.completion.chunk';
}
export interface ChatGPTStreamingDeltaError {
error: {
message: string;
};
}
/**
* Query OpenAI's GPT-3.5 Turbo or GPT-4 model with a chat message to generate a streaming response
* @param chat - An array of chat messages to feed to the model
* @param model - Which GPT model to use. Can be either 'gpt-3.5-turbo' or 'gpt-4'.
* @param apiKey - Your OpenAI API key
* @param onMessage - A callback function to handle the response from the API
*/
export async function queryChatGPTStreaming(
chat: ChatGPTMessage[],
model: 'gpt-3.5-turbo' | 'gpt-4',
onMessage: (arg0: ChatGPTStreamingDelta) => void
): Promise<void> {
console.log(chat);
// Make a POST request to the OpenAI API to get a response from the specified GPT model
const data = await nodefetch('https://api.openai.com/v1/chat/completions', {
headers: {
// Set the Authorization header to contain the API key
// eslint-disable-next-line @typescript-eslint/naming-convention
Authorization: 'Bearer ' + process.env.OPENAI_KEY,
// Set the Content-Type header to indicate that we are sending JSON data
// eslint-disable-next-line @typescript-eslint/naming-convention
'Content-Type': 'application/json',
},
method: 'POST',
// Send a JSON payload containing the model, whether to use streaming mode or not, and the array of chat messages
body: JSON.stringify({
model,
stream: true,
messages: chat,
}),
});
// Create and return a new promise that will be resolved when the API indicates that the streaming is complete
await new Promise<void>((resolve, reject) => {
// Listen for incoming data from the API
data.body?.on('data', (chunk) => {
// Split the chunk into individual lines, remove any empty or whitespace lines
const lines = chunk
.toString()
.split('\n')
.filter((line: string) => line.trim() !== '');
// Iterate over each line of the response
for (const line of lines) {
// Remove the prefix 'data: ' from the line
const message = line.replace(/^data: /, '');
if (message === '[DONE]') {
// If the message is [DONE], resolve the Promise to indicate that the streaming is complete
resolve();
}
try {
// Try to JSON parse the message into a ChatGPTStreamingDelta object
const delta: ChatGPTStreamingDelta = JSON.parse(message);
// Call the callback function with the ChatGPTStreamingDelta object
onMessage(delta);
} catch (error) {
// Reject the promise when an error occurs
reject('Could not JSON parse stream message');
}
}
});
});
}