以討論串承接支援
當使用者在同一支援頻道發起多筆問題時,訊息常常互相交錯、難以追蹤。這個範例會為每一筆提及自動建立討論串,讓主頻道保持整潔。
問題背景
同一支援頻道內如果有多人同時詢問,訊息會快速交錯:重要問題可能被埋掉,使用者也不清楚自己的問題是否有被跟進。討論串讓每則工單具備獨立空間。
解法
建立一個監聽機器人:
- 當使用者在指定支援頻道提及機器人時,建立新討論串
- 在討論串內立即回覆「已收到」
- 後續回覆在同一討論串處理
- 可依需求整合 AI 做自動回應
完整可執行範例
#!/usr/bin/env python3"""討論串支援 — 自動為支援需求建立獨立串。"""
import jsonimport subprocessimport sys
SUPPORT_CHANNELS = {"support", "help", "questions"} # 要監聽的頻道名稱
def run_discli(*args): """執行不需 JSON 的 discli 指令並輸出錯誤。""" result = subprocess.run( ["discli", "--json", *args], capture_output=True, text=True, ) if result.returncode != 0: print(f"[error] discli {' '.join(args)}: {result.stderr.strip()}", file=sys.stderr) return None try: return json.loads(result.stdout) except json.JSONDecodeError: return None
def run_discli_plain(*args): """執行純文字輸出的 discli 指令。""" result = subprocess.run( ["discli", *args], capture_output=True, text=True, ) if result.returncode != 0: print(f"[error] discli {' '.join(args)}: {result.stderr.strip()}", file=sys.stderr)
# 追蹤已經建立過討論串的頻道active_threads = {} # thread_id -> {"author_id": str, "channel_id": str}
# 開始監聽proc = subprocess.Popen( ["discli", "--json", "listen", "--events", "messages"], stdout=subprocess.PIPE, text=True,)
print("討論串支援機器人已啟動,按 Ctrl+C 可停止。", file=sys.stderr)
for line in proc.stdout: line = line.strip() if not line: continue
try: event = json.loads(line) except json.JSONDecodeError: continue
channel_id = event.get("channel_id", "") channel_name = event.get("channel", "") author = event.get("author", "unknown") author_id = event.get("author_id", "") message_id = event.get("message_id", "") content = event.get("content", "")
# --- 新的支援請求:使用者在監控中的頻道提及機器人 --- if ( event.get("mentions_bot") and channel_name in SUPPORT_CHANNELS and channel_id not in active_threads ): # 從原訊息建立討論串 thread = run_discli( "thread", "create", channel_id, message_id, f"支援 - {author}", )
if thread and thread.get("id"): thread_id = thread["id"] active_threads[thread_id] = { "author_id": author_id, "channel_id": channel_id, }
# 發送歡迎訊息 run_discli( "thread", "send", thread_id, f"{author},這個討論串已建立。\n\n" f"**你的問題:** {content}\n\n" f"團隊成員會盡快回覆,請在本討論串補充更多細節。", )
print(f"[討論串] 已為 {author} 建立 '{thread.get('name')}'", file=sys.stderr) continue
# --- 討論串內追問 --- if channel_id in active_threads: # 避免機器人自己回圈 if event.get("is_bot"): continue
# 顯示 typing 代表有在處理 subprocess.Popen( ["discli", "typing", channel_id, "--duration", "3"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, )
# 可在此接 AI 回覆,這裡示範簡訊息 thread_info = active_threads[channel_id] run_discli( "thread", "send", channel_id, f"{author},收到你補充的細節,已通知團隊成員,將盡快回覆。", )
print(f"[追蹤回覆] {author} 在討論串 {channel_id}", file=sys.stderr)逐步解說
設定監看頻道
SUPPORT_CHANNELS 定義哪些頻道可以觸發討論串。只有這些頻道且提及機器人的訊息才會建立新串。
SUPPORT_CHANNELS = {"support", "help", "questions"}頻道有大量同名時,可改用頻道 ID 提高精準度,避免名稱重複誤判。
監聽訊息
腳本透過 discli listen 開啟長連線,所有訊息以 JSON 串流輸出。事件內含 channel、channel_id 與 mentions_bot。
discli --json listen --events messages建立討論串
當使用者在支援頻道提及機器人時,機器人會建立串接到原始訊息的討論串。討論串名稱會帶有提問人名。
discli --json thread create CHANNEL_ID MESSAGE_ID "支援 - Alice"指令回傳含新串的 id,腳本會保留此 ID 供追蹤。
{ "id": "1234567890123456789", "name": "支援 - Alice", "parent_channel": "support", "parent_channel_id": "9876543210987654321", "message_id": "1111111111111111111"}回覆確認訊息
機器人會立刻在討論串回一則歡迎訊息,讓使用者知道需求已被接收。
discli thread send THREAD_ID "Alice,你的需求已建立專用討論串,請在此補充細節..."處理追問
腳本以字典追蹤「活躍中」討論串 ID,收到該串內訊息時回覆中間狀態。你可在此接上 AI 或工單系統。
邊際案例與陷阱
CREATE_PUBLIC_THREADS 權限。 機器人需要目標頻道具備建立公開討論串權限,否則 thread create 會回傳 403。
討論串自動歸檔。 Discord 會在一段時間未活動後歸檔討論串(一般預設 1 小時,推升伺服器可達 24 小時)。歸檔後將無法繼續回覆。高頻需求可定期保活或延長歸檔時間。
討論串上限。 單一伺服器最多允許 1000 條活躍討論串,若請求量大,需建立關單與自動關閉機制。
多種頻道名稱。 若有多個伺服器存在相同頻道名稱,建議改用 channel_id,避免跨伺服器誤關聯。