onebot_v11 0.1.5

OneBot v11 with NapCat/llonebot extension
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
use serde::de::Error;
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Notice {
    /// 群文件上传事件
    GroupFileUpload(GroupFileUploadEvent),
    /// 群管理员变动事件
    GroupAdminChange(GroupAdminChangeEvent),
    /// 群成员减少事件
    GroupMemberDecrease(GroupMemberDecreaseEvent),
    /// 群成员增加事件
    GroupMemberIncrease(GroupMemberIncreaseEvent),
    /// 群禁言事件
    GroupBan(GroupBanEvent),
    /// 好友添加事件
    FriendAdd(FriendAddEvent),
    /// 群消息撤回事件
    GroupMessageRecall(GroupMessageRecallEvent),
    /// 好友消息撤回事件
    FriendMessageRecall(FriendMessageRecallEvent),
    /// 群内戳一戳事件
    GroupPoke(GroupPokeEvent),
    /// 群红包运气王事件
    GroupLuckyKing(GroupLuckyKingEvent),
    /// 群成员荣誉变更事件
    GroupMemberHonorChange(GroupMemberHonorChangeEvent),

    // 仅NapCat / llOneBot 支持的事件
    /// 私聊输入状态事件
    FriendInputStatusChange(FriendInputStatusChangeEvent),
    /// 群精华消息事件
    GroupEssenceMessageChange(GroupEssenceMessageChangeEvent),
    /// 群名片变更事件
    GroupCardChange(GroupCardChangeEvent),
}
impl<'de> Deserialize<'de> for Notice {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let value = serde_json::Value::deserialize(deserializer)?;
        match value["notice_type"].as_str() {
            Some("group_upload") => serde_json::from_value(value)
                .map(Notice::GroupFileUpload)
                .map_err(D::Error::custom),
            Some("group_admin") => serde_json::from_value(value)
                .map(Notice::GroupAdminChange)
                .map_err(D::Error::custom),
            Some("group_decrease") => serde_json::from_value(value)
                .map(Notice::GroupMemberDecrease)
                .map_err(D::Error::custom),
            Some("group_increase") => serde_json::from_value(value)
                .map(Notice::GroupMemberIncrease)
                .map_err(D::Error::custom),
            Some("group_ban") => serde_json::from_value(value)
                .map(Notice::GroupBan)
                .map_err(D::Error::custom),
            Some("friend_add") => serde_json::from_value(value)
                .map(Notice::FriendAdd)
                .map_err(D::Error::custom),
            Some("group_recall") => serde_json::from_value(value)
                .map(Notice::GroupMessageRecall)
                .map_err(D::Error::custom),
            Some("friend_recall") => serde_json::from_value(value)
                .map(Notice::FriendMessageRecall)
                .map_err(D::Error::custom),
            // 仅NapCat / llOneBot 支持的事件
            Some("essence") => serde_json::from_value(value)
                .map(Notice::GroupEssenceMessageChange)
                .map_err(D::Error::custom),
            Some("group_card") => serde_json::from_value(value)
                .map(Notice::GroupCardChange)
                .map_err(D::Error::custom),

            Some("notify") => match value["sub_type"].as_str() {
                Some("poke") => serde_json::from_value(value)
                    .map(Notice::GroupPoke)
                    .map_err(D::Error::custom),
                Some("lucky_king") => serde_json::from_value(value)
                    .map(Notice::GroupLuckyKing)
                    .map_err(D::Error::custom),
                Some("honor") => serde_json::from_value(value)
                    .map(Notice::GroupMemberHonorChange)
                    .map_err(D::Error::custom),
                // 仅NapCat / llOneBot 支持的事件
                Some("input_status") => serde_json::from_value(value)
                    .map(Notice::FriendInputStatusChange)
                    .map_err(D::Error::custom),
                _ => Err(D::Error::custom("Invalid notify sub_type")),
            },
            _ => Err(D::Error::custom("Invalid notice_type")),
        }
    }
    fn deserialize_in_place<D>(deserializer: D, place: &mut Self) -> Result<(), D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let value = serde_json::Value::deserialize(deserializer)?;
        *place = match value["notice_type"].as_str() {
            Some("group_upload") => serde_json::from_value(value)
                .map(Notice::GroupFileUpload)
                .map_err(D::Error::custom)?,
            Some("group_admin") => serde_json::from_value(value)
                .map(Notice::GroupAdminChange)
                .map_err(D::Error::custom)?,
            Some("group_decrease") => serde_json::from_value(value)
                .map(Notice::GroupMemberDecrease)
                .map_err(D::Error::custom)?,
            Some("group_increase") => serde_json::from_value(value)
                .map(Notice::GroupMemberIncrease)
                .map_err(D::Error::custom)?,
            Some("group_ban") => serde_json::from_value(value)
                .map(Notice::GroupBan)
                .map_err(D::Error::custom)?,
            Some("friend_add") => serde_json::from_value(value)
                .map(Notice::FriendAdd)
                .map_err(D::Error::custom)?,
            Some("group_recall") => serde_json::from_value(value)
                .map(Notice::GroupMessageRecall)
                .map_err(D::Error::custom)?,
            Some("friend_recall") => serde_json::from_value(value)
                .map(Notice::FriendMessageRecall)
                .map_err(D::Error::custom)?,
            // 仅 NapCat / llOneBot 支持的事件
            Some("essence") => serde_json::from_value(value)
                .map(Notice::GroupEssenceMessageChange)
                .map_err(D::Error::custom)?,
            Some("group_card") => serde_json::from_value(value)
                .map(Notice::GroupCardChange)
                .map_err(D::Error::custom)?,

            Some("notify") => match value["sub_type"].as_str() {
                Some("poke") => serde_json::from_value(value)
                    .map(Notice::GroupPoke)
                    .map_err(D::Error::custom)?,
                Some("lucky_king") => serde_json::from_value(value)
                    .map(Notice::GroupLuckyKing)
                    .map_err(D::Error::custom)?,
                Some("honor") => serde_json::from_value(value)
                    .map(Notice::GroupMemberHonorChange)
                    .map_err(D::Error::custom)?,
                // 仅 NapCat / llOneBot 支持的事件
                Some("input_status") => serde_json::from_value(value)
                    .map(Notice::FriendInputStatusChange)
                    .map_err(D::Error::custom)?,
                _ => return Err(D::Error::custom("Invalid notify sub_type")),
            },
            _ => return Err(D::Error::custom("Invalid notice_type")),
        };
        Ok(())
    }
}

