# Discord File Sender MCP Server

## 问题描述

当你对 AI 说"把这个文件发给我"时，AI 需要知道如何发送文件到 Discord。

## 解决方案

这个 MCP 服务器让 AI 能够发现并使用文件发送功能。

## 安装步骤

### 1. 注册 MCP 服务器

```bash
cd /Users/cyrus/Documents/project/agent-window-main
claude mcp add discord-file-sender -- node mcp-discord-file/index.js
```

### 2. 验证安装

```bash
claude mcp list
```

应该看到：
```
discord-file-sender: node mcp-discord-file/index.js
```

### 3. 重启 AI 会话

MCP 工具会在新会话中自动加载。

## 工作原理

```
用户说: "把这个图片发给我"
    ↓
AI 发现 MCP 工具: send_discord_file
    ↓
MCP 工具写入命令文件: .pending-file-commands/xxx.json
    ↓
Bot 每 2 秒检查该目录
    ↓
Bot 发现命令，用 Discord Gateway 发送文件
    ↓
✅ 文件出现在 Discord！
```

## 使用方法

在 Discord 中对 AI 说：
- "把 test-file-cli.txt 发给我"
- "send me the file test.png"
- "发送 Gemini_Generated_Image 到 Discord"

AI 会自动调用工具并在 2 秒内发送文件。

## 技术细节

### 为什么不用独立脚本发送文件？

**问题**: Node.js v23.7.0 与 undici 库有兼容性问题
```
Cannot read properties of null (reading 'byteLength')
```

**解决方案**: 使用正在运行的 bot 实例
- Bot 通过 Discord Gateway (WebSocket) 连接
- 避免了 HTTP 文件上传的 undici bug
- 更简单、更可靠

### 文件 IPC 机制

**命令格式** (.pending-file-commands/*.json):
```json
{
  "type": "get-file",
  "pattern": "filename.txt",
  "channelId": "1464085770074853588",
  "timestamp": 1769493600000
}
```

**Bot 处理流程**:
1. 每 2 秒检查 `.pending-file-commands/` 目录
2. 发现命令文件，解析 JSON
3. 在 uploads 目录搜索匹配的文件
4. 通过 Discord Gateway 发送文件
5. 删除已处理的命令文件

## 故障排除

### MCP 工具没有被加载

**检查**:
```bash
cd /Users/cyrus/Documents/project/agent-window-main
claude mcp list
```

**如果未列出**:
```bash
claude mcp add discord-file-sender -- node mcp-discord-file/index.js
```

### 文件没有发送

**检查 bot 日志**:
```bash
pm2 logs cyrus-macbook --lines 50
```

应该看到:
```
[FileCommand] Processing 1 pending file command(s)
[FileCommand] Sent file: xxx.txt to channel 1464085770074853588
```

**手动测试**:
```bash
echo '{"type":"get-file","pattern":"test.txt","channelId":"1464085770074853588","timestamp":'$(date +%s)'}' > .pending-file-commands/test.json
```

2 秒内文件应该出现在 Discord。

## 相关文件

- `mcp-discord-file/index.js` - MCP 服务器
- `src/bot.js` - 文件命令处理器（line 2119-2242）
- `.pending-file-commands/` - 命令文件目录
