Cài Discord Bot cho OpenClaw

Mục tiêu

Cấu hình Discord Bot để chat với OpenClaw qua DM.

Kết quả sau khi cài:

Điều kiện trước khi làm

OpenClaw Gateway đã chạy bằng Docker/Portainer.

Đã cài đặt backend bất kỳ, có thể chat với OpenClaw thông qua web hoặc nền tảng khác.

Tóm tắt nhanh

Các bước chính:

  1. Tạo Discord Application 
  2. Tạo Bot và copy Bot Token
  3. Bật Message Content Intent + Server Members Intent 
  4. Cấu hình Installation → Guild Install → bot + applications.commands 
  5. Invite bot vào server riêng 
  6. Thêm DISCORD_BOT_TOKEN vào Portainer 
  7. Cài plugin @openclaw/discord 
  8. Bật channels.discord trong OpenClaw config 
  9. Restart OpenClaw Gateway 
  10. Pair Discord DM với OpenClaw 
  11. Chat với OpenClaw qua DM bot

Tạo Discord Application và Bot

Vào Discord Developer Portal > New Application > Đặt tên (Ex: OClaw) 

Ở menu bên trái chọn Bot > Reset Token > Copy Bot Token (lưu lại token này để dùng sau)

Bật Gateway Intents cho Bot

Vẫn trong tab Bot > Privileged Gateway Intents

Save Changes

Cấu hình Installation

Chuyển sang tab Installation

Installation Contexts

Default Install Settings

Khi chọn bot, Discord sẽ hiện phần quyền cho bot. Chọn tối thiểu:

Nếu muốn dùng thread/forum channel, bật thêm:

Sau đó copy Install Link, mở bằng trình duyệt và mời bot vào server riêng của bạn.

Bật Developer Mode và lấy ID


Trong Discord app > User Settings > Advanced > Developer Mode: ON

Lấy các ID cần thiết:

Các giá trị cần có:

Thêm Discord Bot Token vào Portainer stack

Vào Portainer > Stacks > openclaw > Editor

Trong phần Environment variables, thêm:

Trong compose, ở service openclaw-gateway, thêm vào phần environment:

Bấm Update the stack

Test Discord Bot Token

Chạy:

docker exec -it openclaw-gateway sh -lc '
node -e "
fetch(\"https://discord.com/api/v10/users/@me\", {
  headers: { Authorization: \"Bot \" + process.env.DISCORD_BOT_TOKEN }
})
.then(async r => {
  console.log(\"status\", r.status);
  console.log(await r.text());
})
.catch(e => {
  console.error(e);
  process.exit(1);
});
"
'

Kết quả mong muốn:

status 200
{"id":"...","username":"...","bot":true,...}

Nếu ra:

status 401

thì token sai hoặc đã bị reset.

Cài plugin Discord cho OpenClaw

Chỉ cấu hình channels.discord là chưa đủ. OpenClaw cần plugin Discord.

Kiểm tra trạng thái channel:

docker exec -it openclaw-gateway \
  node dist/index.js channels list --all | grep -i discord

Nếu thấy:

Discord: not installed, configured, disabled, run openclaw plugins install @openclaw/discord

thì cài plugin:

docker exec -it openclaw-gateway \
  node dist/index.js plugins install @openclaw/discord

Sau đó kiểm tra lại:

docker exec -it openclaw-gateway \
  node dist/index.js channels list --all | grep -i discord

Bật Discord channel trong OpenClaw config

Ghi config Discord:

docker exec -it openclaw-gateway \
  node dist/index.js config set --batch-json '[
    {
      "path": "channels.discord.enabled",
      "value": true
    },
    {
      "path": "channels.discord.token",
      "value": {
        "source": "env",
        "provider": "default",
        "id": "DISCORD_BOT_TOKEN"
      }
    },
    {
      "path": "channels.discord.groupPolicy",
      "value": "allowlist"
    }
  ]'

Kiểm tra config:

docker exec -it openclaw-gateway \
  node dist/index.js config get channels.discord --json

Kết quả mong muốn:

{
  "enabled": true,
  "token": {
    "source": "env",
    "provider": "default",
    "id": "DISCORD_BOT_TOKEN"
  },
  "groupPolicy": "allowlist"
}

Restart gateway:

docker restart openclaw-gateway

Bot trên Discord nên chuyển sang Online là thành công.

Pair Discord DM với OpenClaw

Mở DM với bot Discord, gửi hello hoặc pair > Bot sẽ trả về pairing code.

Trên Docker host, kiểm tra pairing request:

docker exec -it openclaw-gateway \
  node dist/index.js pairing list discord

Approve pairing code:

docker exec -it openclaw-gateway \
  node dist/index.js pairing approve discord <CODE>

Sau đó quay lại Discord DM và test:

Bạn đang hoạt động chưa? Trả lời ngắn gọn.

Nếu bot trả lời được, Discord DM đã hoạt động.

Phụ lục

Bot vẫn Offline

Kiểm tra:

docker exec -it openclaw-gateway \
  node dist/index.js channels list --all | grep -i discord

Nếu thấy not installed, cài plugin:

docker exec -it openclaw-gateway \
  node dist/index.js plugins install @openclaw/discord

Restart:

docker restart openclaw-gateway

Token rỗng trong container

Kiểm tra:

docker exec -it openclaw-gateway sh -lc '
if [ -n "$DISCORD_BOT_TOKEN" ]; then
  echo "OK"
else
  echo "EMPTY"
fi
'

Nếu ra EMPTY, kiểm tra lại Portainer Environment variables và compose environment:.

Token sai

Test:

docker exec -it openclaw-gateway sh -lc '
node -e "
fetch(\"https://discord.com/api/v10/users/@me\", {
  headers: { Authorization: \"Bot \" + process.env.DISCORD_BOT_TOKEN }
})
.then(async r => {
  console.log(\"status\", r.status);
  console.log(await r.text());
})
"
'

Nếu ra 401, reset token trong Discord Developer Portal rồi cập nhật lại Portainer.

Bot online nhưng không phản hồi DM

Kiểm tra pairing:

docker exec -it openclaw-gateway \
  node dist/index.js pairing list discord

Approve pairing code nếu có:

docker exec -it openclaw-gateway \
  node dist/index.js pairing approve discord <CODE>

Xem log Discord:

docker exec -it openclaw-gateway \
  node dist/index.js channels logs --channel discord --lines 200





Revision #1
Created 2026-06-22 04:01:35 UTC by ThanhDV
Updated 2026-06-22 15:30:33 UTC by ThanhDV