impl Serialize for Notice {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::ser::Serializer,
    {
        match self {
            Notice::GroupFileUpload(m) => m.serialize(serializer),
            Notice::GroupAdminChange(m) => m.serialize(serializer),
            Notice::GroupMemberDecrease(m) => m.serialize(serializer),
            Notice::GroupMemberIncrease(m) => m.serialize(serializer),
            Notice::GroupBan(m) => m.serialize(serializer),
            Notice::FriendAdd(m) => m.serialize(serializer),
            Notice::GroupMessageRecall(m) => m.serialize(serializer),
            Notice::FriendMessageRecall(m) => m.serialize(serializer),
            Notice::GroupPoke(m) => m.serialize(serializer),
            Notice::GroupLuckyKing(m) => m.serialize(serializer),
            Notice::GroupMemberHonorChange(m) => m.serialize(serializer),
            Notice::FriendInputStatusChange(m) => m.serialize(serializer),
            Notice::GroupEssenceMessageChange(m) => m.serialize(serializer),
            Notice::GroupCardChange(m) => m.serialize(serializer),
        }
    }
}
/// 群文件上传事件
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct GroupFileUploadEvent {
    /// 事件发生的时间戳
    pub time: i64,
    /// 收到事件的机器人 QQ 号
    pub self_id: i64,
    /// 上报类型
    pub post_type: String,
    /// 通知类型
    pub notice_type: String,
    /// 群号
    pub group_id: i64,
    /// 发送者 QQ 号
    pub user_id: i64,
    /// 文件信息
    pub file: GroupFile,
}

