1- import LifetimeCollection from "../utils/Collection "
1+ import { LimitedCollection } from "discord.js "
22import { Bot } from "../structures/Bot"
3- import { CacheOptionsValidator } from "../utils"
43
54import type {
6- BotManagerOptions , FetchResponse , Nullable , RawBotInstance ,
5+ BotManagerOptions ,
6+ Nullable ,
77 FetchOptions
88} from "../utils/types"
99import type { Koreanbots } from "../client/Koreanbots"
10- import type { RequestInit } from "node-fetch"
1110
12- interface BotQuery {
13- bots ( botID : string ) : {
14- get ( options ?: RequestInit ) : Promise < FetchResponse < RawBotInstance > >
15- }
16- }
1711
1812const defaultCacheMaxSize = 100
19- const defaultCacheMaxAge = 60000 * 60
13+ const defaultCacheSweepInterval = 60000 * 60
14+ const defaultOptions = {
15+ cache : {
16+ maxSize : defaultCacheMaxSize ,
17+ sweepInterval : defaultCacheSweepInterval
18+ }
19+ }
2020
2121export class BotManager {
22- public cache : LifetimeCollection < string , Nullable < Bot > >
22+ public cache : LimitedCollection < string , Nullable < Bot > >
2323
2424 /**
2525 * 새로운 BotManager 인스턴스를 만듭니다.
@@ -33,23 +33,22 @@ export class BotManager {
3333 * })),
3434 * {
3535 * cache: {
36- * max : 150,
37- * maxAge : 60000
36+ * maxSize : 150,
37+ * sweepInterval : 60000
3838 * }
3939 * }
4040 * )
4141 * ```
4242 */
4343 constructor ( public readonly koreanbots : Koreanbots , public readonly options ?: BotManagerOptions ) {
44- this . options = options ?? { cache : { } }
45- const optionsProxy = new Proxy ( this . options , CacheOptionsValidator < BotManagerOptions > ( ) )
44+ this . options = options ?? defaultOptions
4645
47- optionsProxy . cache . max = options ? .cache ?. max ?? defaultCacheMaxSize
48- optionsProxy . cache . maxAge = options ? .cache ?. maxAge ?? defaultCacheMaxAge
46+ if ( ! this . options ?. cache . maxSize ) this . options . cache . maxSize = defaultCacheMaxSize
47+ if ( ! this . options ?. cache . sweepInterval ) this . options . cache . sweepInterval = defaultCacheSweepInterval
4948
50- this . cache = new LifetimeCollection ( {
51- max : this . options . cache . max ,
52- maxAge : this . options . cache . maxAge
49+ this . cache = new LimitedCollection ( {
50+ maxSize : this . options . cache . maxSize ,
51+ sweepInterval : this . options . cache . sweepInterval
5352 } )
5453 }
5554
@@ -78,12 +77,12 @@ export class BotManager {
7877 * ```
7978 */
8079 async fetch ( botID : string , options : FetchOptions = { cache : true , force : false } ) : Promise < Bot > {
81- if ( ! botID || typeof botID !== "string" ) throw new Error ( `"botID" 값은 주어지지 않았거나 문자열이어야 합니다. (받은 타입: ${ typeof botID } )` )
80+ if ( ! botID || typeof botID !== "string" ) throw new Error ( `"botID" 값이 주어지지 않았거나 문자열이어야 합니다. (받은 타입: ${ typeof botID } )` )
8281
8382 const cache = this . cache . get ( botID )
8483 if ( ! options ?. force && cache ) return cache
8584
86- const res = await this . koreanbots . api < BotQuery > ( ) . bots ( botID ) . get ( )
85+ const res = await this . koreanbots . api ( ) . bots ( botID ) . get ( )
8786 if ( res . code !== 200 || ! res . data ) throw new Error ( res . message || `API에서 알 수 없는 응답이 돌아왔습니다. ${ JSON . stringify ( res . data ) } ` )
8887
8988 const bot = new Bot ( this . koreanbots , res . data )
0 commit comments