Max Listeners Exceeded warning, help!

(node:560) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 message listeners added to [Client]. Use emitter.setMaxListeners() to increase limit. This is the warning I get when trying to run my discord bot. I followed this youtube tutorial. After adding new commands to my bot, I get this warning. I don’t know how to fix it. I am brand new to coding so I don’t know what the warning means. I can answer any questions that will help me solve this problem. Please help me!

We can’t really help you much unless we can see your actual code. Can you put it somewhere public (like github)? Have you tried googling “MaxListenersExceededWarning”. You should get back some links that explain what this means and possible fixes.

Here is the github link. I hope you can help me with it, thank you for responding.

Everyone of those calls to on creates a new listener. Apparently node has a pre-configured max number of listeners you can create (although this can be increased if needed). Fortunately, you don’t need to do that, you can combine all of these into just one listener.

All of your listeners are basically doing the same thing. They look at the string in msg.content and send a reply if there is a match. So you can have just one call to client.on and you can combine all of those if statements into if/else statements, or you can use a switch statement. For example:

client.on('message', msg => {
	if (msg.content === 'bruh') {
    else if (msg.content === 'coc') {
		msg.reply('shut up no one cares about coc anymore');
    else if (msg.content === 'lmao') {
		msg.reply('tf is u laughing at');

I think I would probably use a switch statement here though (I’ll leave it up to you to google how to do that).

Thank you so much! I will remember this for my future projects.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.