I’m currently building a Discord AI bot using n8n that listens to messages in my support channels and responds to user questions automatically.
What I Have Done So Far:
Set up a webhook in n8n to receive messages. Created an HTTP POST request to send a test message to my webhook, and it works fine. The AI agent processes the message once it is received.
The Issue:
I’m not sure how to make the webhook trigger automatically when a user sends a support question in my Discord channel. The bot is in the server, but messages aren’t triggering my n8n workflow when users send them. I need the webhook to activate only when a user posts in the support channel.
Any guidance or working examples would be greatly appreciated!
Hello, after extensive research, I realized that a few existing nodes weren’t working. I wrote a bot like the one below and made it work in my own project.
I’ve explained how I did it step by step. It works just fine:
Discord Bot with Node.js (discord.js)
Acquire the required qualifications:
npm install discord.js axios
Write the bot code:
const { Client, GatewayIntentBits } = require('discord.js');
const axios = require('axios');
// Configuration
const BOT_TOKEN = 'YOUR_DISCORD_BOT_TOKEN';
const N8N_WEBHOOK_URL = 'YOUR_N8N_WEBHOOK_URL';
const TARGET_CHANNEL_ID = ''; // Leave blank to listen to all channels
// const TARGET_CHANNEL_ID = '123456789012345678'; // Only for specific channel
// Create a Discord client
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent
]
});
// When the bot is ready
client.once('ready', () => {
console.log(`${client.user.tag} olarak giriş yapıldı!`);
});
// Message listener
client.on('messageCreate', async (message) => {
// Ignore the bot's own messages
if (message.author.bot) return;
// Listen to specific channel only (optional)
if (message.channel.id !== TARGET_CHANNEL_ID) return;
// Prepare message data
const messageData = {
content: message.content,
author: {
id: message.author.id,
username: message.author.username,
displayName: message.author.displayName || message.author.username
},
channel: {
id: message.channel.id,
name: message.channel.name
},
guild: message.guild ? {
id: message.guild.id,
name: message.guild.name
} : null,
timestamp: message.createdAt.toISOString(),
messageId: message.id,
attachments: message.attachments.map(att => ({
id: att.id,
filename: att.name,
url: att.url,
contentType: att.contentType
}))
};
// Send to n8n webhook
try {
const response = await axios.post(N8N_WEBHOOK_URL, messageData, {
headers: {
'Content-Type': 'application/json'
}
});
if (response.status === 200) {
console.log(`Mesaj n8n'e gönderildi: ${message.content.substring(0, 50)}...`);
}
} catch (error) {
console.error('Webhook hatası:', error.message);
}
});
// Error catching
client.on('error', console.error);
// Start the bot
client.login(BOT_TOKEN);
Discord Bot Installation Steps:
Create a Bot in the Discord Developer Portal:
Go to Discord Developer Portal
Click the “New Application” button
Switch to the “Bot” tab in the left menu
Click the “Add Bot” button
Copy the Token
Set Bot Permissions:
Go to “OAuth2” > “URL Generator”
Select Scopes: “bot”
Select Bot Permissions: “Read Messages”, “Read Message History”
Add the bot to your server using the generated URL
Create a Webhook in n8n:
Create a new workflow
Add the “Webhook” node
HTTP Method: POST
Click the “Listen for Test Event” button Click
Copy the URL
Run the Bot:
Update the token and webhook URL in the code.
Set the channel ID.
Run the bot.
This way, every message written on Discord will be sent to n8n, triggering your automation.