@@ -67,10 +67,6 @@ export interface RequiredWebSocketManagerOptions {
6767 * The REST instance to use for fetching gateway information
6868 */
6969 rest : REST ;
70- /**
71- * The token to use for identifying with the gateway
72- */
73- token : string ;
7470}
7571
7672/**
@@ -172,6 +168,12 @@ export interface OptionalWebSocketManagerOptions {
172168 * ```
173169 */
174170 shardIds : number [ ] | ShardRange | null ;
171+ /**
172+ * The token to use for identifying with the gateway
173+ *
174+ * If not provided, the token must be set using {@link WebSocketManager.setToken}
175+ */
176+ token : string ;
175177 /**
176178 * Function used to store session information for a given shard
177179 */
@@ -211,10 +213,12 @@ export interface ManagerShardEventsMap {
211213}
212214
213215export class WebSocketManager extends AsyncEventEmitter < ManagerShardEventsMap > implements AsyncDisposable {
216+ #token: string | null = null ;
217+
214218 /**
215219 * The options being used by this manager
216220 */
217- public readonly options : WebSocketManagerOptions ;
221+ public readonly options : Omit < WebSocketManagerOptions , 'token' > ;
218222
219223 /**
220224 * Internal cache for a GET /gateway/bot result
@@ -236,10 +240,26 @@ export class WebSocketManager extends AsyncEventEmitter<ManagerShardEventsMap> i
236240 */
237241 private readonly strategy : IShardingStrategy ;
238242
243+ /**
244+ * Gets the token set for this manager. If no token is set, an error is thrown.
245+ * To set the token, use {@link WebSocketManager.setToken} or pass it in the options.
246+ *
247+ * @remarks
248+ * This getter is mostly used to pass the token to the sharding strategy internally, there's not much reason to use it.
249+ */
250+ public get token ( ) : string {
251+ if ( ! this . #token) {
252+ throw new Error ( 'Token has not been set' ) ;
253+ }
254+
255+ return this . #token;
256+ }
257+
239258 public constructor ( options : CreateWebSocketManagerOptions ) {
240259 super ( ) ;
241260 this . options = { ...DefaultWebSocketManagerOptions , ...options } ;
242261 this . strategy = this . options . buildStrategy ( this ) ;
262+ this . #token = options . token ?? null ;
243263 }
244264
245265 /**
@@ -334,6 +354,14 @@ export class WebSocketManager extends AsyncEventEmitter<ManagerShardEventsMap> i
334354 await this . strategy . connect ( ) ;
335355 }
336356
357+ public setToken ( token : string ) : void {
358+ if ( this . #token) {
359+ throw new Error ( 'Token has already been set' ) ;
360+ }
361+
362+ this . #token = token ;
363+ }
364+
337365 public destroy ( options ?: Omit < WebSocketShardDestroyOptions , 'recover' > ) {
338366 return this . strategy . destroy ( options ) ;
339367 }
0 commit comments