/// 群文件信息
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct GroupFile {
    /// 文件 ID
    pub id: String,
    /// 文件名
    pub name: String,
    /// 文件大小(字节数)
    pub size: i64,
    /// busid(目前不清楚有什么作用)
    pub busid: i64,
}

/// 群管理员变动事件
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct GroupAdminChangeEvent {
    /// 事件发生的时间戳
    pub time: i64,
    /// 收到事件的机器人 QQ 号
    pub self_id: i64,
    /// 上报类型
    pub post_type: String,
    /// 通知类型
    pub notice_type: String,
    /// 事件子类型,分别表示设置和取消管理员
    pub sub_type: String,
    /// 群号
    pub group_id: i64,
    /// 管理员 QQ 号
    pub user_id: i64,
}

/// 群成员减少事件
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct GroupMemberDecreaseEvent {
    /// 事件发生的时间戳
    pub time: i64,
    /// 收到事件的机器人 QQ 号
    pub self_id: i64,
    /// 上报类型
    pub post_type: String,
    /// 通知类型
    pub notice_type: String,
    /// 事件子类型,分别表示主动退群、成员被踢、登录号被踢
    pub sub_type: String,
    /// 群号
    pub group_id: i64,
    /// 操作者 QQ 号(如果是主动退群,则和 `user_id` 相同)
    pub operator_id: i64,
    /// 离开者 QQ 号
    pub user_id: i64,
}

/// 群成员增加事件
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct GroupMemberIncreaseEvent {
    /// 事件发生的时间戳
    pub time: i64,
    /// 收到事件的机器人 QQ 号
    pub self_id: i64,
    /// 上报类型
    pub post_type: String,
    /// 通知类型
    pub notice_type: String,
    /// 事件子类型,分别表示管理员已同意入群、管理员邀请入群
    pub sub_type: String,
    /// 群号
    pub group_id: i64,
    /// 操作者 QQ 号
    pub operator_id: i64,
    /// 加入者 QQ 号
    pub user_id: i64,
}

/// 群禁言事件
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct GroupBanEvent {
    /// 事件发生的时间戳
    pub time: i64,
    /// 收到事件的机器人 QQ 号
    pub self_id: i64,
    /// 上报类型
    pub post_type: String,
    /// 通知类型
    pub notice_type: String,
    /// 事件子类型,分别表示禁言、解除禁言
    pub sub_type: String,
    /// 群号
    pub group_id: i64,
    /// 操作者 QQ 号
    pub operator_id: i64,
    /// 被禁言 QQ 号
    pub user_id: i64,
    /// 禁言时长,单位秒
    pub duration: u64,
}

/// 好友添加事件
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct FriendAddEvent {
    /// 事件发生的时间戳
    pub time: i64,
    /// 收到事件的机器人 QQ 号
    pub self_id: i64,
    /// 上报类型
    pub post_type: String,
    /// 通知类型
    pub notice_type: String,
    /// 新添加好友 QQ 号
    pub user_id: i64,
}

/// 群消息撤回事件
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct GroupMessageRecallEvent {
    /// 事件发生的时间戳
    pub time: i64,
    /// 收到事件的机器人 QQ 号
    pub self_id: i64,
    /// 上报类型
    pub post_type: String,
    /// 通知类型
    pub notice_type: String,
    /// 群号
    pub group_id: i64,
    /// 消息发送者 QQ 号
    pub user_id: i64,
    /// 操作者 QQ 号
    pub operator_id: i64,
    /// 被撤回的消息 ID
    pub message_id: i64,
}

/// 好友消息撤回事件
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct FriendMessageRecallEvent {
    /// 事件发生的时间戳
    pub time: i64,
    /// 收到事件的机器人 QQ 号
    pub self_id: i64,
    /// 上报类型
    pub post_type: String,
    /// 通知类型
    pub notice_type: String,
    /// 好友 QQ 号
    pub user_id: i64,
    /// 被撤回的消息 ID
    pub message_id: i64,
}

