Windows 特性
discli 在 Windows 可正常運作,但有少數平台行為差異。此頁面彙整這些差異與處理方式。
serve 模式中的 stdin
問題: Python asyncio 在 Windows 上無法像 Unix 一樣讀 stdin。discli 會用內部以執行緒實作的 stdin 讀取器處理。
平常使用下這對外部行為影響不大:你可直接將 JSONL 寫到 stdin,discli 就會處理。但有兩個邊界情況:
- Ctrl+C 可能無法順利結束。 讀取執行緒可能在鍵盤中斷後仍留著程序。
- 子程序清理 需要在代理端明確處理。
修正方式: 從外部控制 discli serve 時,使用 proc.terminate() 而不是依賴 Ctrl+C:
import subprocess, signal
proc = subprocess.Popen( ["discli", "serve", "--events", "messages"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, text=True,)
try: for line in proc.stdout: # process events... passexcept KeyboardInterrupt: proc.terminate() # Windows 用明確終止 proc.wait(timeout=5)Unix 環境中 Ctrl+C 會送 SIGINT,discli 可正常收斂;Windows 對子程序沒有 SIGINT,proc.terminate() 比較可靠。
路徑分隔符
問題: Windows 的路徑使用反斜線(\)。discli 內部會正規化路徑,但錯誤訊息或設定輸出可能仍看到反斜線。
修正方式: 優先使用正斜線:PowerShell 與 cmd 都可接受。
# 兩種寫法在 Windows 都可
discli message send "#logs" "report" --file C:/Users/you/report.csvdiscli message send "#logs" "report" --file C:\Users\you\report.csv設定檔位置為 %USERPROFILE%\\.discli\\config.json(或 ~/.discli/config.json,使用正斜線)。
PowerShell 與 cmd.exe 的引號行為
字串引號在 PowerShell 與 cmd.exe 差異不同。以下整理正確用法:
# 簡單字串可用單引號discli message send '#general' 'Hello from PowerShell!'
# 變數插值需雙引號$channel = "#general"discli message send $channel "Deploy complete at $(Get-Date)"
# 在雙引號字串中跳脫雙引號discli message send '#general' "He said `"hello`""
# JSON 輸出轉 JSON 物件
discli --json message list '#general' --limit 5 | ConvertFrom-Json:: cmd.exe 只能用雙引號discli message send "#general" "Hello from cmd!"
:: 內部雙引號用反斜線轉義discli message send "#general" "He said \"hello\""
:: JSON 輸出導向到檔案discli --json message list "#general" --limit 5 > messages.json# 標準 Unix 引號
discli message send '#general' 'Hello from bash!'discli message send "#general" "Hello from bash!"
# 接 jq
discli --json message list '#general' --limit 5 | jq '.[0].content'如果可以選擇,Git Bash 或 WSL 對 discli 的行為最一致。文件範例預設採 Unix shell 語法。
cmd.exe 的 Unicode 與 emoji
問題: cmd.exe 預設字碼頁(437)無法顯示 Unicode 與 emoji,可能看到亂碼或問號。
修正方式: 在執行前切換到 UTF-8:
chcp 65001discli message list "#general" --limit 5若要固定使用,可在登錄檔設定預設字碼頁,或使用 Windows Terminal(預設 UTF-8)。
PowerShell 與 Windows Terminal 預設可正確處理 UTF-8,只有經典 cmd.exe 受這個限制。
pip 無法於 PATH
問題: 使用 python.org 安裝後,pip 與 discli 可能不在 PATH,會出現 'pip' is not recognized 或 'discli' is not recognized。
修正方式:
:: 改用 python -m pippython -m pip install discord-cli-agent
:: 找到 scripts 安裝路徑python -m site --user-site
:: 或使用 py 啟動器(Python 安裝會帶)py -m pip install discord-cli-agent安裝後,discli 會放在 Python Scripts 目錄。請將該目錄加入 PATH:
# PowerShell:找出路徑python -c "import sysconfig; print(sysconfig.get_path('scripts'))"
# 永久加到 PATH(請以系統管理員執行)$scriptsPath = python -c "import sysconfig; print(sysconfig.get_path('scripts'))"[Environment]::SetEnvironmentVariable("Path", $env:Path + ";$scriptsPath", "User")長路徑支援
問題: Windows 預設路徑長度上限為 260 字元。若使用者名稱很長或 discli 設定資料夾層級過深,可能出現檔案存取錯誤。
修正方式: 啟用 Windows 10/11 長路徑支援:
開啟群組原則編輯器
按 Win+R,輸入 gpedit.msc,按 Enter。
導覽到設定
啟用長路徑
雙擊 Enable Win32 long paths,改為 Enabled,按 OK。
也可直接用登錄檔:
# PowerShell(需管理員)New-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Control\FileSystem" ` -Name "LongPathsEnabled" -Value 1 -PropertyType DWORD -Force實務上較少遇到。只有當 discli 設定檔完整路徑超過 260 字元時會發生,多半是使用者名稱非常長或安裝路徑非常深才會出現。