iT邦幫忙

2025 iThome 鐵人賽

DAY 11
0
生成式 AI

30 天效率革命:用 n8n + AI 打造專屬助手系列 第 11

Day 11 : Discord Trigger:監聽訊息事件

  • 分享至 

  • xImage
  •  

本文將介紹如何在 n8n 中安裝與設定社群節點 Discord Trigger,並建立一個能監聽 Discord 訊息事件的 Workflow。完成後,當指定伺服器或頻道有新訊息時,Bot 就能自動觸發流程。


安裝 Discord Trigger 社群節點

  1. 登入 n8n 控制面板
  2. 點擊 Settings → Community nodes → Install
  3. 搜尋並安裝 n8n-nodes-discord-trigger
  4. 安裝完成後重新整理頁面,在建立 Workflow 時即可找到 Discord Trigger 節點
    https://ithelp.ithome.com.tw/upload/images/20250925/201489794KWtrqIeID.png

步驟 2|設定 Discord Bot 權限

  1. 調整 Bot 設定
    • 關閉 PUBLIC BOT
    • 關閉 REQUIRES OAUTH2 CODE GRANT
  2. 開啟 Privileged Gateway Intents
    • Presence Intent
    • Server Members Intent
    • Message Content Intent

https://ithelp.ithome.com.tw/upload/images/20250925/201489793pdjlZgVBG.png

  1. 設定 OAuth2 Redirects

    • n8n 提供的 Redirect URL 複製到 Discord Developer Portal → OAuth2 → Redirects (在上一篇有設定過了)
  2. Scopes 與 Permissions

    • 勾選 botapplications.commands
    • bot權限選 Administrator
      https://ithelp.ithome.com.tw/upload/images/20250925/20148979HSNtOigqMv.png
  3. 邀請 Bot 加入伺服器

    • 複製生成的邀請網址,在瀏覽器開啟後選擇要加入的伺服器
      https://ithelp.ithome.com.tw/upload/images/20250925/20148979QCvxpYYaoA.png

在 n8n 新增 Credentials

在 n8n 建立一組 Discord Trigger 使用的憑證,需填寫以下資訊:

  • Client ID:於 Discord Developer Portal → OAuth2 頁面取得
  • Client Secret:同上
  • Bot Token:於 Discord Developer Bot 頁面複製
  • n8n API Key:在 n8n Settings 中建立,建議無過期時間
  • Base URL:n8n 伺服器對外網址,例如 https://your-domain/api/v1

https://ithelp.ithome.com.tw/upload/images/20250925/20148979wm9yZbq5Qa.png

https://ithelp.ithome.com.tw/upload/images/20250925/20148979RLem2Oxm3U.png


建立並測試 Discord Trigger Workflow

  1. 建立新 Workflow

  2. 新增 Discord Trigger 節點

  3. 設定參數:

    • Trigger Type:如 Message
    • Server IDs:指定伺服器
    • Channels:指定要監聽的頻道
    • Roles(選用):限制特定角色觸發
    • Pattern:監聽訊息模式(如關鍵字或訊息開頭)
  4. 儲存後重新啟動 n8n

  5. 在 Discord 發送測試訊息,確認 Workflow 是否被觸發

https://ithelp.ithome.com.tw/upload/images/20250925/20148979JSinhssFJO.png


處理 Discord 傳入的字串

假設收到訊息中包含網址,可用 Python Function Node 抽取:

import re
for item in _input.all():
    pattern = r'https?://[a-zA-Z0-9./?&=%_\-#]+'
    item.json.feed.entry.id = re.findall(pattern, item.json.feed.entry.id)[0]
return {'url': item.json.feed.entry.id}

從arxiv 回傳的內容提取出論文名稱

import re

# Loop over input items and add a new field called 'myNewField' to the JSON of each one
for item in _input.all():
    pattern = r'<title>(.*?)</title>'
    matches = re.findall(pattern, item.json.data, re.DOTALL)
    if matches:
        item.json.data = matches[0].strip()
    else:
        item.json.data = None

return {'titel': item.json.data}

https://ithelp.ithome.com.tw/upload/images/20250925/20148979MUlT4z9v0e.png

完成
https://ithelp.ithome.com.tw/upload/images/20250925/20148979yaTVsV0OQ1.png

參考資料


上一篇
Day 10 : Discord + n8n :發送訊息到指定頻道
下一篇
Day 12 : 用 n8n 串接 Discord 與 Gemini:自動化上傳論文筆記到 Notion
系列文
30 天效率革命:用 n8n + AI 打造專屬助手12
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言