diff --git a/index.ts b/index.ts index f9f6709..106a08b 100644 --- a/index.ts +++ b/index.ts @@ -1,70 +1,73 @@ -import { Bot, Channel, Collection, Guild, Member, Role, User } from '@discordeno/bot'; +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 = LastInteractedTimeTrackedRecord, Undesired>>; +type FilterProps = LastInteractedTimeTrackedRecord, Undesired>>; -type FilteredProxyCacheTypes = CreateProxyCacheOptions> = { +type FilteredProxyCacheTypes, Props extends TransformersDesiredProperties, Behavior extends DesiredPropertiesBehavior, O extends CreateProxyCacheOptions = CreateProxyCacheOptions> = { [P in keyof T]: P extends 'guild' ? LastInteractedTimeTrackedRecord< Omit ? O['desiredProps'][P][number] : keyof T[P], O['undesiredProps'] extends Record ? O['undesiredProps'][P][number] : never>, 'channels' | 'members' | 'roles' | 'lastInteractedTime'> & { - channels?: Collection['channel']>; - members?: Collection['member']>; - roles?: Collection['role']>; + channels?: Collection['channel']>; + members?: Collection['member']>; + roles?: Collection['role']>; } > : FilterProps ? O['desiredProps'][P][number] : keyof T[P], O['undesiredProps'] extends Record ? O['undesiredProps'][P][number] : never>; }; -export interface ProxyCacheProps> { +export interface ProxyCacheProps, DiscordenoDesiredProps extends TransformersDesiredProperties, DiscordenoDesiredPropsBehavior extends DesiredPropertiesBehavior, O extends CreateProxyCacheOptions> { cache: { - options: CreateProxyCacheOptions; + options: CreateProxyCacheOptions; channels: { guildIds: Collection; - memory: Collection['channel']>; - get: (id: bigint) => Promise['channel'] | undefined>; - set: (value: FilteredProxyCacheTypes['channel']) => Promise; + memory: Collection['channel']>; + get: (id: bigint) => Promise['channel'] | undefined>; + set: (value: FilteredProxyCacheTypes['channel']) => Promise; delete: (id: bigint) => Promise; }; guilds: { - memory: Collection['guild']>; - get: (id: bigint) => Promise['guild'] | undefined>; - set: (value: FilteredProxyCacheTypes['guild']) => Promise; + memory: Collection['guild']>; + get: (id: bigint) => Promise['guild'] | undefined>; + set: (value: FilteredProxyCacheTypes['guild']) => Promise; delete: (id: bigint) => Promise; }; members: { - get: (id: bigint, guildId: bigint) => Promise['member'] | undefined>; - set: (value: FilteredProxyCacheTypes['member']) => Promise; + get: (id: bigint, guildId: bigint) => Promise['member'] | undefined>; + set: (value: FilteredProxyCacheTypes['member']) => Promise; delete: (id: bigint, guildId: bigint) => Promise; }; roles: { guildIds: Collection; - get: (id: bigint) => Promise['role'] | undefined>; - set: (value: FilteredProxyCacheTypes['role']) => Promise; + get: (id: bigint) => Promise['role'] | undefined>; + set: (value: FilteredProxyCacheTypes['role']) => Promise; delete: (id: bigint) => Promise; }; users: { - memory: Collection['user']>; - get: (id: bigint) => Promise['user'] | undefined>; - set: (value: FilteredProxyCacheTypes['user']) => Promise; + memory: Collection['user']>; + get: (id: bigint) => Promise['user'] | undefined>; + set: (value: FilteredProxyCacheTypes['user']) => Promise; delete: (id: bigint) => Promise; }; $inferredTypes: { - channel: FilteredProxyCacheTypes['channel']; - guild: FilteredProxyCacheTypes['guild']; - member: FilteredProxyCacheTypes['member']; - role: FilteredProxyCacheTypes['role']; - user: FilteredProxyCacheTypes['user']; + channel: FilteredProxyCacheTypes['channel']; + guild: FilteredProxyCacheTypes['guild']; + member: FilteredProxyCacheTypes['member']; + role: FilteredProxyCacheTypes['role']; + user: FilteredProxyCacheTypes['user']; }; }; } -export type BotWithProxyCache = CreateProxyCacheOptions> = B & ProxyCacheProps; +export type BotWithProxyCache, Props extends TransformersDesiredProperties, Behavior extends DesiredPropertiesBehavior, B extends Bot, O extends CreateProxyCacheOptions = CreateProxyCacheOptions> = B & ProxyCacheProps; -export const createProxyCache = = CreateProxyCacheOptions>(rawBot: B, options: O): BotWithProxyCache => { - const bot = rawBot as BotWithProxyCache; +export const createProxyCache = , T extends ProxyCacheTypes = ProxyCacheTypes, O extends CreateProxyCacheOptions = CreateProxyCacheOptions>( + rawBot: Bot & B, + options: O +): BotWithProxyCache => { + const bot = rawBot as BotWithProxyCache; // @ts-ignore bot.cache = { options }; @@ -72,9 +75,9 @@ export const createProxyCache = ['channel']>; - members?: Collection['member']>; - roles?: Collection['role']>; + channels?: Collection['channel']>; + members?: Collection['member']>; + roles?: Collection['role']>; } >(); @@ -109,9 +112,9 @@ export const createProxyCache = { - if (channel.guildId === id) { - bot.cache.channels.memory.delete(channel.id); - bot.cache.channels.guildIds.delete(channel.id); + 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); } }); }, @@ -125,7 +128,7 @@ export const createProxyCache = { - if (member.roles?.includes(id)) member.roles = member.roles.filter((roleId) => roleId !== id); + 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); }); } } @@ -193,7 +196,7 @@ export const createProxyCache = { - if (options.shouldCache?.role && !(await options.shouldCache.role(role))) return; + set: async (internalRole) => { + if (options.shouldCache?.role && !(await options.shouldCache.role(internalRole))) return; - role.lastInteractedTime = Date.now(); + internalRole.lastInteractedTime = Date.now(); + + const role = internalRole as unknown as Role; // If user wants memory cache, we cache it if (options.cacheInMemory?.role) { @@ -299,18 +304,18 @@ export const createProxyCache = { // Remove from memory @@ -350,10 +355,12 @@ export const createProxyCache = { - if (options.shouldCache?.member && !(await options.shouldCache.member(member))) return; + set: async (internalMember) => { + if (options.shouldCache?.member && !(await options.shouldCache.member(internalMember))) return; + + internalMember.lastInteractedTime = Date.now(); - member.lastInteractedTime = Date.now(); + const member = internalMember as unknown as Member; // If user wants memory cache, we cache it if (options.cacheInMemory?.member) { @@ -363,18 +370,18 @@ export const createProxyCache = { // Remove from memory @@ -433,14 +440,16 @@ export const createProxyCache = { - if (options.shouldCache?.channel && !(await options.shouldCache.channel(channel))) return; + set: async (internalChannel) => { + if (options.shouldCache?.channel && !(await options.shouldCache.channel(internalChannel))) return; - channel.lastInteractedTime = Date.now(); + 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 (channel.guildId) bot.cache.channels.guildIds.set(channel.id, channel.guildId!); if (options.cacheInMemory?.guild) { const guildId = bot.cache.channels.guildIds.get(channel.id); @@ -449,18 +458,18 @@ export const createProxyCache = { // Remove from memory @@ -475,19 +484,21 @@ export const createProxyCache = { + 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['member'] = {}; + const args: FilteredProxyCacheTypes['member'] = {}; - const keys = Object.keys(old) as (keyof 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[key] = old[key] as never; + 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[key] = old[key] as never; + else if (!options.desiredProps?.member?.length) (args as any)[key] = old[key]; } // Add to memory @@ -497,19 +508,21 @@ export const createProxyCache = { + 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['user'] = {}; + const args: FilteredProxyCacheTypes['user'] = {}; - const keys = Object.keys(old) as (keyof 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[key] = old[key] as never; + 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[key] = old[key] as never; + else if (!options.desiredProps?.user?.length) (args as any)[key] = old[key]; } // Add to memory @@ -519,25 +532,27 @@ export const createProxyCache = { + 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['guild'] = {}; + const args: FilteredProxyCacheTypes['guild'] = {}; - const keys = Object.keys(old) as (keyof 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)) args[key] = old[key] as never; + 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)) continue; + 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[key] = old[key] as never; + else if (!options.desiredProps?.guild?.length) (args as any)[key] = old[key]; } - const pendingGuildData = pendingGuildsData.get(old.id); + const pendingGuildData = pendingGuildsData.get(old.id as Guild['id']); if (pendingGuildData) { - pendingGuildsData.delete(old.id); + 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 || [])]); @@ -545,12 +560,12 @@ export const createProxyCache = [channel.id, { ...channel, lastInteractedTime: Date.now() }])); - if (args.members) args.members = new Collection(args.members.array().map((member) => [member.id, { ...member, lastInteractedTime: Date.now() }])); - if (args.roles) args.roles = new Collection(args.roles.array().map((role) => [role.id, { ...role, lastInteractedTime: Date.now() }])); + 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?.includes('memberCount')) args.memberCount = payload.approximate_member_count; + 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); @@ -558,7 +573,7 @@ export const createProxyCache = { + 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['channel'] = {}; + const args: FilteredProxyCacheTypes['channel'] = {}; - const keys = Object.keys(old) as (keyof 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[key] = old[key] as never; + 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[key] = old[key] as never; + else if (!options.desiredProps?.channel?.length) (args as any)[key] = old[key]; } // Add to memory @@ -590,19 +607,21 @@ export const createProxyCache = { + 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['role'] = {}; + const args: FilteredProxyCacheTypes['role'] = {}; - const keys = Object.keys(old) as (keyof 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[key] = old[key] as never; + 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[key] = old[key] as never; + else if (!options.desiredProps?.role?.length) (args as any)[key] = old[key]; } // Add to memory @@ -624,32 +643,32 @@ export const createProxyCache = { if (filter.channel) bot.cache.channels.memory.forEach((channel) => { - if (filter.channel?.(channel)) bot.cache.channels.memory.delete(channel.id); + 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.id); + 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.id); + 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.id); + 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.id); + 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.id); + if (filter.user?.(user)) bot.cache.users.memory.delete((user as unknown as User).id); }); }, interval); } @@ -657,47 +676,47 @@ export const createProxyCache = & { - channels?: Collection>; - members?: Collection>; - roles?: Collection>; +export type ProxyCacheTypes = { + channel: SetupDesiredProps; + guild: Omit, 'channels' | 'members' | 'roles'> & { + channels?: Collection>>; + members?: Collection>>; + roles?: Collection>>; }; - member: Member; - role: Role; - user: User; + member: SetupDesiredProps; + role: SetupDesiredProps; + user: SetupDesiredProps; }; // Note: Adding ProxyCacheTypes[K] because TS doesn't provide autocomplete on extends keyof generic, this trick will provide autocomplete -type DesiredPropsArray = Array; +type DesiredPropsArray, K extends keyof ProxyCacheTypes, Props extends TransformersDesiredProperties, Behavior extends DesiredPropertiesBehavior> = Array[K]>; -export interface CreateProxyCacheOptions { +export interface CreateProxyCacheOptions, 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; + channel?: DesiredPropsArray; /** The properties you want to keep in a guild object. */ - guild?: DesiredPropsArray; + guild?: DesiredPropsArray; /** The properties you want to keep in a member object. */ - member?: DesiredPropsArray; + member?: DesiredPropsArray; /** The properties you want to keep in a role object. */ - role?: DesiredPropsArray; + role?: DesiredPropsArray; /** The properties you want to keep in a user object. */ - user?: DesiredPropsArray; + user?: DesiredPropsArray; }; /** Configure the properties you do NOT want in each object. */ undesiredProps?: { /** The properties you do NOT want in a channel object. */ - channel?: DesiredPropsArray; + channel?: DesiredPropsArray; /** The properties you do NOT want in a guild object. */ - guild?: DesiredPropsArray; + guild?: DesiredPropsArray; /** The properties you do NOT want in a member object. */ - member?: DesiredPropsArray; + member?: DesiredPropsArray; /** The properties you do NOT want in a role object. */ - role?: DesiredPropsArray; + role?: DesiredPropsArray; /** The properties you do NOT want in a user object. */ - user?: DesiredPropsArray; + user?: DesiredPropsArray; }; /** * Options to choose how the proxy will cache everything. @@ -738,9 +757,9 @@ export interface CreateProxyCacheOptions { default: boolean; }; /** Handler to get an object from a specific table. */ - getItem?: (...args: [table: Exclude, id: bigint] | [table: Extract, id: bigint, guildId: bigint]) => Promise[K]>; + getItem?: (...args: [table: Exclude, id: bigint] | [table: Extract, id: bigint, guildId: bigint]) => Promise[K]>; /** Handler to set an object in a specific table. */ - setItem?: (table: K, item: FilteredProxyCacheTypes[K]) => Promise[K]>; + setItem?: (table: K, item: FilteredProxyCacheTypes[K]) => Promise[K]>; /** Handler to delete an object in a specific table. */ removeItem?: (...args: [table: Exclude, id: bigint] | [table: Extract, id: bigint, guildId: bigint]) => Promise; /** @@ -768,15 +787,15 @@ export interface CreateProxyCacheOptions { /** 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['channel']) => Promise; + channel?: (channel: FilteredProxyCacheTypes['channel']) => Promise; /** Handler to check whether or not to cache this guild. */ - guild?: (guild: FilteredProxyCacheTypes['guild']) => Promise; + guild?: (guild: FilteredProxyCacheTypes['guild']) => Promise; /** Handler to check whether or not to cache this member. */ - member?: (member: FilteredProxyCacheTypes['member']) => Promise; + member?: (member: FilteredProxyCacheTypes['member']) => Promise; /** Handler to check whether or not to cache this role. */ - role?: (role: FilteredProxyCacheTypes['role']) => Promise; + role?: (role: FilteredProxyCacheTypes['role']) => Promise; /** Handler to check whether or not to cache this user. */ - user?: (user: FilteredProxyCacheTypes['user']) => Promise; + user?: (user: FilteredProxyCacheTypes['user']) => Promise; }; /** Options for cache sweeper. This works for in-memory cache only. For outside memory cache, you should implement your own sweeper. */ sweeper?: { @@ -789,15 +808,15 @@ export interface CreateProxyCacheOptions { */ filter: { /** Filter to decide whether or not to remove a channel from the cache. */ - channel?: (channel: FilteredProxyCacheTypes['channel']) => boolean; + channel?: (channel: FilteredProxyCacheTypes['channel']) => boolean; /** Filter to decide whether or not to remove a guild from the cache. */ - guild?: (guild: FilteredProxyCacheTypes['guild']) => boolean; + guild?: (guild: FilteredProxyCacheTypes['guild']) => boolean; /** Filter to decide whether or not to remove a member from the cache. */ - member?: (member: FilteredProxyCacheTypes['member']) => boolean; + member?: (member: FilteredProxyCacheTypes['member']) => boolean; /** Filter to decide whether or not to remove a role from the cache. */ - role?: (role: FilteredProxyCacheTypes['role']) => boolean; + role?: (role: FilteredProxyCacheTypes['role']) => boolean; /** Filter to decide whether or not to remove a user from the cache. */ - user?: (user: FilteredProxyCacheTypes['user']) => boolean; + user?: (user: FilteredProxyCacheTypes['user']) => boolean; }; }; } diff --git a/setupCacheEdits.ts b/setupCacheEdits.ts index d3f7512..6cc857b 100644 --- a/setupCacheEdits.ts +++ b/setupCacheEdits.ts @@ -1,7 +1,7 @@ -import { Bot, DiscordGuildMemberAdd, DiscordGuildMemberRemove } from '@discordeno/bot'; +import { Bot, DiscordGuildMemberAdd, DiscordGuildMemberRemove, Guild, type DesiredPropertiesBehavior, type TransformersDesiredProperties } from '@discordeno/bot'; import { BotWithProxyCache, ProxyCacheTypes } from './index.js'; -export const setupCacheEdits = (bot: BotWithProxyCache) => { +export const setupCacheEdits = , Props extends TransformersDesiredProperties, Behavior extends DesiredPropertiesBehavior, B extends Bot>(bot: BotWithProxyCache) => { const { GUILD_MEMBER_ADD, GUILD_MEMBER_REMOVE } = bot.handlers; bot.handlers.GUILD_MEMBER_ADD = async (_, data, shardId) => { @@ -11,7 +11,7 @@ export const setupCacheEdits = (bot: B const guild = bot.cache.guilds.memory.get(guildID); if (guild) { - if (guild.memberCount) guild.memberCount++; + if ('memberCount' in guild) (guild.memberCount as Guild['memberCount'])++; await bot.cache.guilds.set(guild); } @@ -26,7 +26,7 @@ export const setupCacheEdits = (bot: B const guild = bot.cache.guilds.memory.get(guildID); if (guild) { - if (guild.memberCount) guild.memberCount--; + if ('memberCount' in guild) (guild.memberCount as Guild['memberCount'])--; await bot.cache.guilds.set(guild); } diff --git a/setupCacheRemovals.ts b/setupCacheRemovals.ts index 7ba663a..769b720 100644 --- a/setupCacheRemovals.ts +++ b/setupCacheRemovals.ts @@ -1,7 +1,7 @@ -import { Bot, DiscordChannel, DiscordGuildBanAddRemove, DiscordGuildMemberRemove, DiscordGuildRoleDelete, DiscordUnavailableGuild } from '@discordeno/bot'; +import { Bot, DiscordChannel, DiscordGuildBanAddRemove, DiscordGuildMemberRemove, DiscordGuildRoleDelete, DiscordUnavailableGuild, type DesiredPropertiesBehavior, type TransformersDesiredProperties } from '@discordeno/bot'; import { BotWithProxyCache, ProxyCacheTypes } from './index.js'; -export const setupCacheRemovals = (bot: BotWithProxyCache) => { +export const setupCacheRemovals = , Props extends TransformersDesiredProperties, Behavior extends DesiredPropertiesBehavior, B extends Bot>(bot: BotWithProxyCache) => { const { CHANNEL_DELETE, GUILD_BAN_ADD, GUILD_DELETE, GUILD_MEMBER_REMOVE, GUILD_ROLE_DELETE, THREAD_DELETE } = bot.handlers; bot.handlers.CHANNEL_DELETE = (_, data, shardId) => { diff --git a/setupDummyEvents.ts b/setupDummyEvents.ts index 8f11573..14b7ab3 100644 --- a/setupDummyEvents.ts +++ b/setupDummyEvents.ts @@ -1,10 +1,12 @@ -import { Bot, EventHandlers } from '@discordeno/bot'; +import { Bot, DesiredPropertiesBehavior, EventHandlers, TransformersDesiredProperties } from '@discordeno/bot'; import { BotWithProxyCache, ProxyCacheTypes } from './index.js'; const ignore = () => {}; -export const setupDummyEvents = (bot: BotWithProxyCache) => { - const eventsRequired = new Set(); +type EventHandlersKeys = keyof EventHandlers; + +export const setupDummyEvents = , Props extends TransformersDesiredProperties, Behavior extends DesiredPropertiesBehavior, B extends Bot>(bot: BotWithProxyCache) => { + const eventsRequired = new Set(); const { cacheInMemory, cacheOutsideMemory } = bot.cache.options || {}; // Guild @@ -40,14 +42,14 @@ export const setupDummyEvents = (bot: // Handlers: GUILD_CREATE, GUILD_UPDATE // Transformers: None -const setupDummyGuildEvents = (eventsRequired: Set) => { +const setupDummyGuildEvents = (eventsRequired: Set) => { eventsRequired.add('guildCreate'); eventsRequired.add('guildUpdate'); }; // Handlers: CHANNEL_CREATE, CHANNEL_DELETE, CHANNEL_UPDATE, THREAD_CREATE, THREAD_DELETE, THREAD_UPDATE, THREAD_LIST_SYNC // Transformers: Guild, Interaction, Message -const setupDummyChannelEvents = (eventsRequired: Set) => { +const setupDummyChannelEvents = (eventsRequired: Set) => { eventsRequired.add('channelCreate'); eventsRequired.add('channelDelete'); eventsRequired.add('channelUpdate'); @@ -63,7 +65,7 @@ const setupDummyChannelEvents = (eventsRequired: Set) => { // Handlers: GUILD_ROLE_CREATE, GUILD_ROLE_UPDATE // Transformers: Guild, Interaction -const setupDummyRoleEvents = (eventsRequired: Set) => { +const setupDummyRoleEvents = (eventsRequired: Set) => { eventsRequired.add('roleCreate'); eventsRequired.add('roleUpdate'); @@ -73,7 +75,7 @@ const setupDummyRoleEvents = (eventsRequired: Set) => { // Handlers: GUILD_MEMBER_ADD, GUILD_MEMBER_UPDATE, MESSAGE_REACTION_ADD, TYPING_START // Transformers: Guild, Interaction, Message, Stage Invite Instance (Invite) -const setupDummyMemberEvents = (eventsRequired: Set) => { +const setupDummyMemberEvents = (eventsRequired: Set) => { eventsRequired.add('guildMemberAdd'); eventsRequired.add('guildMemberUpdate'); eventsRequired.add('reactionAdd'); @@ -87,7 +89,7 @@ const setupDummyMemberEvents = (eventsRequired: Set) => { // Handlers: GUILD_BAN_ADD, GUILD_BAN_REMOVE, GUILD_MEMBER_ADD, GUILD_MEMBER_REMOVE, GUILD_MEMBER_UPDATE, MESSAGE_REACTION_ADD, READY, USER_UPDATE // Transformers: Application (Invite), Channel, Emoji, Integration, Interaction, Invite, Member, Message, Presence, Scheduled Event, Sticker Pack (REST Only), Team (Application (Invite)), Template (REST Only), Webhook (REST Only) -const setupDummyUserEvents = (eventsRequired: Set) => { +const setupDummyUserEvents = (eventsRequired: Set) => { eventsRequired.add('guildBanAdd'); eventsRequired.add('guildBanRemove'); eventsRequired.add('guildMemberAdd'); @@ -110,7 +112,7 @@ const setupDummyUserEvents = (eventsRequired: Set) => { // Handlers: GUILD_EMOJIS_UPDATE, MESSAGE_REACTION_ADD, MESSAGE_REACTION_REMOVE_EMOJI, MESSAGE_REACTION_REMOVE // Transformers: Guild, Message, Onboarding (REST Only), Poll (Message) -const setupDummyEmojiEvents = (eventsRequired: Set) => { +const setupDummyEmojiEvents = (eventsRequired: Set) => { eventsRequired.add('guildEmojisUpdate'); eventsRequired.add('reactionAdd'); eventsRequired.add('reactionRemoveEmoji'); @@ -122,26 +124,26 @@ const setupDummyEmojiEvents = (eventsRequired: Set) => { // Handlers: INTEGRATION_CREATE, INTEGRATION_UPDATE // Transformers: None -const setupDummyIntegrationEvents = (eventsRequired: Set) => { +const setupDummyIntegrationEvents = (eventsRequired: Set) => { eventsRequired.add('integrationCreate'); eventsRequired.add('integrationUpdate'); }; // Handlers: INTERACTION_CREATE // Transformers: None -const setupDummyInteractionEvents = (eventsRequired: Set) => { +const setupDummyInteractionEvents = (eventsRequired: Set) => { eventsRequired.add('interactionCreate'); }; // Handlers: INVITE_CREATE // Transformers: None -const setupDummyInviteEvents = (eventsRequired: Set) => { +const setupDummyInviteEvents = (eventsRequired: Set) => { eventsRequired.add('inviteCreate'); }; // Handlers: MESSAGE_CREATE, MESSAGE_UPDATE // Transformers: Interaction, Message (Self) -const setupDummyMessageEvents = (eventsRequired: Set) => { +const setupDummyMessageEvents = (eventsRequired: Set) => { eventsRequired.add('messageCreate'); eventsRequired.add('messageUpdate'); @@ -150,7 +152,7 @@ const setupDummyMessageEvents = (eventsRequired: Set) => { // Handlers: PRESENCE_UPDATE // Transformers: Guild -const setupDummyPresenceEvents = (eventsRequired: Set) => { +const setupDummyPresenceEvents = (eventsRequired: Set) => { eventsRequired.add('presenceUpdate'); setupDummyGuildEvents(eventsRequired); @@ -158,7 +160,7 @@ const setupDummyPresenceEvents = (eventsRequired: Set) => { // Handlers: GUILD_SCHEDULED_EVENT_CREATE, GUILD_SCHEDULED_EVENT_DELETE, GUILD_SCHEDULED_EVENT_UPDATE // Transformers: Invite -const setupDummyScheduledEventEvents = (eventsRequired: Set) => { +const setupDummyScheduledEventEvents = (eventsRequired: Set) => { eventsRequired.add('scheduledEventCreate'); eventsRequired.add('scheduledEventDelete'); eventsRequired.add('scheduledEventUpdate');