File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -139,6 +139,28 @@ class BaseClient extends EventEmitter {
139139 this . _immediates . delete ( immediate ) ;
140140 }
141141
142+ /**
143+ * Increments max listeners by one, if they are not zero.
144+ * @private
145+ */
146+ incrementMaxListeners ( ) {
147+ const maxListeners = this . getMaxListeners ( ) ;
148+ if ( maxListeners !== 0 ) {
149+ this . setMaxListeners ( maxListeners + 1 ) ;
150+ }
151+ }
152+
153+ /**
154+ * Decrements max listeners by one, if they are not zero.
155+ * @private
156+ */
157+ decrementMaxListeners ( ) {
158+ const maxListeners = this . getMaxListeners ( ) ;
159+ if ( maxListeners !== 0 ) {
160+ this . setMaxListeners ( maxListeners - 1 ) ;
161+ }
162+ }
163+
142164 toJSON ( ...props ) {
143165 return Util . flatten ( this , { domain : false } , ...props ) ;
144166 }
Original file line number Diff line number Diff line change @@ -196,15 +196,18 @@ class GuildManager extends BaseManager {
196196
197197 const handleGuild = guild => {
198198 if ( guild . id === data . id ) {
199- this . client . removeListener ( Events . GUILD_CREATE , handleGuild ) ;
200199 this . client . clearTimeout ( timeout ) ;
200+ this . client . removeListener ( Events . GUILD_CREATE , handleGuild ) ;
201+ this . client . decrementMaxListeners ( ) ;
201202 resolve ( guild ) ;
202203 }
203204 } ;
205+ this . client . incrementMaxListeners ( ) ;
204206 this . client . on ( Events . GUILD_CREATE , handleGuild ) ;
205207
206208 const timeout = this . client . setTimeout ( ( ) => {
207209 this . client . removeListener ( Events . GUILD_CREATE , handleGuild ) ;
210+ this . client . decrementMaxListeners ( ) ;
208211 resolve ( this . client . guilds . add ( data ) ) ;
209212 } , 10000 ) ;
210213 return undefined ;
Original file line number Diff line number Diff line change @@ -267,17 +267,21 @@ class GuildMemberManager extends BaseManager {
267267 ( limit && fetchedMembers . size >= limit ) ||
268268 i === chunk . count
269269 ) {
270- this . guild . client . removeListener ( Events . GUILD_MEMBERS_CHUNK , handler ) ;
270+ this . client . clearTimeout ( timeout ) ;
271+ this . client . removeListener ( Events . GUILD_MEMBERS_CHUNK , handler ) ;
272+ this . client . decrementMaxListeners ( ) ;
271273 let fetched = option ? fetchedMembers : this . cache ;
272274 if ( user_ids && ! Array . isArray ( user_ids ) && fetched . size ) fetched = fetched . first ( ) ;
273275 resolve ( fetched ) ;
274276 }
275277 } ;
276- const timeout = this . guild . client . setTimeout ( ( ) => {
277- this . guild . client . removeListener ( Events . GUILD_MEMBERS_CHUNK , handler ) ;
278+ const timeout = this . client . setTimeout ( ( ) => {
279+ this . client . removeListener ( Events . GUILD_MEMBERS_CHUNK , handler ) ;
280+ this . client . decrementMaxListeners ( ) ;
278281 reject ( new Error ( 'GUILD_MEMBERS_TIMEOUT' ) ) ;
279282 } , time ) ;
280- this . guild . client . on ( Events . GUILD_MEMBERS_CHUNK , handler ) ;
283+ this . client . incrementMaxListeners ( ) ;
284+ this . client . on ( Events . GUILD_MEMBERS_CHUNK , handler ) ;
281285 } ) ;
282286 }
283287}
Original file line number Diff line number Diff line change @@ -42,7 +42,7 @@ class MessageCollector extends Collector {
4242 this . _handleChannelDeletion = this . _handleChannelDeletion . bind ( this ) ;
4343 this . _handleGuildDeletion = this . _handleGuildDeletion . bind ( this ) ;
4444
45- if ( this . client . getMaxListeners ( ) !== 0 ) this . client . setMaxListeners ( this . client . getMaxListeners ( ) + 1 ) ;
45+ this . client . incrementMaxListeners ( ) ;
4646 this . client . on ( Events . MESSAGE_CREATE , this . handleCollect ) ;
4747 this . client . on ( Events . MESSAGE_DELETE , this . handleDispose ) ;
4848 this . client . on ( Events . MESSAGE_BULK_DELETE , bulkDeleteListener ) ;
@@ -55,7 +55,7 @@ class MessageCollector extends Collector {
5555 this . client . removeListener ( Events . MESSAGE_BULK_DELETE , bulkDeleteListener ) ;
5656 this . client . removeListener ( Events . CHANNEL_DELETE , this . _handleChannelDeletion ) ;
5757 this . client . removeListener ( Events . GUILD_DELETE , this . _handleGuildDeletion ) ;
58- if ( this . client . getMaxListeners ( ) !== 0 ) this . client . setMaxListeners ( this . client . getMaxListeners ( ) - 1 ) ;
58+ this . client . decrementMaxListeners ( ) ;
5959 } ) ;
6060 }
6161
Original file line number Diff line number Diff line change @@ -49,7 +49,7 @@ class ReactionCollector extends Collector {
4949 this . _handleGuildDeletion = this . _handleGuildDeletion . bind ( this ) ;
5050 this . _handleMessageDeletion = this . _handleMessageDeletion . bind ( this ) ;
5151
52- if ( this . client . getMaxListeners ( ) !== 0 ) this . client . setMaxListeners ( this . client . getMaxListeners ( ) + 1 ) ;
52+ this . client . incrementMaxListeners ( ) ;
5353 this . client . on ( Events . MESSAGE_REACTION_ADD , this . handleCollect ) ;
5454 this . client . on ( Events . MESSAGE_REACTION_REMOVE , this . handleDispose ) ;
5555 this . client . on ( Events . MESSAGE_REACTION_REMOVE_ALL , this . empty ) ;
@@ -64,7 +64,7 @@ class ReactionCollector extends Collector {
6464 this . client . removeListener ( Events . MESSAGE_DELETE , this . _handleMessageDeletion ) ;
6565 this . client . removeListener ( Events . CHANNEL_DELETE , this . _handleChannelDeletion ) ;
6666 this . client . removeListener ( Events . GUILD_DELETE , this . _handleGuildDeletion ) ;
67- if ( this . client . getMaxListeners ( ) !== 0 ) this . client . setMaxListeners ( this . client . getMaxListeners ( ) - 1 ) ;
67+ this . client . decrementMaxListeners ( ) ;
6868 } ) ;
6969
7070 this . on ( 'collect' , ( reaction , user ) => {
Original file line number Diff line number Diff line change @@ -96,6 +96,8 @@ declare module 'discord.js' {
9696 private _immediates : Set < NodeJS . Immediate > ;
9797 private readonly api : object ;
9898 private rest : object ;
99+ private decrementMaxListeners ( ) : void ;
100+ private incrementMaxListeners ( ) : void ;
99101
100102 public options : ClientOptions ;
101103 public clearInterval ( interval : NodeJS . Timer ) : void ;
You can’t perform that action at this time.
0 commit comments