@@ -96,58 +96,60 @@ class ShardClientUtil {
9696 }
9797
9898 /**
99- * Fetches a client property value of each shard.
99+ * Fetches a client property value of each shard, or a given shard .
100100 * @param {string } prop Name of the client property to get, using periods for nesting
101- * @returns {Promise<Array<*>> }
101+ * @param {number } [shard] Shard to fetch property from, all if undefined
102+ * @returns {Promise<*>|Promise<Array<*>> }
102103 * @example
103104 * client.shard.fetchClientValues('guilds.cache.size')
104105 * .then(results => console.log(`${results.reduce((prev, val) => prev + val, 0)} total guilds`))
105106 * .catch(console.error);
106107 * @see {@link ShardingManager#fetchClientValues }
107108 */
108- fetchClientValues ( prop ) {
109+ fetchClientValues ( prop , shard ) {
109110 return new Promise ( ( resolve , reject ) => {
110111 const parent = this . parentPort || process ;
111112
112113 const listener = message => {
113- if ( ! message || message . _sFetchProp !== prop ) return ;
114+ if ( ! message || message . _sFetchProp !== prop || message . _sFetchPropShard !== shard ) return ;
114115 parent . removeListener ( 'message' , listener ) ;
115116 if ( ! message . _error ) resolve ( message . _result ) ;
116117 else reject ( Util . makeError ( message . _error ) ) ;
117118 } ;
118119 parent . on ( 'message' , listener ) ;
119120
120- this . send ( { _sFetchProp : prop } ) . catch ( err => {
121+ this . send ( { _sFetchProp : prop , _sFetchPropShard : shard } ) . catch ( err => {
121122 parent . removeListener ( 'message' , listener ) ;
122123 reject ( err ) ;
123124 } ) ;
124125 } ) ;
125126 }
126127
127128 /**
128- * Evaluates a script or function on all shards, in the context of the {@link Client}s.
129+ * Evaluates a script or function on all shards, or a given shard, in the context of the {@link Client}s.
129130 * @param {string|Function } script JavaScript to run on each shard
130- * @returns {Promise<Array<*>> } Results of the script execution
131+ * @param {number } [shard] Shard to run script on, all if undefined
132+ * @returns {Promise<*>|Promise<Array<*>> } Results of the script execution
131133 * @example
132134 * client.shard.broadcastEval('this.guilds.cache.size')
133135 * .then(results => console.log(`${results.reduce((prev, val) => prev + val, 0)} total guilds`))
134136 * .catch(console.error);
135137 * @see {@link ShardingManager#broadcastEval }
136138 */
137- broadcastEval ( script ) {
139+ broadcastEval ( script , shard ) {
138140 return new Promise ( ( resolve , reject ) => {
139141 const parent = this . parentPort || process ;
140142 script = typeof script === 'function' ? `(${ script } )(this)` : script ;
141143
142144 const listener = message => {
143- if ( ! message || message . _sEval !== script ) return ;
145+ if ( ! message || message . _sEval !== script || message . _sEvalShard !== shard ) return ;
144146 parent . removeListener ( 'message' , listener ) ;
145147 if ( ! message . _error ) resolve ( message . _result ) ;
146148 else reject ( Util . makeError ( message . _error ) ) ;
147149 } ;
148150 parent . on ( 'message' , listener ) ;
149151
150- this . send ( { _sEval : script } ) . catch ( err => {
152+ this . send ( { _sEval : script , _sEvalShard : shard } ) . catch ( err => {
151153 parent . removeListener ( 'message' , listener ) ;
152154 reject ( err ) ;
153155 } ) ;
@@ -224,6 +226,18 @@ class ShardClientUtil {
224226 }
225227 return this . _singleton ;
226228 }
229+
230+ /**
231+ * Get the shard ID for a given guild ID.
232+ * @param {Snowflake } guildID Snowflake guild ID to get shard ID for
233+ * @param {number } shardCount Number of shards
234+ * @returns {number }
235+ */
236+ static shardIDForGuildID ( guildID , shardCount ) {
237+ const shard = Number ( BigInt ( guildID ) >> 22n ) % shardCount ;
238+ if ( shard < 0 ) throw new Error ( 'SHARDING_SHARD_MISCALCULATION' , shard , guildID , shardCount ) ;
239+ return shard ;
240+ }
227241}
228242
229243module . exports = ShardClientUtil ;
0 commit comments