onebot-commander
Version:
OneBot12 Message Segment Commander - TypeScript version with dual ESM/CJS format support
159 lines (113 loc) • 4.17 kB
Markdown
[](https://www.npmjs.com/package/onebot-commander)
[](https://www.npmjs.com/package/onebot-commander)
[](https://github.com/your-username/onebot-commander/blob/main/LICENSE)
[](https://github.com/your-username/onebot-commander)
[](https://nodejs.org/)
OneBot12 消息段命令解析器 - TypeScript 版本,支持 ESM/CJS 双格式
- [📚 完整文档](https://your-username.github.io/onebot-commander/) - 详细的 API 文档和使用指南
- [🚀 快速开始](/docs/guide/) - 5分钟快速上手
- [💡 使用示例](/docs/examples/) - 丰富的代码示例
- [🔄 迁移指南](/docs/migration/) - 从其他库迁移
- [🤝 贡献指南](/docs/contributing/) - 参与项目开发
- 🎯 **精确匹配**: 支持复杂的消息段模式匹配
- ⚡ **高性能**: 基于优化的匹配算法,性能优异
- 🔧 **灵活配置**: 支持自定义类型化字面量字段映射
- 🛡️ **类型安全**: 完整的 TypeScript 支持
- 🔗 **链式调用**: 优雅的 API 设计
- 📦 **双格式**: 同时支持 ESM 和 CommonJS
- 🧪 **测试完善**: 90%+ 测试覆盖率
```bash
npm install onebot-commander
```
```typescript
import { Commander } from 'onebot-commander';
// 创建命令解析器(注意空格敏感)
const commander = new Commander('hello <name:text>'); // "hello " 后面的空格
// 添加处理逻辑
commander
.action((params) => {
console.log(`Hello, ${params.name}!`);
return params.name.toUpperCase();
})
.action((upperName) => {
console.log(`Uppercase: ${upperName}`);
});
// 匹配消息段
const segments = [
{ type: 'text', data: { text: 'hello Alice' } } // 注意 "hello " 后面的空格
];
const result = commander.match(segments);
// 输出: Hello, Alice!
// 输出: Uppercase: ALICE
```
OneBot Commander 对空格非常敏感,这是确保命令精确匹配的重要特性:
```typescript
// 模式: "ping [count:number={value:1}]"
const commander = new Commander('ping [count:number={value:1}]'); // "ping " 后面的空格
commander.action((params) => {
const count = params.count || { value: 1 };
return `Pong! (${count.value} times)`;
});
// ✅ 用户输入 "ping " - 匹配成功
const segments1 = [{ type: 'text', data: { text: 'ping ' } }];
const result1 = commander.match(segments1); // ['Pong! (1 times)']
// ❌ 用户输入 "ping" - 匹配失败
const segments2 = [{ type: 'text', data: { text: 'ping' } }];
const result2 = commander.match(segments2); // []
```
本项目包含完整的文档站,使用 VitePress 构建:
```bash
npm run docs:dev
npm run docs:build
npm run docs:preview
```
```bash
npm run docs:deploy:github
npm run docs:deploy:netlify
npm run docs:deploy:vercel
npm run docs:config
```
访问 http://localhost:5173 查看本地文档。
```bash
npm test
npm run test:coverage
npm run benchmark
```
```bash
npm run build
npm run clean
```
欢迎贡献!请查看 [贡献指南](/docs/contributing/) 了解详情。
MIT License - 查看 [LICENSE](LICENSE) 文件了解详情。
- [OneBot 官网](https://onebot.dev/)
- [OneBot12 规范](https://12.onebot.dev/)
- [GitHub 仓库](https://github.com/your-username/onebot-commander)
- [npm 包](https://www.npmjs.com/package/onebot-commander)
- [在线文档](https://your-username.github.io/onebot-commander/)