botrs 0.2.8

A Rust QQ Bot framework based on QQ Guild Bot API
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
# 机器人 API 参考

`BotApi` 是用于与 QQ 频道 REST API 交互的核心客户端。它提供了完整的 API 端点访问,支持消息发送、频道管理、成员操作等所有功能。

## 概述

```rust
pub struct BotApi {
    http_client: HttpClient,
}
```

`BotApi` 封装了所有 QQ 频道机器人 API 端点,提供类型安全的接口和自动的错误处理。所有方法都是异步的,支持高并发操作。

## 构造函数

### `new`

创建新的 BotApi 实例。

```rust
pub fn new() -> Self
```

#### 返回值

返回新的 `BotApi` 实例,使用默认的 HTTP 客户端配置。

#### 示例

```rust
use botrs::BotApi;

let api = BotApi::new();
```

### `with_http_client`

使用自定义 HTTP 客户端创建 BotApi 实例。

```rust
pub fn with_http_client(http_client: HttpClient) -> Self
```

#### 参数

- `http_client`: 自定义的 HTTP 客户端

#### 示例

```rust
use botrs::{BotApi, HttpClient};
use std::time::Duration;

let http_client = HttpClient::builder()
    .timeout(Duration::from_secs(30))
    .build()?;

let api = BotApi::with_http_client(http_client);
```

## 消息 API

### `post_message`

发送消息到指定频道(旧版 API)。

```rust
pub async fn post_message(
    &self,
    token: &Token,
    channel_id: &str,
    content: Option<&str>,
    embed: Option<&Embed>,
) -> Result<Message, BotError>
```

#### 参数

- `token`: 身份验证令牌
- `channel_id`: 目标频道 ID
- `content`: 消息文本内容(可选)
- `embed`: 嵌入内容(可选)

#### 返回值

返回发送成功的消息对象或错误。

#### 示例

```rust
let message = api.post_message(
    &token,
    "channel_123",
    Some("你好,世界!"),
    None
).await?;
```

### `post_message_with_params`

使用结构化参数发送消息(推荐)。

```rust
pub async fn post_message_with_params(
    &self,
    token: &Token,
    channel_id: &str,
    params: MessageParams,
) -> Result<Message, BotError>
```

#### 参数

- `token`: 身份验证令牌
- `channel_id`: 目标频道 ID
- `params`: 消息参数结构体

#### 示例

```rust
use botrs::{MessageParams, Embed};

// 发送文本消息
let params = MessageParams::new_text("Hello, World!");
let message = api.post_message_with_params(&token, "channel_123", params).await?;

// 发送嵌入消息
let embed = Embed::new()
    .title("标题")
    .description("描述")
    .color(0x3498db);
let params = MessageParams::new_embed(embed);
let message = api.post_message_with_params(&token, "channel_123", params).await?;

// 发送回复消息
let params = MessageParams::new_text("这是回复")
    .with_reply("original_message_id");
let message = api.post_message_with_params(&token, "channel_123", params).await?;
```

### `post_message_with_file`

发送带文件的消息。

```rust
pub async fn post_message_with_file(
    &self,
    token: &Token,
    channel_id: &str,
    filename: &str,
    file_data: &[u8],
    file_type: &str,
) -> Result<Message, BotError>
```

#### 参数

- `token`: 身份验证令牌
- `channel_id`: 目标频道 ID
- `filename`: 文件名
- `file_data`: 文件数据
- `file_type`: 文件类型(如 "image", "video", "audio")

#### 示例

```rust
let file_data = std::fs::read("image.png")?;
let message = api.post_message_with_file(
    &token,
    "channel_123",
    "image.png",
    &file_data,
    "image"
).await?;
```

### `get_message`

获取指定消息。

```rust
pub async fn get_message(
    &self,
    token: &Token,
    channel_id: &str,
    message_id: &str,
) -> Result<Message, BotError>
```

### `delete_message`

删除指定消息。

```rust
pub async fn delete_message(
    &self,
    token: &Token,
    channel_id: &str,
    message_id: &str,
    hidetip: Option<bool>,
) -> Result<(), BotError>
```

## 频道管理 API

### `get_guild`

获取频道信息。

```rust
pub async fn get_guild(
    &self,
    token: &Token,
    guild_id: &str,
) -> Result<Guild, BotError>
```

#### 示例

```rust
let guild = api.get_guild(&token, "guild_123").await?;
println!("频道名称: {}", guild.name.unwrap_or_default());
```

### `get_guild_channels`

获取频道的子频道列表。

