Files
SaveAny-Bot/docs/content/zh/usage/config.md
krau c9bb6c9e3c feat(docs): add CLI subcommands documentation and file naming strategies
- Introduced new documentation for CLI subcommands: `upload` and `watch`, detailing their usage, flags, and examples.
- Added a section on file naming and conflict strategies, explaining how users can customize file names and handle duplicates via `/config` and `/fnametmpl` commands.
- Translated the new documentation into Chinese for broader accessibility.
2026-06-28 18:00:02 +08:00

75 lines
2.6 KiB
Markdown

---
title: "文件命名与重名策略"
weight: 11
---
# 文件命名与重名策略
SaveAny-Bot 支持在 Telegram 中通过 `/config``/fnametmpl` 命令自定义保存文件的命名方式, 以及处理与已存在文件重名时的冲突策略.
## `/config` — 用户配置
`/config` 命令会弹出一个内联菜单, 你可以在其中修改以下两项用户级设置:
- **文件名策略** — 保存文件的命名方式
- **重名文件保存策略** — 目标存储中已存在同名文件时的处理方式
设置按用户分别保存, 对该用户后续所有的保存/转存任务生效.
### 文件名策略
| 选项 | 行为 |
|---|---|
| `默认` | 使用媒体原始文件名, 没有原始文件名时使用生成名 |
| `优先从消息生成` | 优先根据消息内容 (如 caption、文本) 生成文件名, 而非原始文件名 |
| `自定义模板` | 使用 `/fnametmpl` 设置的自定义模板渲染文件名 |
### 重名文件保存策略
| 选项 | 行为 |
|---|---|
| `始终重命名` (默认) | 保留已有文件, 将新文件以另一个名字保存 |
| `每次询问` | 每次遇到重名时通过内联按钮提示你选择 |
| `始终覆盖` | 用新文件替换已有文件 |
| `始终跳过` | 对重名文件不做处理 |
{{< hint info >}}
重名策略仅在能够检测文件是否已存在的存储后端生效. 不支持检测文件是否存在的存储后端会退化为覆盖行为.
{{< /hint >}}
## `/fnametmpl` — 自定义文件名模板
当文件名策略设置为 `自定义模板` 时, SaveAny-Bot 会用 `/fnametmpl` 配置的模板来渲染所保存文件的文件名.
```
/fnametmpl [模板]
```
- 不带参数运行 `/fnametmpl` 会显示当前模板以及帮助说明
- 带模板字符串运行则会把它设为你的文件名模板
模板使用 Go [`text/template`](https://pkg.go.dev/text/template) 语法. 可用变量如下:
| 变量 | 说明 |
|---|---|
| `{{.msgid}}` | Telegram 消息 ID |
| `{{.msgtags}}` | 消息中的标签, 以 `_` 连接输出 |
| `{{.msggen}}` | 根据消息生成的文件名 |
| `{{.msgdate}}` | 消息日期, 格式 `YYYY-MM-DD_HH-MM-SS` |
| `{{.msgraw}}` | 消息的原始文本内容 (不做处理) |
| `{{.origname}}` | 媒体的原始文件名 (如有) |
| `{{.chatid}}` | 消息所在聊天的 ID |
示例:
```
# 固定前缀 + 消息 ID + 日期
/fnametmpl 图片_{{.msgid}}_{{.msgdate}}.jpg
# 优先使用原始文件名, 没有则用生成名
/fnametmpl {{.origname}}
```
{{< hint warning >}}
模板仅在文件名策略设置为 `自定义模板` 时生效. 如果模板解析失败, SaveAny-Bot 会回退到默认的文件名生成逻辑.
{{< /hint >}}