-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsetupCacheEdits.ts
36 lines (25 loc) · 1.47 KB
/
setupCacheEdits.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
import { Bot, DiscordGuildMemberAdd, DiscordGuildMemberRemove, Guild, type DesiredPropertiesBehavior, type TransformersDesiredProperties } from '@discordeno/bot';
import { BotWithProxyCache, ProxyCacheTypes } from './index.js';
export const setupCacheEdits = <T extends ProxyCacheTypes<Props, Behavior>, Props extends TransformersDesiredProperties, Behavior extends DesiredPropertiesBehavior, B extends Bot<Props, Behavior>>(bot: BotWithProxyCache<T, Props, Behavior, B>) => {
const { GUILD_MEMBER_ADD, GUILD_MEMBER_REMOVE } = bot.handlers;
bot.handlers.GUILD_MEMBER_ADD = async (_, data, shardId) => {
const payload = data.d as DiscordGuildMemberAdd;
const guildID = bot.transformers.snowflake(payload.guild_id);
const guild = bot.cache.guilds.memory.get(guildID);
if (guild) {
if ('memberCount' in guild) (guild.memberCount as Guild['memberCount'])++;
await bot.cache.guilds.set(guild);
}
GUILD_MEMBER_ADD(bot, data, shardId);
};
bot.handlers.GUILD_MEMBER_REMOVE = async (_, data, shardId) => {
const payload = data.d as DiscordGuildMemberRemove;
const guildID = bot.transformers.snowflake(payload.guild_id);
const guild = bot.cache.guilds.memory.get(guildID);
if (guild) {
if ('memberCount' in guild) (guild.memberCount as Guild['memberCount'])--;
await bot.cache.guilds.set(guild);
}
GUILD_MEMBER_REMOVE(bot, data, shardId);
};
};