import { Client , ClientOptions } from '@src/core' ; import { asyncSome , NonOptional } from '@src/utils' ; import { ChannelsCachingPolicy , EmojisCachingPolicy , GlobalCachingPolicy , GuildsCachingPolicy , GuildMembersCachingPolicy , MessagesCachingPolicy , OverwritesCachingPolicy , PermissionOverwriteTypes , PresencesCachingPolicy , RolesCachingPolicy , StickerFormatTypes , StickersCachingPolicy , StickerTypes , UsersCachingPolicy } from '@src/constants' ; import { ActivityEmoji , AnyEmoji , GuildEmoji , GuildMember , Message , Presence , ReactionEmoji , Role , Sticker , User } from '@src/api' ; import { PermissionOverwrite } from '@src/api/entities/overwrites/PermissionOverwrite' ; export class CachingPoliciesProcessor { public client : Client ; private options : NonOptional < ClientOptions , 'cache' > [ 'cache' ] ; constructor ( client ) { this . client = client ; this . options = this . client . options ?. cache ?? { } ; } async global ( entity ) { if ( this . options . global ) { const results = [ ] ; results . push ( await this . options . global . custom ?. ( entity ) ?? undefined ) ; results . push ( await asyncSome ( this . options . global . policies , policy => { switch ( policy ) { case GlobalCachingPolicy . NONE : return false ; case GlobalCachingPolicy . ALL : default : return true ; } } ) ) ; return results [ 0 ] ?? results [ 1 ] ; } } async channels ( channel ) { let result = true ; if ( this . options . channels ) { const results = [ ] ; results . push ( await this . options . channels . custom ?. ( channel ) ?? undefined ) ; results . push ( await asyncSome ( this . options . channels . policies , policy => { switch ( policy ) { case ChannelsCachingPolicy . CATEGORY : return channel . type === 'category' ; case ChannelsCachingPolicy . DM : return channel . type === 'dm' ; case ChannelsCachingPolicy . NEWS : return channel . type === 'news' ; case ChannelsCachingPolicy . NEWS_THREAD : return channel . type === 'newsThread' ; case ChannelsCachingPolicy . TEXT : return channel . type === 'text' ; case ChannelsCachingPolicy . STORE : return channel . type === 'store' ; case ChannelsCachingPolicy . VOICE : return channel . type === 'voice' ; case ChannelsCachingPolicy . STAGE_VOICE : return channel . type === 'stageVoice' ; case ChannelsCachingPolicy . PUBLIC_THREAD : return channel . type === 'publicThread' ; case ChannelsCachingPolicy . PRIVATE_THREAD : return channel . type === 'privateThread' ; case ChannelsCachingPolicy . NONE : return false ; case ChannelsCachingPolicy . ALL : default : return true ; } } ) ) ; result = results [ 0 ] ?? results [ 1 ] ; } return result ; } async emojis ( emoji ) { let result = true ; if ( this . options . emojis ) { const results = [ ] ; results . push ( await this . options . emojis . custom ?. ( emoji ) ?? undefined ) ; results . push ( await asyncSome ( this . options . emojis . policies , policy => { switch ( policy ) { case EmojisCachingPolicy . NONE : return false ; case EmojisCachingPolicy . GUILD : return emoji instanceof GuildEmoji ; case EmojisCachingPolicy . ACTIVITY : return emoji instanceof ActivityEmoji ; case EmojisCachingPolicy . REACTION : return emoji instanceof ReactionEmoji ; case EmojisCachingPolicy . STATIC : case EmojisCachingPolicy . ANIMATED : return ! ! emoji . animated ; case EmojisCachingPolicy . ALL : default : return true ; } } ) ) ; result = results [ 0 ] ?? results [ 1 ] ; } return result ; } async stickers ( sticker ) { let result = true ; if ( this . options . stickers ) { const results = [ ] ; results . push ( await this . options . stickers . custom ?. ( sticker ) ?? undefined ) ; results . push ( await asyncSome ( this . options . stickers . policies , policy => { switch ( policy ) { case StickersCachingPolicy . NONE : return false ; case StickersCachingPolicy . STANDARD : return sticker . type === StickerTypes . STANDARD ; case StickersCachingPolicy . GUILD : return sticker . type === StickerTypes . GUILD ; case StickersCachingPolicy . PNG : return sticker . formatType === StickerFormatTypes . PNG ; case StickersCachingPolicy . APNG : return sticker . formatType === StickerFormatTypes . APNG ; case StickersCachingPolicy . LOTTIE : return sticker . formatType === StickerFormatTypes . LOTTIE ; case StickersCachingPolicy . ALL : default : return true ; } } ) ) ; result = results [ 0 ] ?? results [ 1 ] ; } return result ; } async guilds ( guild ) { let result = true ; if ( this . options . guilds ) { const results = [ ] ; results . push ( await this . options . guilds . custom ?. ( guild ) ?? undefined ) ; results . push ( await asyncSome ( this . options . guilds . policies , policy => { switch ( policy ) { case GuildsCachingPolicy . NONE : return false ; case GuildsCachingPolicy . ALL : default : return true ; } } ) ) ; result = results [ 0 ] ?? results [ 1 ] ; } return result ; } async members ( member ) { let result = true ; if ( this . options . members ) { const results = [ ] , presence = await member . presence ( ) ; results . push ( await this . options . members . custom ?. ( member ) ?? undefined ) ; results . push ( await asyncSome ( this . options . members . policies , policy => { switch ( policy ) { case GuildMembersCachingPolicy . ONLINE : return presence ?. status === 'online' ; case GuildMembersCachingPolicy . DND : return presence ?. status === 'dnd' ; case GuildMembersCachingPolicy . IDLE : return presence ?. status === 'idle' ; case GuildMembersCachingPolicy . OFFLINE : return presence ?. status === 'offline' ; case GuildMembersCachingPolicy . OWNER : return member . guildOwner ; case GuildMembersCachingPolicy . PENDING : return ! ! member . pending ; case GuildMembersCachingPolicy . VOICE : return false ; case GuildMembersCachingPolicy . RECENT_MESSAGE : return false ; case GuildMembersCachingPolicy . NONE : return false ; case GuildMembersCachingPolicy . ALL : default : return true ; } } ) ) ; result = results [ 0 ] ?? results [ 1 ] ; } return result ; } async messages ( message ) { let result = true ; if ( this . options . messages ) { const results = [ ] ; results . push ( await this . options . messages . custom ?. ( message ) ?? undefined ) ; results . push ( await asyncSome ( this . options . messages . policies , async ( policy ) => { const author = await message . author ( ) ; switch ( policy ) { case MessagesCachingPolicy . BOTS : return ! ! author ?. bot ; case MessagesCachingPolicy . USERS : return ! author ?. bot ; case MessagesCachingPolicy . NONE : return false ; case MessagesCachingPolicy . ALL : default : return true ; } } ) ) ; result = results [ 0 ] ?? results [ 1 ] ; } return result ; } async presences ( presence ) { let result = true ; if ( this . options . presences ) { const results = [ ] ; results . push ( await this . options . presences . custom ?. ( presence ) ?? undefined ) ; results . push ( await asyncSome ( this . options . presences . policies , policy => { switch ( policy ) { case PresencesCachingPolicy . NONE : return false ; case PresencesCachingPolicy . ONLINE : return presence . status === 'online' ; case PresencesCachingPolicy . IDLE : return presence . status === 'idle' ; case PresencesCachingPolicy . DND : return presence . status === 'dnd' ; case PresencesCachingPolicy . OFFLINE : return presence . status === 'offline' ; case PresencesCachingPolicy . ALL : default : return true ; } } ) ) ; result = results [ 0 ] ?? results [ 1 ] ; } return result ; } async overwrites ( overwrite ) { let result = true ; if ( this . options . overwrites ) { const results = [ ] ; results . push ( await this . options . overwrites . custom ?. ( overwrite ) ?? undefined ) ; results . push ( await asyncSome ( this . options . overwrites . policies , policy => { switch ( policy ) { case OverwritesCachingPolicy . NONE : return false ; case OverwritesCachingPolicy . MEMBERS : return overwrite . type === PermissionOverwriteTypes . MEMBER ; case OverwritesCachingPolicy . ROLES : return overwrite . type === PermissionOverwriteTypes . ROLE ; case OverwritesCachingPolicy . ALL : default : return true ; } } ) ) ; result = results [ 0 ] ?? results [ 1 ] ; } return result ; } async roles ( role ) { let result = true ; if ( this . options . roles ) { const results = [ ] ; results . push ( await this . options . roles . custom ?. ( role ) ?? undefined ) ; results . push ( await asyncSome ( this . options . roles . policies , policy => { switch ( policy ) { case RolesCachingPolicy . EVERYONE : return role . id === role . guildId ; case RolesCachingPolicy . MANAGED : return role . managed ; case RolesCachingPolicy . NONE : return false ; case RolesCachingPolicy . ALL : default : return true ; } } ) ) ; result = results [ 0 ] ?? results [ 1 ] ; } return result ; } async users ( user ) { let result = true ; if ( this . options . users ) { const results = [ ] ; results . push ( await this . options . users . custom ?. ( user ) ?? undefined ) ; results . push ( await asyncSome ( this . options . users . policies , policy => { switch ( policy ) { case UsersCachingPolicy . NONE : return false ; case UsersCachingPolicy . ALL : default : return true ; } } ) ) ; result = results [ 0 ] ?? results [ 1 ] ; } return result ; } }