Sending and receiving Discord messages with Node-RED

Sending notifications is crucial for IoT applications, especially for critical alerts, and many different services are used, but Discord is a standout among them. In recent months, we've written many articles on integrating other services with Node-RED, including popular notification platforms like Email and Telegram. In this guide, we will delve into sending and receiving messages from Node-RED to users and channels on Discord, along with troubleshooting tips for resolving common errors that may occur.

Prerequsite

Before proceeding further, make sure you have installed the following node:

Creating Bot in Discord

  1. Navigate to the Discord developer portal.
  2. Login with your Discord account credentials.
  3. To create an application, click on the top-right "Create Application" button.

"Screenshot showing the 'create application' button"

  1. Enter the name for your application and click on the "Create" button.

"Screenshot showing the 'create' button"

  1. After the successful creation of the application, you'll be redirected to that application's setting. Click on the "Bot" option from the left sidebar.

"Screenshot showing the 'bot' sidebar option and 'reset token' button"

  1. Click on the "Reset Token" and copy the regenerated bot secret token.
  2. Next, you'll need to enable Privileged Gateway Intents for your bot. To do so, navigate to the bot page and enable three intents as seen below.

"Screenshot showing the 'Privileged Gateway Intents' options"

Note:- If your application is verified or if your bot is in more than 100 servers, you'll need to apply for Privileged Gateway Intents to use them.

Adding configuration for the discord nodes

Before proceeding further, make sure you have added the environment variable for your bot's secret token. Using environment variables prevents the configuration token from being exposed in the application flow. For more information refer to the guide on Using environment variable.

  1. Drag the DiscordMessageManager Node onto the canvas.
  2. Double-click on it and then click on the pencil icon next to "Token" input field, and add the environment variable added for the bot token into the Token input field.

Sending a message to User

To send a message to a user, you will need the ID of that user. Before copying the ID, ensure that you have enabled "Developer Mode" in Discord, which starts displaying IDs for users, channels, and messages. Navigate to the Discord app, go to "Settings" -> "Advanced," and enable the "Developer Mode" option.

"Screenshot showing the 'Developer mode' option"

  1. In the Discord app, click on the user profile you want to send a message to, and click on "Copy User ID" to copy the ID.

"Screenshot showing 'copy user id' option"

  1. Drag an Inject node onto the canvas.
  2. Set the msg.payload to the message you want to send and the msg.user to the user ID of the user you want to send the message to.
  3. Connect the Inject node's output to the input of the DiscordMessageManager node.

"Sending messages to Discord users"

Sending messages to the Discord server

To send a message to the Discord server, you have to make sure that your bot is a member of that server with appropriate permissions.

Adding the bot to the Discord server

  1. Navigate to the Discord Developer Portal and click on your application, then click on the OAuth2 option from the sidebar.
  2. Select "bot" from the OAuth2 URL Generator's scope section and select the permissions you want to give to the bot in the server.
  3. At the bottom, you'll find the "Copy" button. Click on it to copy the OAuth2 URL.
  4. Paste that URL into the browser field and press Enter.
  5. Now, you'll see a Discord popup. Select the server to which you want to add the bot and click on the "Continue" button.

"Screenshot showing discord popup to add bot into the server"

  1. Next, confirm that you want to give the permissions you selected while generating the OAuth2 URL to the bot on the server and click "Authorize" to add it to the server along with those permissions.

"Screenshot showing conformation discord popup asking to conform the permission should be given to bot into server"

Sending messages to Discord server

  1. Drag the Inject node onto the canvas.
  2. Set the msg.payload to the message you want to send, and set msg.channel (you can also set it in the node) to the Channel ID to which you want to send the message. To grab the Channel ID, go to the Discord app, right-click on the server in the sidebar where the channel is located that you want to send the message to, click on the channel, and then click on "Copy Channel ID.

"Screenshot showing 'copy channel id' option"

  1. Connect the Inject node's output to the input of the DiscordMessageManager node.

"Sending message to Discord server's channel"

Receiving messages from Discord

  1. Drag the DiscordMessage node onto the canvas.
  2. Double-click on it to ensure you have configured your bot correctly.
  3. Drag the Debug node onto the canvas, double-click on it, and select the output to "Complete message object".

After deploying the flow, you will start receiving messages sent to your bot. In the debug panel in the sidebar, you will see the message object printed for each message, which contains different objects. Each object shows different details; for example, the author object contains details about the sender, and the channel object includes information of the channel if the message was sent in a channel.

"Receiving messages from users and server channels"

Below, I have provided the complete flow we built throughout the guide. Make sure to replace the environment variable 'BOT_TOKEN' with your actual bot token.

Debugging and Troubleshooting

Below are common errors that can occur while integrating Discord with Node-RED, along with troubleshooting tips.

"User disallowed intent"

If your Discord nodes show a status similar to the image above, you might still need to enable the Privileged Gateway Intents option. If you have already enabled it but are still encountering this issue, it could be due to your app bot is verified. A verified app bot is required to apply for the privileged gateway intents. For more information, refer to Discord support article.

"DiscordAPIError:Unknwon channel"

DiscordAPIError:Unknwon user

If you are getting errors similar to the images above, it's likely because the channelId or userId is invalid. Double-check and correct these identifiers to resolve the errors.