```rust
pub async fn get_guild_channels(
    &self,
    token: &Token,
    guild_id: &str,
) -> Result<Vec<Channel>, BotError>
```

#### 示例

```rust
let channels = api.get_guild_channels(&token, "guild_123").await?;
for channel in channels {
    println!("子频道: {} ({})", 
             channel.name.unwrap_or_default(), 
             channel.id);
}
```

### `get_channel`

获取指定子频道信息。

```rust
pub async fn get_channel(
    &self,
    token: &Token,
    channel_id: &str,
) -> Result<Channel, BotError>
```

### `create_guild_channel`

创建新的子频道。

```rust
pub async fn create_guild_channel(
    &self,
    token: &Token,
    guild_id: &str,
    channel_data: &serde_json::Value,
) -> Result<Channel, BotError>
```

#### 示例

```rust
use serde_json::json;

let channel_data = json!({
    "name": "新子频道",
    "type": 0,  // 文本频道
    "sub_type": 0,  // 聊天频道
    "position": 1
});

let channel = api.create_guild_channel(&token, "guild_123", &channel_data).await?;
```

### `modify_guild_channel`

修改子频道信息。

```rust
pub async fn modify_guild_channel(
    &self,
    token: &Token,
    channel_id: &str,
    channel_data: &serde_json::Value,
) -> Result<Channel, BotError>
```

### `delete_guild_channel`

删除子频道。

```rust
pub async fn delete_guild_channel(
    &self,
    token: &Token,
    channel_id: &str,
) -> Result<(), BotError>
```

## 成员管理 API

### `get_guild_members`

获取频道成员列表。

```rust
pub async fn get_guild_members(
    &self,
    token: &Token,
    guild_id: &str,
    after: Option<&str>,
    limit: Option<u32>,
) -> Result<Vec<Member>, BotError>
```

#### 参数

- `after`: 分页参数,获取此 ID 之后的成员
- `limit`: 返回成员数量限制(最大 400)

#### 示例

```rust
// 获取前100个成员
let members = api.get_guild_members(&token, "guild_123", None, Some(100)).await?;

// 分页获取
let first_batch = api.get_guild_members(&token, "guild_123", None, Some(400)).await?;
if let Some(last_member) = first_batch.last() {
    let second_batch = api.get_guild_members(
        &token, 
        "guild_123", 
        Some(&last_member.user.id), 
        Some(400)
    ).await?;
}
```

### `get_guild_member`

获取指定成员信息。

```rust
pub async fn get_guild_member(
    &self,
    token: &Token,
    guild_id: &str,
    user_id: &str,
) -> Result<Member, BotError>
```

### `add_guild_member_role`

为成员添加身份组。

```rust
pub async fn add_guild_member_role(
    &self,
    token: &Token,
    guild_id: &str,
    user_id: &str,
    role_id: &str,
) -> Result<(), BotError>
```

### `remove_guild_member_role`

移除成员的身份组。

```rust
pub async fn remove_guild_member_role(
    &self,
    token: &Token,
    guild_id: &str,
    user_id: &str,
    role_id: &str,
) -> Result<(), BotError>
```

### `create_guild_member_mute`

禁言频道成员。

```rust
pub async fn create_guild_member_mute(
    &self,
    token: &Token,
    guild_id: &str,
    user_id: &str,
    mute_data: &serde_json::Value,
) -> Result<(), BotError>
```

#### 示例

```rust
use serde_json::json;

// 禁言10分钟
let mute_data = json!({
    "mute_end_timestamp": (chrono::Utc::now() + chrono::Duration::minutes(10)).timestamp().to_string(),
    "mute_seconds": 600
});

api.create_guild_member_mute(&token, "guild_123", "user_456", &mute_data).await?;
```

## 私信 API

### `create_direct_message_session`

创建私信会话。

```rust
pub async fn create_direct_message_session(
    &self,
    token: &Token,
    guild_id: &str,
    user_id: &str,
) -> Result<DirectMessageSession, BotError>
```

### `post_direct_message_with_params`

发送私信消息。

```rust
pub async fn post_direct_message_with_params(
    &self,
    token: &Token,
    guild_id: &str,
    channel_id: &str,
    params: MessageParams,
) -> Result<DirectMessage, BotError>
```

#### 示例

```rust
// 创建私信会话
let session = api.create_direct_message_session(&token, "guild_123", "user_456").await?;

// 发送私信
let params = MessageParams::new_text("这是一条私信");
let dm = api.post_direct_message_with_params(
    &token,
    "guild_123",
    &session.channel_id,
    params
).await?;
```

