Skip to main content

What are listeners and how do they work?

Listeners are classes that contain logic that will be ran whenever a given event occurs.

So far we have mainly covered how to make your bot execute logic in response to messages (commands) send by a user, but sometimes you need to execute code when no command was ran but instead some event occurred. A typical example of the need of a listener is a welcoming message bot. For this you'll want to send a message such as hello @newmember whenever a member joins. You can't wait for a user to send a command for this, but instead you'll want to respond to the guildMemberAdd (GUILD_MEMBER_ADD in Discord API) event.

By default listeners will work for all discordjs events that are tied to the "Client" class. If you want your listeners to respond to events emitted by other event emitters, see Listening to events emitted by other emitters.