-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.ts
827 lines (696 loc) · 40.5 KB
/
index.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
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
import { Bot, Channel, Collection, Guild, Member, Role, User, type DesiredPropertiesBehavior, type SetupDesiredProps, type TransformersDesiredProperties } from '@discordeno/bot';
import { setupCacheEdits } from './setupCacheEdits.js';
import { setupCacheRemovals } from './setupCacheRemovals.js';
import { setupDummyEvents } from './setupDummyEvents.js';
// Filter props from types of the objects based on the provided desired and undesired props
type FilterProps<T, Desired extends keyof T, Undesired extends keyof T> = LastInteractedTimeTrackedRecord<Omit<Pick<T, Desired>, Undesired>>;
type FilteredProxyCacheTypes<T extends ProxyCacheTypes<Props, Behavior>, Props extends TransformersDesiredProperties, Behavior extends DesiredPropertiesBehavior, O extends CreateProxyCacheOptions<T, Props, Behavior> = CreateProxyCacheOptions<T, Props, Behavior>> = {
[P in keyof T]: P extends 'guild'
? LastInteractedTimeTrackedRecord<
Omit<FilterProps<T[P], O['desiredProps'] extends Record<P, (keyof T[P])[]> ? O['desiredProps'][P][number] : keyof T[P], O['undesiredProps'] extends Record<P, (keyof T[P])[]> ? O['undesiredProps'][P][number] : never>, 'channels' | 'members' | 'roles' | 'lastInteractedTime'> & {
channels?: Collection<bigint, FilteredProxyCacheTypes<T, Props, Behavior, O>['channel']>;
members?: Collection<bigint, FilteredProxyCacheTypes<T, Props, Behavior, O>['member']>;
roles?: Collection<bigint, FilteredProxyCacheTypes<T, Props, Behavior, O>['role']>;
}
>
: FilterProps<T[P], O['desiredProps'] extends Record<P, (keyof T[P])[]> ? O['desiredProps'][P][number] : keyof T[P], O['undesiredProps'] extends Record<P, (keyof T[P])[]> ? O['undesiredProps'][P][number] : never>;
};
export interface ProxyCacheProps<T extends ProxyCacheTypes<DiscordenoDesiredProps, DiscordenoDesiredPropsBehavior>, DiscordenoDesiredProps extends TransformersDesiredProperties, DiscordenoDesiredPropsBehavior extends DesiredPropertiesBehavior, O extends CreateProxyCacheOptions<T, DiscordenoDesiredProps, DiscordenoDesiredPropsBehavior>> {
cache: {
options: CreateProxyCacheOptions<T, DiscordenoDesiredProps, DiscordenoDesiredPropsBehavior>;
channels: {
guildIds: Collection<bigint, bigint>;
memory: Collection<bigint, FilteredProxyCacheTypes<T, DiscordenoDesiredProps, DiscordenoDesiredPropsBehavior, O>['channel']>;
get: (id: bigint) => Promise<FilteredProxyCacheTypes<T, DiscordenoDesiredProps, DiscordenoDesiredPropsBehavior, O>['channel'] | undefined>;
set: (value: FilteredProxyCacheTypes<T, DiscordenoDesiredProps, DiscordenoDesiredPropsBehavior, O>['channel']) => Promise<void>;
delete: (id: bigint) => Promise<void>;
};
guilds: {
memory: Collection<bigint, FilteredProxyCacheTypes<T, DiscordenoDesiredProps, DiscordenoDesiredPropsBehavior, O>['guild']>;
get: (id: bigint) => Promise<FilteredProxyCacheTypes<T, DiscordenoDesiredProps, DiscordenoDesiredPropsBehavior, O>['guild'] | undefined>;
set: (value: FilteredProxyCacheTypes<T, DiscordenoDesiredProps, DiscordenoDesiredPropsBehavior, O>['guild']) => Promise<void>;
delete: (id: bigint) => Promise<void>;
};
members: {
get: (id: bigint, guildId: bigint) => Promise<FilteredProxyCacheTypes<T, DiscordenoDesiredProps, DiscordenoDesiredPropsBehavior, O>['member'] | undefined>;
set: (value: FilteredProxyCacheTypes<T, DiscordenoDesiredProps, DiscordenoDesiredPropsBehavior, O>['member']) => Promise<void>;
delete: (id: bigint, guildId: bigint) => Promise<void>;
};
roles: {
guildIds: Collection<bigint, bigint>;
get: (id: bigint) => Promise<FilteredProxyCacheTypes<T, DiscordenoDesiredProps, DiscordenoDesiredPropsBehavior, O>['role'] | undefined>;
set: (value: FilteredProxyCacheTypes<T, DiscordenoDesiredProps, DiscordenoDesiredPropsBehavior, O>['role']) => Promise<void>;
delete: (id: bigint) => Promise<void>;
};
users: {
memory: Collection<bigint, FilteredProxyCacheTypes<T, DiscordenoDesiredProps, DiscordenoDesiredPropsBehavior, O>['user']>;
get: (id: bigint) => Promise<FilteredProxyCacheTypes<T, DiscordenoDesiredProps, DiscordenoDesiredPropsBehavior, O>['user'] | undefined>;
set: (value: FilteredProxyCacheTypes<T, DiscordenoDesiredProps, DiscordenoDesiredPropsBehavior, O>['user']) => Promise<void>;
delete: (id: bigint) => Promise<void>;
};
$inferredTypes: {
channel: FilteredProxyCacheTypes<T, DiscordenoDesiredProps, DiscordenoDesiredPropsBehavior, O>['channel'];
guild: FilteredProxyCacheTypes<T, DiscordenoDesiredProps, DiscordenoDesiredPropsBehavior, O>['guild'];
member: FilteredProxyCacheTypes<T, DiscordenoDesiredProps, DiscordenoDesiredPropsBehavior, O>['member'];
role: FilteredProxyCacheTypes<T, DiscordenoDesiredProps, DiscordenoDesiredPropsBehavior, O>['role'];
user: FilteredProxyCacheTypes<T, DiscordenoDesiredProps, DiscordenoDesiredPropsBehavior, O>['user'];
};
};
}
export type BotWithProxyCache<T extends ProxyCacheTypes<Props, Behavior>, Props extends TransformersDesiredProperties, Behavior extends DesiredPropertiesBehavior, B extends Bot<Props, Behavior>, O extends CreateProxyCacheOptions<T, Props, Behavior> = CreateProxyCacheOptions<T, Props, Behavior>> = B & ProxyCacheProps<T, Props, Behavior, O>;
export const createProxyCache = <Props extends TransformersDesiredProperties, Behavior extends DesiredPropertiesBehavior, B extends Bot<Props, Behavior>, T extends ProxyCacheTypes<Props, Behavior> = ProxyCacheTypes<Props, Behavior>, O extends CreateProxyCacheOptions<T, Props, Behavior> = CreateProxyCacheOptions<T, Props, Behavior>>(
rawBot: Bot<Props, Behavior> & B,
options: O
): BotWithProxyCache<T, Props, Behavior, B, O> => {
const bot = rawBot as BotWithProxyCache<T, Props, Behavior, B>;
// @ts-ignore
bot.cache = { options };
const pendingGuildsData = new Collection<
bigint,
{
channels?: Collection<bigint, FilteredProxyCacheTypes<T, Props, Behavior>['channel']>;
members?: Collection<bigint, FilteredProxyCacheTypes<T, Props, Behavior>['member']>;
roles?: Collection<bigint, FilteredProxyCacheTypes<T, Props, Behavior>['role']>;
}
>();
if (!bot.cache.options.cacheInMemory) bot.cache.options.cacheInMemory = { default: true };
if (!bot.cache.options.cacheOutsideMemory) bot.cache.options.cacheOutsideMemory = { default: false };
const cacheInMemoryDefault = bot.cache.options.cacheInMemory.default;
const cacheOutsideMemoryDefault = bot.cache.options.cacheOutsideMemory.default;
bot.cache.options.cacheInMemory = {
channel: cacheInMemoryDefault,
guild: cacheInMemoryDefault,
member: cacheInMemoryDefault,
role: cacheInMemoryDefault,
user: cacheInMemoryDefault,
...bot.cache.options.cacheInMemory,
};
bot.cache.options.cacheOutsideMemory = {
channel: cacheOutsideMemoryDefault,
guild: cacheOutsideMemoryDefault,
member: cacheOutsideMemoryDefault,
role: cacheOutsideMemoryDefault,
user: cacheOutsideMemoryDefault,
...bot.cache.options.cacheOutsideMemory,
};
const internalBulkRemover = {
removeGuild: async (id: bigint) => {
// Remove from memory
bot.cache.guilds.memory.delete(id);
// Remove any associated channels
bot.cache.channels.memory.forEach((channel) => {
if ((channel as unknown as Channel).guildId === id) {
bot.cache.channels.memory.delete((channel as unknown as Channel).id);
bot.cache.channels.guildIds.delete((channel as unknown as Channel).id);
}
});
},
removeRole: async (id: bigint) => {
const guildId = bot.cache.roles.guildIds.get(id);
if (guildId) {
// Get the guild if its in cache
const guild = bot.cache.guilds.memory.get(guildId);
if (guild) {
// if roles are stored inside the guild remove it
guild.roles?.delete(id);
// Each mem who has this role needs to be edited and the role id removed
guild.members?.forEach((member) => {
if ((member as unknown as Member).roles?.includes(id)) (member as unknown as Member).roles = (member as unknown as Member).roles.filter((roleId) => roleId !== id);
});
}
}
bot.cache.roles.guildIds.delete(id);
},
};
if (!bot.cache.options.bulk) bot.cache.options.bulk = {};
// Get bulk removers passed by user, data about which internal removers to replace
const { removeGuild, removeRole } = bot.cache.options.bulk;
const { replaceInternalBulkRemover } = bot.cache.options.bulk;
// If user passed bulk.removeGuild else if replaceInternalBulkRemover.guild is not set to true
if (removeGuild || !replaceInternalBulkRemover?.guild) {
bot.cache.options.bulk.removeGuild = async (id) => {
// If replaceInternalBulkRemover.guild is not set to true, run internal guild bulk remover
if (!replaceInternalBulkRemover?.guild) await internalBulkRemover.removeGuild(id);
// If user passed bulk.removeGuild, run passed bulk remover
await removeGuild?.(id);
};
}
// If user passed bulk.removeRole else if replaceInternalBulkRemover.role is not set to true
if (removeRole || !replaceInternalBulkRemover?.role) {
bot.cache.options.bulk.removeRole = async (id) => {
// If replaceInternalBulkRemover.role is not set to true, run internal role bulk remover
if (!replaceInternalBulkRemover?.role) await internalBulkRemover.removeRole(id);
// If user passed bulk.removeRole, run passed bulk remover
await removeRole?.(id);
};
}
bot.cache.guilds = {
memory: new Collection(),
get: async (guildId) => {
// If available in memory, use it.
if (options.cacheInMemory?.guild) {
const guild = bot.cache.guilds.memory.get(guildId);
if (guild) {
guild.lastInteractedTime = Date.now();
return guild;
}
}
// Otherwise try to get from non-memory cache
if (!options.cacheOutsideMemory?.guild || !options.getItem) return;
const stored = await options.getItem('guild', guildId);
if (stored) {
stored.lastInteractedTime = Date.now();
if (options.cacheInMemory?.guild) bot.cache.guilds.memory.set(guildId, stored);
}
return stored;
},
set: async (guild) => {
// Should this be cached or not?
if (options.shouldCache?.guild && !(await options.shouldCache.guild(guild))) return;
guild.lastInteractedTime = Date.now();
// If user wants memory cache, we cache it
if (options.cacheInMemory?.guild) bot.cache.guilds.memory.set((guild as unknown as Guild).id, guild);
// If user wants non-memory cache, we cache it
if (options.cacheOutsideMemory?.guild && options.setItem) await options.setItem('guild', guild);
},
delete: async (guildId) => {
// Remove from memory
bot.cache.guilds.memory.delete(guildId);
// Remove from non-memory cache
await options.bulk?.removeGuild?.(guildId);
},
};
bot.cache.users = {
memory: new Collection(),
get: async (userId) => {
// If available in memory, use it.
if (options.cacheInMemory?.user) {
const user = bot.cache.users.memory.get(userId);
if (user) {
user.lastInteractedTime = Date.now();
return user;
}
}
// Otherwise try to get from non-memory cache
if (!options.cacheOutsideMemory?.user || !options.getItem) return;
const stored = await options.getItem('user', userId);
if (stored) {
stored.lastInteractedTime = Date.now();
if (options.cacheInMemory?.user) bot.cache.users.memory.set(userId, stored);
}
return stored;
},
set: async (user) => {
if (options.shouldCache?.user && !(await options.shouldCache.user(user))) return;
user.lastInteractedTime = Date.now();
// If user wants memory cache, we cache it
if (options.cacheInMemory?.user) bot.cache.users.memory.set((user as unknown as User).id, user);
// If user wants non-memory cache, we cache it
if (options.cacheOutsideMemory?.user && options.setItem) await options.setItem('user', user);
},
delete: async (userId) => {
// Remove from memory
bot.cache.users.memory.delete(userId);
// Remove from non-memory cache
if (options.removeItem) await options.removeItem('user', userId);
},
};
bot.cache.roles = {
guildIds: new Collection(),
get: async (roleId) => {
// If available in memory, use it.
if (options.cacheInMemory?.role) {
// If guilds are cached, roles will be inside them
if (options.cacheInMemory?.guild) {
const guildId = bot.cache.roles.guildIds.get(roleId);
if (guildId) {
const role = bot.cache.guilds.memory.get(guildId)?.roles?.get(roleId);
if (role) {
role.lastInteractedTime = Date.now();
return role;
}
}
}
}
// Otherwise try to get from non-memory cache
if (!options.cacheOutsideMemory?.role || !options.getItem) return;
const stored = await options.getItem('role', roleId);
if (stored) {
stored.lastInteractedTime = Date.now();
if (options.cacheInMemory?.role) bot.cache.roles.set(stored);
}
return stored;
},
set: async (internalRole) => {
if (options.shouldCache?.role && !(await options.shouldCache.role(internalRole))) return;
internalRole.lastInteractedTime = Date.now();
const role = internalRole as unknown as Role;
// If user wants memory cache, we cache it
if (options.cacheInMemory?.role) {
if (role.guildId) bot.cache.roles.guildIds.set(role.id, role.guildId);
if (options.cacheInMemory?.guild) {
const guildId = bot.cache.roles.guildIds.get(role.id);
if (guildId) {
const guild = bot.cache.guilds.memory.get(guildId);
if (guild) {
if (!guild.roles) guild.roles = new Collection();
guild.roles.set(role.id, internalRole);
} else {
const pendingGuild = pendingGuildsData.get(guildId);
if (!pendingGuild) pendingGuildsData.set(guildId, { channels: new Collection(), members: new Collection(), roles: new Collection() });
pendingGuildsData.get(guildId)?.roles!.set(role.id, internalRole);
}
} else console.warn(`[CACHE] Can't cache role(${role.id}) since guild.roles is enabled but a guild id was not found.`);
}
}
// If user wants non-memory cache, we cache it
if (options.cacheOutsideMemory?.role && options.setItem) await options.setItem('role', internalRole);
},
delete: async (roleId) => {
// Remove from memory
bot.cache.guilds.memory.get(bot.cache.roles.guildIds.get(roleId)!)?.roles?.delete(roleId);
bot.cache.roles.guildIds.delete(roleId);
// Remove from non-memory cache
if (options.removeItem) await options.removeItem('role', roleId);
},
};
bot.cache.members = {
get: async (memberId, guildId) => {
// If available in memory, use it.
if (options.cacheInMemory?.member) {
// If guilds are cached, members will be inside them
if (options.cacheInMemory?.guild) {
const member = bot.cache.guilds.memory.get(guildId)?.members?.get(memberId);
if (member) {
member.lastInteractedTime = Date.now();
return member;
}
}
}
// Otherwise try to get from non-memory cache
if (!options.cacheOutsideMemory?.member || !options.getItem) return;
const stored = await options.getItem('member', memberId, guildId);
if (stored) {
stored.lastInteractedTime = Date.now();
if (options.cacheInMemory?.member) bot.cache.members.set(stored);
}
return stored;
},
set: async (internalMember) => {
if (options.shouldCache?.member && !(await options.shouldCache.member(internalMember))) return;
internalMember.lastInteractedTime = Date.now();
const member = internalMember as unknown as Member;
// If user wants memory cache, we cache it
if (options.cacheInMemory?.member) {
if (options.cacheInMemory?.guild) {
if (member.guildId) {
const guild = bot.cache.guilds.memory.get(member.guildId);
if (guild) {
if (!guild.members) guild.members = new Collection();
guild.members.set(member.id, internalMember);
} else {
const pendingGuild = pendingGuildsData.get(member.guildId);
if (!pendingGuild) pendingGuildsData.set(member.guildId, { channels: new Collection(), members: new Collection(), roles: new Collection() });
pendingGuildsData.get(member.guildId)?.members!.set(member.id, internalMember);
}
} else console.warn(`[CACHE] Can't cache member(${member.id}) since guild.members is enabled but a guild id was not found.`);
}
}
// If user wants non-memory cache, we cache it
if (options.cacheOutsideMemory?.member && options.setItem) await options.setItem('member', internalMember);
},
delete: async (memberId, guildId) => {
// Remove from memory
bot.cache.guilds.memory.get(guildId)?.members?.delete(memberId);
// Remove from non-memory cache
if (options.removeItem) await options.removeItem('member', memberId, guildId);
},
};
bot.cache.channels = {
guildIds: new Collection(),
memory: new Collection(),
get: async (channelId) => {
// If available in memory, use it.
if (options.cacheInMemory?.channel) {
// If guilds are cached, channels will be inside them
if (options.cacheInMemory?.guild) {
const guildId = bot.cache.channels.guildIds.get(channelId);
if (guildId) {
const channel = bot.cache.guilds.memory.get(guildId)?.channels?.get(channelId);
if (channel) {
channel.lastInteractedTime = Date.now();
return channel;
}
} else {
// Return from cache.channels if this channel isn't in a guild
const channel = bot.cache.channels.memory.get(channelId);
if (channel) {
channel.lastInteractedTime = Date.now();
return channel;
}
}
} else {
const channel = bot.cache.channels.memory.get(channelId);
if (channel) {
channel.lastInteractedTime = Date.now();
return channel;
}
}
}
// Otherwise try to get from non-memory cache
if (!options.cacheOutsideMemory?.channel || !options.getItem) return;
const stored = await options.getItem('channel', channelId);
if (stored) {
stored.lastInteractedTime = Date.now();
if (options.cacheInMemory?.channel) bot.cache.channels.memory.set(channelId, stored);
}
return stored;
},
set: async (internalChannel) => {
if (options.shouldCache?.channel && !(await options.shouldCache.channel(internalChannel))) return;
internalChannel.lastInteractedTime = Date.now();
const channel = internalChannel as unknown as Channel;
// If user wants memory cache, we cache it
if (options.cacheInMemory?.channel) {
if (channel.guildId) bot.cache.channels.guildIds.set(channel.id, channel.guildId!);
if (options.cacheInMemory?.guild) {
const guildId = bot.cache.channels.guildIds.get(channel.id);
if (guildId) {
const guild = bot.cache.guilds.memory.get(guildId);
if (guild) {
if (!guild.channels) guild.channels = new Collection();
guild.channels.set(channel.id, internalChannel);
} else {
const pendingGuild = pendingGuildsData.get(guildId);
if (!pendingGuild) pendingGuildsData.set(guildId, { channels: new Collection(), members: new Collection(), roles: new Collection() });
pendingGuildsData.get(guildId)?.channels!.set(channel.id, internalChannel);
}
} else console.warn(`[CACHE] Can't cache channel(${channel.id}) since guild.channels is enabled but a guild id was not found.`);
} else bot.cache.channels.memory.set(channel.id, internalChannel);
}
// If user wants non-memory cache, we cache it
if (options.cacheOutsideMemory?.channel && options.setItem) await options.setItem('channel', internalChannel);
},
delete: async (channelId) => {
// Remove from memory
bot.cache.channels.memory.delete(channelId);
bot.cache.guilds.memory.get(bot.cache.channels.guildIds.get(channelId)!)?.channels?.delete(channelId);
bot.cache.channels.guildIds.delete(channelId);
// Remove from non-memory cache
if (options.removeItem) await options.removeItem('channel', channelId);
},
};
// MAKE SURE TO NOT MOVE THIS BELOW GUILD CUSTOMIZER
bot.transformers.customizers.member = (_, _payload, old) => {
if (!('id' in old)) return console.warn(`[CACHE] Can't cache member since id is missing.`);
// Filter to desired args
// @ts-ignore
const args: FilteredProxyCacheTypes<T, Props, Behavior>['member'] = {};
const keys = Object.keys(old) as (keyof typeof old)[];
for (const key of keys) {
// ID is required. Desired props take priority.
if (key === 'id' || options.desiredProps?.member?.includes(key)) (args as any)[key] = old[key];
// If undesired we skip
else if (options.undesiredProps?.member?.includes(key)) continue;
// If member did not say this is undesired and did not provide any desired props we accept it
else if (!options.desiredProps?.member?.length) (args as any)[key] = old[key];
}
// Add to memory
bot.cache.members.set(args);
return args;
};
bot.transformers.customizers.user = (_, _payload, old) => {
if (!('id' in old)) return console.warn(`[CACHE] Can't cache user since id is missing.`);
// Filter to desired args
// @ts-ignore
const args: FilteredProxyCacheTypes<T, Props, Behavior>['user'] = {};
const keys = Object.keys(old) as (keyof typeof old)[];
for (const key of keys) {
// ID is required. Desired props take priority.
if (key === 'id' || options.desiredProps?.user?.includes(key)) (args as any)[key] = old[key];
// If undesired we skip
else if (options.undesiredProps?.user?.includes(key)) continue;
// If user did not say this is undesired and did not provide any desired props we accept it
else if (!options.desiredProps?.user?.length) (args as any)[key] = old[key];
}
// Add to memory
bot.cache.users.set(args);
return args;
};
bot.transformers.customizers.guild = (_, payload, old) => {
if (!('id' in old)) return console.warn(`[CACHE] Can't cache guild since id is missing.`);
// Filter to desired args
// @ts-ignore
const args: FilteredProxyCacheTypes<T, Props, Behavior>['guild'] = {};
const keys = Object.keys(old) as (keyof typeof old)[];
for (const key of keys) {
// ID is required. Desired props take priority.
if (key === 'id' || options.desiredProps?.guild?.includes(key as any)) (args as any)[key] = old[key];
// If undesired we skip
else if (options.undesiredProps?.guild?.includes(key as any)) continue;
// If guild did not say this is undesired and did not provide any desired props we accept it
else if (!options.desiredProps?.guild?.length) (args as any)[key] = old[key];
}
const pendingGuildData = pendingGuildsData.get(old.id as Guild['id']);
if (pendingGuildData) {
pendingGuildsData.delete(old.id as Guild['id']);
if (pendingGuildData.channels?.size) args.channels = new Collection([...pendingGuildData.channels, ...(args.channels || [])]);
if (pendingGuildData.members?.size) args.members = new Collection([...pendingGuildData.members, ...(args.members || [])]);
if (pendingGuildData.roles?.size) args.roles = new Collection([...pendingGuildData.roles, ...(args.roles || [])]);
}
// Update last interacted time for all channels, members and roles
if (args.channels) args.channels = new Collection(args.channels.array().map((channel) => [(channel as unknown as Channel).id, { ...channel, lastInteractedTime: Date.now() }]));
if (args.members) args.members = new Collection(args.members.array().map((member) => [(member as unknown as Member).id, { ...member, lastInteractedTime: Date.now() }]));
if (args.roles) args.roles = new Collection(args.roles.array().map((role) => [(role as unknown as Role).id, { ...role, lastInteractedTime: Date.now() }]));
// Set approximate member count as member count if payload is from API
if (payload.approximate_member_count && (options.desiredProps?.guild as (keyof Guild)[])?.includes('memberCount')) (args as unknown as Guild).memberCount = payload.approximate_member_count;
// Add to memory
bot.cache.guilds.set(args);
if (payload.members) {
for (const member of payload.members) {
if (member.user) {
bot.transformers.member(bot, member, old.id as Guild['id'], BigInt(member.user.id));
bot.transformers.user(bot, member.user);
}
}
}
return args;
};
bot.transformers.customizers.channel = (_, _payload, old) => {
if (!('id' in old)) return console.warn(`[CACHE] Can't cache channel since id is missing.`);
// Filter to desired args
// @ts-ignore
const args: FilteredProxyCacheTypes<T, Props, Behavior>['channel'] = {};
const keys = Object.keys(old) as (keyof typeof old)[];
for (const key of keys) {
// ID is required. Desired props take priority.
if (key === 'id' || options.desiredProps?.channel?.includes(key)) (args as any)[key] = old[key];
// If undesired we skip
else if (options.undesiredProps?.channel?.includes(key)) continue;
// If channel did not say this is undesired and did not provide any desired props we accept it
else if (!options.desiredProps?.channel?.length) (args as any)[key] = old[key];
}
// Add to memory
bot.cache.channels.set(args);
return args;
};
bot.transformers.customizers.role = (_, _payload, old) => {
if (!('id' in old)) return console.warn(`[CACHE] Can't cache role since id is missing.`);
// Filter to desired args
// @ts-ignore
const args: FilteredProxyCacheTypes<T, Props, Behavior>['role'] = {};
const keys = Object.keys(old) as (keyof typeof old)[];
for (const key of keys) {
// ID is required. Desired props take priority.
if (key === 'id' || options.desiredProps?.role?.includes(key)) (args as any)[key] = old[key];
// If undesired we skip
else if (options.undesiredProps?.role?.includes(key)) continue;
// If role did not say this is undesired and did not provide any desired props we accept it
else if (!options.desiredProps?.role?.length) (args as any)[key] = old[key];
}
// Add to memory
bot.cache.roles.set(args);
return args;
};
setupCacheEdits(bot);
setupCacheRemovals(bot);
// Set dummy functions to events that aren't used, so customizers will still be run so we can cache data
setupDummyEvents(bot);
// Handle cache sweeping
if (options.sweeper) {
const { filter, interval } = options.sweeper;
setInterval(() => {
if (filter.channel)
bot.cache.channels.memory.forEach((channel) => {
if (filter.channel?.(channel)) bot.cache.channels.memory.delete((channel as unknown as Channel).id);
});
if (filter.guild || filter.channel || filter.member || filter.role)
bot.cache.guilds.memory.forEach((guild): boolean | void => {
if (filter.guild?.(guild)) return bot.cache.guilds.memory.delete((guild as unknown as Guild).id);
if (guild.channels && filter.channel)
guild.channels.forEach((channel) => {
if (filter.channel?.(channel)) guild.channels?.delete((channel as unknown as Channel).id);
});
if (guild.members && filter.member)
guild.members.forEach((member) => {
if (filter.member?.(member)) guild.members?.delete((member as unknown as Member).id);
});
if (guild.roles && filter.role)
guild.roles.forEach((role) => {
if (filter.role?.(role)) guild.roles?.delete((role as unknown as Role).id);
});
});
if (filter.user)
bot.cache.users.memory.forEach((user) => {
if (filter.user?.(user)) bot.cache.users.memory.delete((user as unknown as User).id);
});
}, interval);
}
return bot;
};
export type ProxyCacheTypes<Props extends TransformersDesiredProperties, Behavior extends DesiredPropertiesBehavior> = {
channel: SetupDesiredProps<Channel, Props, Behavior>;
guild: Omit<SetupDesiredProps<Guild, Props, Behavior>, 'channels' | 'members' | 'roles'> & {
channels?: Collection<bigint, LastInteractedTimeTrackedRecord<SetupDesiredProps<Channel, Props, Behavior>>>;
members?: Collection<bigint, LastInteractedTimeTrackedRecord<SetupDesiredProps<Member, Props, Behavior>>>;
roles?: Collection<bigint, LastInteractedTimeTrackedRecord<SetupDesiredProps<Role, Props, Behavior>>>;
};
member: SetupDesiredProps<Member, Props, Behavior>;
role: SetupDesiredProps<Role, Props, Behavior>;
user: SetupDesiredProps<User, Props, Behavior>;
};
// Note: Adding ProxyCacheTypes[K] because TS doesn't provide autocomplete on extends keyof generic, this trick will provide autocomplete
type DesiredPropsArray<T extends ProxyCacheTypes<Props, Behavior>, K extends keyof ProxyCacheTypes<Props, Behavior>, Props extends TransformersDesiredProperties, Behavior extends DesiredPropertiesBehavior> = Array<keyof T[K] | keyof ProxyCacheTypes<Props, Behavior>[K]>;
export interface CreateProxyCacheOptions<T extends ProxyCacheTypes<Props, Behavior>, Props extends TransformersDesiredProperties, Behavior extends DesiredPropertiesBehavior> {
/** Configure the exact properties you wish to have in each object. */
desiredProps?: {
/** The properties you want to keep in a channel object. */
channel?: DesiredPropsArray<T, 'channel', Props, Behavior>;
/** The properties you want to keep in a guild object. */
guild?: DesiredPropsArray<T, 'guild', Props, Behavior>;
/** The properties you want to keep in a member object. */
member?: DesiredPropsArray<T, 'member', Props, Behavior>;
/** The properties you want to keep in a role object. */
role?: DesiredPropsArray<T, 'role', Props, Behavior>;
/** The properties you want to keep in a user object. */
user?: DesiredPropsArray<T, 'user', Props, Behavior>;
};
/** Configure the properties you do NOT want in each object. */
undesiredProps?: {
/** The properties you do NOT want in a channel object. */
channel?: DesiredPropsArray<T, 'channel', Props, Behavior>;
/** The properties you do NOT want in a guild object. */
guild?: DesiredPropsArray<T, 'guild', Props, Behavior>;
/** The properties you do NOT want in a member object. */
member?: DesiredPropsArray<T, 'member', Props, Behavior>;
/** The properties you do NOT want in a role object. */
role?: DesiredPropsArray<T, 'role', Props, Behavior>;
/** The properties you do NOT want in a user object. */
user?: DesiredPropsArray<T, 'user', Props, Behavior>;
};
/**
* Options to choose how the proxy will cache everything.
*
* By default, all props inside `cacheInMemory` are set to `true`.
*/
cacheInMemory?: {
/** Whether or not to cache channels. If guilds is enabled, then these are cached inside the guild object. */
channel?: boolean;
/** Whether or not to cache guilds. */
guild?: boolean;
/** Whether or not to cache members. If guilds is enabled, then these are cached inside the guild object. */
member?: boolean;
/** Whether or not the cache roles. If guilds is enabled, then these are cached inside the guild object.*/
role?: boolean;
/** Whether or not to cache users. */
user?: boolean;
/** Default value for the properties that are not provided inside `cacheInMemory`. */
default: boolean;
};
/**
* Options to choose how the proxy will cache in a separate persitant cache.
*
* By default, all props inside `cacheOutsideMemory` are set to `false`.
*/
cacheOutsideMemory?: {
/** Whether or not to cache channels. */
channel?: boolean;
/** Whether or not to cache guilds. */
guild?: boolean;
/** Whether or not to cache members. */
member?: boolean;
/** Whether or not to cache roles. */
role?: boolean;
/** Whether or not to cache users. */
user?: boolean;
/** Default value for the properties that are not provided inside `cacheOutsideMemory`. */
default: boolean;
};
/** Handler to get an object from a specific table. */
getItem?: <K extends keyof T>(...args: [table: Exclude<K, 'member'>, id: bigint] | [table: Extract<K, 'member'>, id: bigint, guildId: bigint]) => Promise<FilteredProxyCacheTypes<T, Props, Behavior>[K]>;
/** Handler to set an object in a specific table. */
setItem?: <K extends keyof T>(table: K, item: FilteredProxyCacheTypes<T, Props, Behavior>[K]) => Promise<FilteredProxyCacheTypes<T, Props, Behavior>[K]>;
/** Handler to delete an object in a specific table. */
removeItem?: <K extends keyof T>(...args: [table: Exclude<K, 'member'>, id: bigint] | [table: Extract<K, 'member'>, id: bigint, guildId: bigint]) => Promise<unknown>;
/**
* Options for handling the removal of objects that may trigger bulk modifications or deletions of associated entities.
*
* This allows for performance optimization by consolidating multiple operations into a single action, rather than executing hundreds of queries.
*/
bulk?: {
/** Handler for the removal of guilds and their associated entities (e.g., channels, members and roles). */
removeGuild?: (id: bigint) => Promise<unknown>;
/** Handler for the removal of roles and their associated entities (e.g., members having this role). */
removeRole?: (id: bigint) => Promise<unknown>;
/**
* Options to choose whether or not to replace internal removers.
*
* By default, the proxy will handle the bulk modifications and deletions of associated entities from in-memory cache. You can override this behavior by setting this option to `true`.
*/
replaceInternalBulkRemover?: {
/** Whether or not to replace the internal guild remover. */
guild?: boolean;
/** Whether or not to replace the internal role remover. */
role?: boolean;
};
};
/** Configure the handlers that should be ran whenever something is about to be cached to determine whether it should or should not be cached. */
shouldCache?: {
/** Handler to check whether or not to cache this channel. */
channel?: (channel: FilteredProxyCacheTypes<T, Props, Behavior>['channel']) => Promise<boolean>;
/** Handler to check whether or not to cache this guild. */
guild?: (guild: FilteredProxyCacheTypes<T, Props, Behavior>['guild']) => Promise<boolean>;
/** Handler to check whether or not to cache this member. */
member?: (member: FilteredProxyCacheTypes<T, Props, Behavior>['member']) => Promise<boolean>;
/** Handler to check whether or not to cache this role. */
role?: (role: FilteredProxyCacheTypes<T, Props, Behavior>['role']) => Promise<boolean>;
/** Handler to check whether or not to cache this user. */
user?: (user: FilteredProxyCacheTypes<T, Props, Behavior>['user']) => Promise<boolean>;
};
/** Options for cache sweeper. This works for in-memory cache only. For outside memory cache, you should implement your own sweeper. */
sweeper?: {
/** The interval (in milliseconds) at which the cache sweeper should run the provided filter functions. */
interval: number;
/**
* Filters to decide which objects to remove from the cache. Defaults to removing nothing.
*
* Note: Make use of the `lastInteractedTime` property in the objects to implement an NRU (Not Recently Used) cache if you'd like.
*/
filter: {
/** Filter to decide whether or not to remove a channel from the cache. */
channel?: (channel: FilteredProxyCacheTypes<T, Props, Behavior>['channel']) => boolean;
/** Filter to decide whether or not to remove a guild from the cache. */
guild?: (guild: FilteredProxyCacheTypes<T, Props, Behavior>['guild']) => boolean;
/** Filter to decide whether or not to remove a member from the cache. */
member?: (member: FilteredProxyCacheTypes<T, Props, Behavior>['member']) => boolean;
/** Filter to decide whether or not to remove a role from the cache. */
role?: (role: FilteredProxyCacheTypes<T, Props, Behavior>['role']) => boolean;
/** Filter to decide whether or not to remove a user from the cache. */
user?: (user: FilteredProxyCacheTypes<T, Props, Behavior>['user']) => boolean;
};
};
}
type LastInteractedTimeTrackedRecord<T> = T & {
/** The UNIX timestamp representing the time this object was last accessed or modified. */
lastInteractedTime: number;
};