### `get_direct_messages`

获取私信历史。

```rust
pub async fn get_direct_messages(
    &self,
    token: &Token,
    guild_id: &str,
    channel_id: &str,
    limit: Option<u32>,
) -> Result<Vec<DirectMessage>, BotError>
```

## 群组消息 API

### `post_group_message_with_params`

发送群组消息。

```rust
pub async fn post_group_message_with_params(
    &self,
    token: &Token,
    group_id: &str,
    params: MessageParams,
) -> Result<GroupMessage, BotError>
```

### `post_c2c_message_with_params`

发送 C2C(用户对用户)消息。

```rust
pub async fn post_c2c_message_with_params(
    &self,
    token: &Token,
    user_id: &str,
    params: MessageParams,
) -> Result<C2CMessage, BotError>
```

## 公告 API

### `create_guild_announce`

创建频道公告。

```rust
pub async fn create_guild_announce(
    &self,
    token: &Token,
    guild_id: &str,
    announce_data: &serde_json::Value,
) -> Result<Announce, BotError>
```

#### 示例

```rust
use serde_json::json;

let announce_data = json!({
    "message": "重要通知:系统维护将在今晚进行",
    "channel_id": "channel_123"
});

let announce = api.create_guild_announce(&token, "guild_123", &announce_data).await?;
```

### `delete_guild_announce`

删除频道公告。

```rust
pub async fn delete_guild_announce(
    &self,
    token: &Token,
    guild_id: &str,
    announce_id: &str,
) -> Result<(), BotError>
```

## 表情回应 API

### `put_message_reaction`

为消息添加表情回应。

```rust
pub async fn put_message_reaction(
    &self,
    token: &Token,
    channel_id: &str,
    message_id: &str,
    emoji: &str,
) -> Result<(), BotError>
```

#### 示例

```rust
// 添加点赞表情
api.put_message_reaction(&token, "channel_123", "message_456", "👍").await?;
```

### `delete_message_reaction`

删除消息的表情回应。

```rust
pub async fn delete_message_reaction(
    &self,
    token: &Token,
    channel_id: &str,
    message_id: &str,
    emoji: &str,
) -> Result<(), BotError>
```

### `get_message_reaction_users`

获取对消息添加特定表情的用户列表。

```rust
pub async fn get_message_reaction_users(
    &self,
    token: &Token,
    channel_id: &str,
    message_id: &str,
    emoji: &str,
    cookie: Option<&str>,
    limit: Option<u32>,
) -> Result<ReactionUsers, BotError>
```

## 身份组 API

### `get_guild_roles`

获取频道身份组列表。

```rust
pub async fn get_guild_roles(
    &self,
    token: &Token,
    guild_id: &str,
) -> Result<GuildRoles, BotError>
```

### `create_guild_role`

创建频道身份组。

```rust
pub async fn create_guild_role(
    &self,
    token: &Token,
    guild_id: &str,
    role_data: &serde_json::Value,
) -> Result<GuildRole, BotError>
```

### `modify_guild_role`

修改频道身份组。

```rust
pub async fn modify_guild_role(
    &self,
    token: &Token,
    guild_id: &str,
    role_id: &str,
    role_data: &serde_json::Value,
) -> Result<GuildRole, BotError>
```

### `delete_guild_role`

删除频道身份组。

```rust
pub async fn delete_guild_role(
    &self,
    token: &Token,
    guild_id: &str,
    role_id: &str,
) -> Result<(), BotError>
```

## 音频 API

### `get_channel_audio_members`

获取音频频道成员列表。

```rust
pub async fn get_channel_audio_members(
    &self,
    token: &Token,
    channel_id: &str,
) -> Result<Vec<Member>, BotError>
```

### `audio_control`

控制音频播放。

```rust
pub async fn audio_control(
    &self,
    token: &Token,
    channel_id: &str,
    control_data: &serde_json::Value,
) -> Result<AudioControl, BotError>
```

## 日程 API

### `get_guild_schedules`

获取频道日程列表。

```rust
pub async fn get_guild_schedules(
    &self,
    token: &Token,
    guild_id: &str,
    since: Option<u64>,
) -> Result<Vec<Schedule>, BotError>
```

### `get_guild_schedule`

获取指定日程信息。

```rust
pub async fn get_guild_schedule(
    &self,
    token: &Token,
    guild_id: &str,
    schedule_id: &str,
) -> Result<Schedule, BotError>
```

### `create_guild_schedule`

创建频道日程。