/// 群内戳一戳事件
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct GroupPokeEvent {
    /// 事件发生的时间戳
    pub time: i64,
    /// 收到事件的机器人 QQ 号
    pub self_id: i64,
    /// 上报类型
    pub post_type: String,
    /// 通知类型
    pub notice_type: String,
    /// 子类型
    pub sub_type: String,
    /// 群号
    pub group_id: i64,
    /// 发送者 QQ 号
    pub user_id: i64,
    /// 被戳者 QQ 号
    pub target_id: i64,
}

/// 群红包运气王事件
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct GroupLuckyKingEvent {
    /// 事件发生的时间戳
    pub time: i64,
    /// 收到事件的机器人 QQ 号
    pub self_id: i64,
    /// 上报类型
    pub post_type: String,
    /// 通知类型
    pub notice_type: String,
    /// 子类型
    pub sub_type: String,
    /// 群号
    pub group_id: i64,
    /// 红包发送者 QQ 号
    pub user_id: i64,
    /// 运气王 QQ 号
    pub target_id: i64,
}

/// 群成员荣誉变更事件
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct GroupMemberHonorChangeEvent {
    /// 事件发生的时间戳
    pub time: i64,
    /// 收到事件的机器人 QQ 号
    pub self_id: i64,
    /// 上报类型
    pub post_type: String,
    /// 通知类型
    pub notice_type: String,
    /// 子类型
    pub sub_type: String,
    /// 群号
    pub group_id: i64,
    /// 荣誉类型,分别表示龙王、群聊之火、快乐源泉
    pub honor_type: String,
    /// 成员 QQ 号
    pub user_id: i64,
}

// 仅 NapCat / llOneBot 支持的事件
/// 私聊输入状态事件
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct FriendInputStatusChangeEvent {
    /// 事件发生的时间戳
    pub time: i64,
    /// 收到事件的机器人 QQ 号
    pub self_id: i64,
    /// 上报类型
    pub post_type: String,
    /// 通知类型
    pub notice_type: String,
    /// 子类型
    pub sub_type: String,
    /// 输入状态文本
    pub status_text: String,
    /// 事件类型
    pub event_type: u8,
    /// 好友 QQ 号
    pub user_id: i64,
}

/// 输入状态
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum InputStatus {
    /// 开始输入
    Start,
    /// 继续输入
    Typing,
    /// 清空输入
    Cleared,
}

impl FriendInputStatusChangeEvent {
    /// 转换为输入状态
    pub fn to_status(&self) -> InputStatus {
        if !self.status_text.is_empty() {
            if self.event_type == 1 {
                InputStatus::Start
            } else {
                InputStatus::Typing
            }
        } else {
            InputStatus::Cleared
        }
    }
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub enum EssenceMessageChangeType {
    /// 精华消息添加
    #[serde(rename = "add")]
    Add,
    /// 精华消息删除
    #[serde(rename = "delete")]
    Delete,
}

// 仅NapCat / llOneBot 支持的事件

/// 精华消息事件
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct GroupEssenceMessageChangeEvent {
    /// 事件发生的时间戳
    pub time: i64,
    /// 收到事件的机器人 QQ 号
    pub self_id: i64,
    /// 上报类型
    pub post_type: String,
    /// 群号
    pub group_id: i64,
    /// 操作者 QQ 号
    pub user_id: i64,
    /// 通知类型
    pub notice_type: String,
    /// 消息 ID
    pub message_id: i64,
    /// 发送者 QQ 号
    pub sender_id: i64,
    /// 子类型
    pub sub_type: EssenceMessageChangeType,
}

/// 群名片变更事件
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct GroupCardChangeEvent {
    /// 事件发生的时间戳
    pub time: i64,
    /// 收到事件的机器人 QQ 号
    pub self_id: i64,
    /// 上报类型
    pub post_type: String,
    /// 通知类型
    pub notice_type: String,
    /// 群号
    pub group_id: i64,
    /// 用户 QQ 号
    pub user_id: i64,
    /// 新名片
    pub card_new: String,
    /// 旧名片
    pub card_old: String,
}