Search code examples
javascriptnode.jsdiscordxmlhttprequestfetch

How to listen for new messages in a channel from scratch in Discord


I wanna learn how to listen for new messages purely with requests to the Discord API. I already know how to load messages (I fetch https://discord.com/api/v9/channels/channelid/messages?limit=50), but I can't seem to figure out how to listen for new messages, and I am pretty sure that it's not in the Discord API documentation either.

Is there a way to do it?


Solution

  • You should be using a library to interface with the discord API.

    For NodeJS, the most common one is discord.js.

    If you really want to do this manually, listening to messages is done via a websocket connection (they call it a Gateway connection). The docs for this can be found here: https://discord.com/developers/docs/topics/gateway#gateways

    Even their own Docs say the following, though:

    The API for interacting with Gateways is complex and fairly unforgiving