```rust
pub async fn create_guild_schedule(
    &self,
    token: &Token,
    guild_id: &str,
    schedule_data: &serde_json::Value,
) -> Result<Schedule, BotError>
```

### `modify_guild_schedule`

修改频道日程。

```rust
pub async fn modify_guild_schedule(
    &self,
    token: &Token,
    guild_id: &str,
    schedule_id: &str,
    schedule_data: &serde_json::Value,
) -> Result<Schedule, BotError>
```

### `delete_guild_schedule`

删除频道日程。

```rust
pub async fn delete_guild_schedule(
    &self,
    token: &Token,
    guild_id: &str,
    schedule_id: &str,
) -> Result<(), BotError>
```

## 论坛 API

### `get_threads`

获取论坛帖子列表。

```rust
pub async fn get_threads(
    &self,
    token: &Token,
    channel_id: &str,
) -> Result<Vec<Thread>, BotError>
```

### `get_thread`

获取指定论坛帖子信息。

```rust
pub async fn get_thread(
    &self,
    token: &Token,
    channel_id: &str,
    thread_id: &str,
) -> Result<ThreadInfo, BotError>
```

### `create_thread`

创建论坛帖子。

```rust
pub async fn create_thread(
    &self,
    token: &Token,
    channel_id: &str,
    thread_data: &serde_json::Value,
) -> Result<OpenThread, BotError>
```

### `delete_thread`

删除论坛帖子。

```rust
pub async fn delete_thread(
    &self,
    token: &Token,
    channel_id: &str,
    thread_id: &str,
) -> Result<(), BotError>
```

## 权限 API

### `get_guild_api_permission`

获取频道 API 权限。

```rust
pub async fn get_guild_api_permission(
    &self,
    token: &Token,
    guild_id: &str,
) -> Result<ApiPermission, BotError>
```

### `post_guild_api_permission_demand`

申请频道 API 权限。

```rust
pub async fn post_guild_api_permission_demand(
    &self,
    token: &Token,
    guild_id: &str,
    permission_data: &serde_json::Value,
) -> Result<ApiPermissionDemand, BotError>
```

## 错误处理

所有 API 方法都返回 `Result<T, BotError>`,其中 `BotError` 包含详细的错误信息:

```rust
use botrs::BotError;

match api.get_guild(&token, "invalid_guild_id").await {
    Ok(guild) => println!("获取频道成功: {}", guild.id),
    Err(BotError::NotFound) => eprintln!("频道不存在"),
    Err(BotError::Forbidden) => eprintln!("权限不足"),
    Err(BotError::RateLimited(retry_after)) => {
        eprintln!("速率限制,{}秒后重试", retry_after);
    }
    Err(BotError::Authentication(_)) => eprintln!("身份验证失败"),
    Err(BotError::Network(_)) => eprintln!("网络连接错误"),
    Err(e) => eprintln!("其他错误: {}", e),
}
```

## 批量操作示例

### 批量获取频道信息

```rust
use futures::future::try_join_all;

async fn get_multiple_guilds(
    api: &BotApi,
    token: &Token,
    guild_ids: &[String]
) -> Result<Vec<Guild>, BotError> {
    let futures: Vec<_> = guild_ids.iter()
        .map(|id| api.get_guild(token, id))
        .collect();
    
    try_join_all(futures).await
}
```

### 批量发送消息

```rust
async fn broadcast_message(
    api: &BotApi,
    token: &Token,
    channel_ids: &[String],
    content: &str
) -> Result<Vec<Message>, BotError> {
    let mut results = Vec::new();
    
    for channel_id in channel_ids {
        let params = MessageParams::new_text(content);
        match api.post_message_with_params(token, channel_id, params).await {
            Ok(message) => results.push(message),
            Err(e) => eprintln!("发送到频道 {} 失败: {}", channel_id, e),
        }
        
        // 避免速率限制
        tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
    }
    
    Ok(results)
}
```

## 最佳实践

1. **错误处理**: 始终处理可能的 API 错误
2. **速率限制**: 在批量操作中添加适当的延迟
3. **重试机制**: 对临时错误实现自动重试
4. **参数验证**: 在调用 API 前验证输入参数
5. **日志记录**: 记录重要的 API 调用和错误

## 另请参阅

- [`Token` API 参考]./token.md - 身份验证令牌管理
- [`Context` API 参考]./context.md - 事件处理器中的 API 使用
- [API 客户端使用指南]/zh/guide/api-client.md - API 使用最佳实践
- [错误处理指南]/zh/guide/error-handling.md - API 错误处理策略