# 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
- Navigate to the Discord developer portal.
- Login with your Discord account credentials.
- To create an application, click on the top-right "Create Application" button.
- Enter the name for your application and click on the "Create" button.
- 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.
- Click on the "Reset Token" and copy the regenerated bot secret token.
- 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.
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.
- Drag the DiscordMessageManager Node onto the canvas.
- 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.
- 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.
- Drag an Inject node onto the canvas.
- Set the
msg.payload
to the message you want to send and themsg.user
to the user ID of the user you want to send the message to. - Connect the Inject node's output to the input of the DiscordMessageManager node.
# 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
- Navigate to the Discord Developer Portal and click on your application, then click on the OAuth2 option from the sidebar.
- Select "bot" from the OAuth2 URL Generator's scope section and select the permissions you want to give to the bot in the server.
- At the bottom, you'll find the "Copy" button. Click on it to copy the OAuth2 URL.
- Paste that URL into the browser field and press Enter.
- Now, you'll see a Discord popup. Select the server to which you want to add the bot and click on the "Continue" button.
- 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.
# Sending messages to Discord server
- Drag the Inject node onto the canvas.
- Set the
msg.payload
to the message you want to send, and setmsg.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.
- Connect the Inject node's output to the input of the DiscordMessageManager node.
# Receiving messages from Discord
- Drag the DiscordMessage node onto the canvas.
- Double-click on it to ensure you have configured your bot correctly.
- 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.
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.
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.
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.