{
  "name": "AI to Discord - Send Messages via Webhook",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "discord-send",
        "responseMode": "responseNode",
        "options": {}
      },
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        -80,
        -144
      ],
      "id": "285c529c-de87-4b2a-8018-f33ead4bd4e9",
      "name": "Webhook - Discord Send",
      "webhookId": "discord-send"
    },
    {
      "parameters": {
        "jsCode": "const body = $input.first().json.body;\n\n// Store the original request data to pass through\nconst channelName = (body.channel || 'general').toLowerCase().trim();\nconst message = body.message || body.content || '';\nconst imageUrl = body.imageUrl || body.image || '';\nconst fileUrl = body.fileUrl || body.file || '';\nconst embed = body.embed || null;\n\n// Build the message content\nlet content = message;\nif (fileUrl && message) {\n  content = message + '\\n' + fileUrl;\n} else if (fileUrl && !message) {\n  content = fileUrl;\n}\n\nreturn {\n  json: {\n    channelName: channelName,\n    content: content,\n    imageUrl: imageUrl,\n    embed: embed\n  }\n};"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        144,
        -144
      ],
      "id": "1e62bc7c-907b-40b7-b098-6a43020809d7",
      "name": "Parse Request"
    },
    {
      "parameters": {
        "operation": "getAll",
        "guildId": {
          "__rl": true,
          "value": "1428914820652204075",
          "mode": "list",
          "cachedResultName": "Arcellite server",
          "cachedResultUrl": "https://discord.com/channels/1428914820652204075"
        },
        "returnAll": true,
        "options": {}
      },
      "type": "n8n-nodes-base.discord",
      "typeVersion": 2,
      "position": [
        368,
        -144
      ],
      "id": "a6e30499-bfd5-48a3-9929-9916ca0795f0",
      "name": "Get Server Channels",
      "webhookId": "1ce9b359-f94c-487e-9876-b79dae8810f2",
      "credentials": {
        "discordBotApi": {
          "id": "PvtdtvlzpxNxA1b8",
          "name": "Discord Bot account"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "// Get all channels from Discord API response\nconst channels = $input.all().map(item => item.json);\n\n// Get the requested channel name from the Parse Request node\nconst requestData = $('Parse Request').first().json;\nconst targetName = requestData.channelName;\n\n// Find the matching text channel (type 0 = text channel)\nconst matched = channels.find(ch => \n  ch.name && ch.name.toLowerCase() === targetName && ch.type === 0\n);\n\nif (!matched) {\n  // List available text channels for the error message\n  const available = channels\n    .filter(ch => ch.type === 0)\n    .map(ch => ch.name)\n    .join(', ');\n  throw new Error(`Channel \"${targetName}\" not found. Available text channels: ${available}`);\n}\n\nreturn {\n  json: {\n    channelId: matched.id,\n    channelName: matched.name,\n    content: requestData.content,\n    imageUrl: requestData.imageUrl,\n    embed: requestData.embed\n  }\n};"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        592,
        -144
      ],
      "id": "7e79d0ef-e84c-428a-abde-6f6d0d5015f3",
      "name": "Match Channel"
    },
    {
      "parameters": {
        "resource": "message",
        "guildId": {
          "__rl": true,
          "value": "1428914820652204075",
          "mode": "list",
          "cachedResultName": "Arcellite server",
          "cachedResultUrl": "https://discord.com/channels/1428914820652204075"
        },
        "channelId": {
          "__rl": true,
          "value": "={{ $json.channelId }}",
          "mode": "id"
        },
        "content": "={{ $json.content }}",
        "options": {}
      },
      "type": "n8n-nodes-base.discord",
      "typeVersion": 2,
      "position": [
        816,
        -144
      ],
      "id": "e3e292b2-c7ce-4a88-a622-dc7259fc144e",
      "name": "Send Discord Message",
      "webhookId": "1e8fa07b-517c-42d6-98a7-d40c9ee1d1b0",
      "credentials": {
        "discordBotApi": {
          "id": "PvtdtvlzpxNxA1b8",
          "name": "Discord Bot account"
        }
      }
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ JSON.stringify({ success: true, message: 'Message sent to Discord', channel: $('Match Channel').item.json.channelName, channelId: $('Match Channel').item.json.channelId, messageId: $json.id || 'sent' }) }}",
        "options": {
          "responseHeaders": {
            "entries": [
              {
                "name": "Access-Control-Allow-Origin",
                "value": "*"
              },
              {
                "name": "Content-Type",
                "value": "application/json"
              }
            ]
          }
        }
      },
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [
        1040,
        -144
      ],
      "id": "8d37a214-7a72-4531-8c5d-fe3e296d50c3",
      "name": "Respond to Webhook"
    },
    {
      "parameters": {
        "path": "discord-channels",
        "responseMode": "responseNode",
        "options": {}
      },
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        -80,
        -368
      ],
      "id": "5b4f4210-f545-4a2c-beba-3880c3869de5",
      "name": "Webhook - Get Channels",
      "webhookId": "discord-channels"
    },
    {
      "parameters": {
        "operation": "getAll",
        "guildId": {
          "__rl": true,
          "value": "1428914820652204075",
          "mode": "list",
          "cachedResultName": "Arcellite server",
          "cachedResultUrl": "https://discord.com/channels/1428914820652204075"
        },
        "returnAll": true,
        "options": {}
      },
      "type": "n8n-nodes-base.discord",
      "typeVersion": 2,
      "position": [
        144,
        -368
      ],
      "id": "0ec481c6-9816-4209-90a5-11bcfe7773a0",
      "name": "Fetch All Channels",
      "webhookId": "a2b3c4d5-3333-4444-5555-666677778888",
      "credentials": {
        "discordBotApi": {
          "id": "PvtdtvlzpxNxA1b8",
          "name": "Discord Bot account"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "const channels = $input.all().map(item => item.json);\n\n// Discord channel types\nconst typeNames = {\n  0: 'text',\n  2: 'voice',\n  4: 'category',\n  5: 'announcement',\n  13: 'stage',\n  15: 'forum'\n};\n\n// Get text channels grouped by category\nconst categories = channels.filter(ch => ch.type === 4);\nconst textChannels = channels.filter(ch => ch.type === 0);\nconst voiceChannels = channels.filter(ch => ch.type === 2);\n\n// Build organized channel list\nconst organized = categories\n  .sort((a, b) => (a.position || 0) - (b.position || 0))\n  .map(cat => {\n    const catTextChannels = textChannels\n      .filter(ch => ch.parent_id === cat.id)\n      .sort((a, b) => (a.position || 0) - (b.position || 0))\n      .map(ch => ({\n        id: ch.id,\n        name: ch.name,\n        type: 'text',\n        topic: ch.topic || ''\n      }));\n    \n    const catVoiceChannels = voiceChannels\n      .filter(ch => ch.parent_id === cat.id)\n      .sort((a, b) => (a.position || 0) - (b.position || 0))\n      .map(ch => ({\n        id: ch.id,\n        name: ch.name,\n        type: 'voice'\n      }));\n\n    return {\n      category: cat.name,\n      categoryId: cat.id,\n      textChannels: catTextChannels,\n      voiceChannels: catVoiceChannels\n    };\n  })\n  .filter(cat => cat.textChannels.length > 0 || cat.voiceChannels.length > 0);\n\n// Uncategorized channels\nconst uncategorizedText = textChannels\n  .filter(ch => !ch.parent_id)\n  .sort((a, b) => (a.position || 0) - (b.position || 0))\n  .map(ch => ({\n    id: ch.id,\n    name: ch.name,\n    type: 'text',\n    topic: ch.topic || ''\n  }));\n\n// Flat list of all text channels (for easy AI use)\nconst allTextChannels = textChannels\n  .sort((a, b) => (a.position || 0) - (b.position || 0))\n  .map(ch => ({\n    id: ch.id,\n    name: ch.name,\n    topic: ch.topic || '',\n    category: categories.find(cat => cat.id === ch.parent_id)?.name || 'Uncategorized'\n  }));\n\nreturn [\n  {\n    json: {\n      success: true,\n      server: 'Arcellite server',\n      serverId: '1428914820652204075',\n      totalTextChannels: textChannels.length,\n      totalVoiceChannels: voiceChannels.length,\n      channels: allTextChannels,\n      organized: organized,\n      uncategorized: uncategorizedText\n    }\n  }\n];"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        368,
        -368
      ],
      "id": "63b95528-8607-4c46-8d7f-c6ba87e207c3",
      "name": "Format Channel List"
    },
    {
      "parameters": {
        "respondWith": "allIncomingItems",
        "options": {
          "responseHeaders": {
            "entries": [
              {
                "name": "Access-Control-Allow-Origin",
                "value": "*"
              },
              {
                "name": "Content-Type",
                "value": "application/json"
              }
            ]
          }
        }
      },
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [
        592,
        -368
      ],
      "id": "f1563efd-9572-43b7-820e-36437a92b08c",
      "name": "Respond Channels"
    }
  ],
  "pinData": {},
  "connections": {
    "Webhook - Discord Send": {
      "main": [
        [
          {
            "node": "Parse Request",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Parse Request": {
      "main": [
        [
          {
            "node": "Get Server Channels",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Server Channels": {
      "main": [
        [
          {
            "node": "Match Channel",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Match Channel": {
      "main": [
        [
          {
            "node": "Send Discord Message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send Discord Message": {
      "main": [
        [
          {
            "node": "Respond to Webhook",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Webhook - Get Channels": {
      "main": [
        [
          {
            "node": "Fetch All Channels",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch All Channels": {
      "main": [
        [
          {
            "node": "Format Channel List",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Format Channel List": {
      "main": [
        [
          {
            "node": "Respond Channels",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": true,
  "settings": {
    "executionOrder": "v1",
    "availableInMCP": false
  },
  "versionId": "ed722a45-5678-4446-bc6b-89e837813ab1",
  "meta": {
    "templateCredsSetupCompleted": true,
    "instanceId": "de60ded3609ed675dd3435a79fde884038d2348f816754f1f978c92a10e2da20"
  },
  "id": "KHzrTJyG-EQt7HsT5NIdd",
  "tags": []
}