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
# Intents API Reference

The `Intents` struct controls which gateway events your bot receives from QQ Guild. This system allows you to optimize performance and bandwidth by only subscribing to the events your bot actually needs.

## Overview

```rust
use botrs::Intents;

// Default intents for most bots
let intents = Intents::default();

// Custom intent combinations
let intents = Intents::GUILD_MESSAGES | Intents::GUILDS;

// Builder pattern
let intents = Intents::new()
    .with_guilds()
    .with_public_guild_messages()
    .with_direct_message();
```

Intents act as a subscription system for gateway events. By selecting only the intents you need, you can reduce bandwidth usage and improve bot performance.

## Intent Types

### Public Intents

These intents are available to all bots without special approval:

#### `GUILDS`

Guild creation, update, and deletion events.

```rust
const GUILDS: u32 = 1 << 0;
```

**Events enabled:**
- `guild_create`
- `guild_update` 
- `guild_delete`

#### `GUILD_MEMBERS`

Guild member join, update, and leave events.

```rust
const GUILD_MEMBERS: u32 = 1 << 1;
```

**Events enabled:**
- `guild_member_add`
- `guild_member_update`
- `guild_member_remove`

#### `GUILD_MESSAGE_REACTIONS`

Message reaction add and remove events.

```rust
const GUILD_MESSAGE_REACTIONS: u32 = 1 << 10;
```

**Events enabled:**
- Reaction add/remove events
- Emoji interaction events

#### `DIRECT_MESSAGE`

Private message events between users and the bot.

```rust
const DIRECT_MESSAGE: u32 = 1 << 12;
```

**Events enabled:**
- `direct_message_create`
- Private message events

#### `INTERACTION`

Interactive component events like button clicks and slash commands.

```rust
const INTERACTION: u32 = 1 << 26;
```

**Events enabled:**
- Button interactions
- Select menu interactions
- Slash command interactions

#### `MESSAGE_AUDIT`

Message audit and moderation events.

```rust
const MESSAGE_AUDIT: u32 = 1 << 27;
```

**Events enabled:**
- `message_audit_pass`
- `message_audit_reject`

#### `AUDIO_ACTION`

Voice channel and audio events.

```rust
const AUDIO_ACTION: u32 = 1 << 29;
```

**Events enabled:**
- Voice channel updates
- Audio state changes

#### `PUBLIC_GUILD_MESSAGES`

Public guild messages including @mentions and replies to the bot.

```rust
const PUBLIC_GUILD_MESSAGES: u32 = 1 << 30;
```

**Events enabled:**
- `message_create` (when bot is mentioned)
- Reply messages to bot
- Public channel messages involving bot

#### `AUDIO_OR_LIVE_CHANNEL_MEMBER`

Voice and live channel member events.

```rust
const AUDIO_OR_LIVE_CHANNEL_MEMBER: u32 = 1 << 19;
```

**Events enabled:**
- `audio_or_live_channel_member_enter`
- `audio_or_live_channel_member_exit`

#### `OPEN_FORUM_EVENT`

Public forum thread and post events.

```rust
const OPEN_FORUM_EVENT: u32 = 1 << 18;
```

**Events enabled:**
- `open_forum_thread_create`
- `open_forum_thread_update`
- `open_forum_thread_delete`
- `open_forum_post_create`
- `open_forum_post_delete`
- `open_forum_reply_create`
- `open_forum_reply_delete`

#### `PUBLIC_MESSAGES`

Group and C2C message events.

```rust
const PUBLIC_MESSAGES: u32 = 1 << 25;
```

**Events enabled:**
- `group_message_create`
- `c2c_message_create`

### Privileged Intents

These intents require special approval from QQ and may have additional restrictions:

#### `GUILD_MESSAGES`

All guild message events (privileged).

```rust
const GUILD_MESSAGES: u32 = 1 << 9;
```

**Requirements:**
- Special approval from QQ
- Additional verification for large bots

**Events enabled:**
- All `message_create` events in guilds
- `message_delete` events

#### `FORUMS`

Forum thread and post events (privileged).

```rust
const FORUMS: u32 = 1 << 28;
```

**Requirements:**
- Special approval from QQ
- May require additional permissions

**Events enabled:**
- All forum-related events
- Private forum access

## Constructor Methods

### `new`

Creates an empty intent set.

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

#### Example

```rust
let intents = Intents::new(); // No intents enabled
```

### `none`

Creates an intent set with no intents enabled (alias for `new`).

```rust
pub const fn none() -> Self
```

### `all`

Creates an intent set with all available intents enabled.

```rust
pub const fn all() -> Self
```

#### Example

```rust
let intents = Intents::all(); // All intents enabled
```

### `default`

Creates the default intent set for most bots (excludes privileged intents).

```rust
pub const fn default() -> Self
```

The default intents include all public intents but exclude `GUILD_MESSAGES` and `FORUMS` which require special approval.

#### Example

```rust
let intents = Intents::default(); // Safe for most bots
```

### `from_bits`

Creates intents from raw bit flags.

```rust
pub const fn from_bits(bits: u32) -> Self
```

#### Parameters

- `bits`: Raw intent bit flags

#### Example

```rust
let intents = Intents::from_bits(0b1011); // Custom bit combination
```

## Intent Management

### `contains`

Checks if a specific intent is enabled.

```rust
pub const fn contains(self, intent: u32) -> bool
```

#### Parameters

- `intent`: The intent flag to check

#### Returns

`true` if the intent is enabled, `false` otherwise.

#### Example

```rust
let intents = Intents::GUILDS | Intents::PUBLIC_GUILD_MESSAGES;
assert!(intents.contains(Intents::GUILDS));
assert!(!intents.contains(Intents::DIRECT_MESSAGE));
```

### `with_intent`

Enables a specific intent.

```rust
pub const fn with_intent(self, intent: u32) -> Self
```

#### Parameters

- `intent`: The intent flag to enable

#### Returns

New `Intents` instance with the intent enabled.

#### Example

```rust
let intents = Intents::new().with_intent(Intents::GUILDS);
```

### `without_intent`

Disables a specific intent.

```rust
pub const fn without_intent(self, intent: u32) -> Self
```

#### Parameters

- `intent`: The intent flag to disable

#### Returns

New `Intents` instance with the intent disabled.

#### Example

```rust
let intents = Intents::all().without_intent(Intents::GUILD_MESSAGES);
```

## Builder Methods

### Guild Intents

```rust
pub const fn with_guilds(self) -> Self
pub const fn with_guild_members(self) -> Self
pub const fn with_guild_messages(self) -> Self
pub const fn with_guild_message_reactions(self) -> Self
```

### Message Intents

```rust
pub const fn with_direct_message(self) -> Self
pub const fn with_public_guild_messages(self) -> Self
pub const fn with_public_messages(self) -> Self
```

### Feature Intents

```rust
pub const fn with_interaction(self) -> Self
pub const fn with_message_audit(self) -> Self
pub const fn with_forums(self) -> Self
pub const fn with_audio_action(self) -> Self
pub const fn with_audio_or_live_channel_member(self) -> Self
pub const fn with_open_forum_event(self) -> Self
```

#### Example

```rust
let intents = Intents::new()
    .with_guilds()
    .with_public_guild_messages()
    .with_direct_message()
    .with_interaction();
```

## Query Methods

### Guild Queries

```rust
pub const fn guilds(self) -> bool
pub const fn guild_members(self) -> bool
pub const fn guild_messages(self) -> bool
pub const fn guild_message_reactions(self) -> bool
```

### Message Queries

```rust
pub const fn direct_message(self) -> bool
pub const fn public_guild_messages(self) -> bool
pub const fn public_messages(self) -> bool
```

### Feature Queries

```rust
pub const fn interaction(self) -> bool
pub const fn message_audit(self) -> bool
pub const fn forums(self) -> bool
pub const fn audio_action(self) -> bool
pub const fn audio_or_live_channel_member(self) -> bool
pub const fn open_forum_event(self) -> bool
```

#### Example

```rust
let intents = Intents::default();

if intents.guilds() {
    println!("Guild events enabled");
}

if intents.direct_message() {
    println!("Direct message events enabled");
}
```

## Utility Methods

### `has_privileged`

Checks if any privileged intents are enabled.

```rust
pub const fn has_privileged(self) -> bool
```

#### Returns

`true` if `GUILD_MESSAGES` or `FORUMS` intents are enabled.

#### Example

```rust
let intents = Intents::default();
assert!(!intents.has_privileged()); // Default excludes privileged

let privileged = Intents::new().with_guild_messages();
assert!(privileged.has_privileged());
```

### `bits`

Gets the raw intent bit flags.

```rust
pub const fn bits(self) -> u32
```

#### Returns

The raw intent bits as a 32-bit unsigned integer.

#### Example

```rust
let intents = Intents::GUILDS | Intents::PUBLIC_GUILD_MESSAGES;
let bits = intents.bits();
println!("Intent bits: {:#032b}", bits);
```

## Bitwise Operations

Intents support standard bitwise operations for combining and manipulating intent sets:

### Bitwise OR (`|`)

Combines intents from multiple sets.

```rust
let intents = Intents::GUILDS | Intents::PUBLIC_GUILD_MESSAGES | Intents::DIRECT_MESSAGE;
```

### Bitwise AND (`&`)

Finds common intents between sets.

```rust
let common = intents1 & intents2;
```

### Bitwise XOR (`^`)

Finds intents that differ between sets.

```rust
let different = intents1 ^ intents2;
```

### Bitwise NOT (`!`)

Inverts all intent flags.

```rust
let inverted = !intents;
```

### Assignment Operators

```rust
let mut intents = Intents::new();
intents |= Intents::GUILDS;        // Add intent
intents &= !Intents::DIRECT_MESSAGE; // Remove intent
```

## Common Usage Patterns

### Basic Bot

```rust
// Simple bot that responds to mentions
let intents = Intents::PUBLIC_GUILD_MESSAGES | Intents::GUILDS;
```

### Moderation Bot

```rust
// Bot with moderation capabilities
let intents = Intents::default()
    .with_guild_members()
    .with_message_audit();
```

### Voice Bot

```rust
// Bot that manages voice channels
let intents = Intents::new()
    .with_guilds()
    .with_audio_action()
    .with_audio_or_live_channel_member();
```

### Forum Bot

```rust
// Bot that manages forum content
let intents = Intents::new()
    .with_guilds()
    .with_open_forum_event()
    .with_forums(); // Requires approval
```

### Comprehensive Bot

```rust
// Bot with full capabilities (requires privileged intents)
let intents = Intents::all();
```

## Privileged Intent Approval

To use privileged intents (`GUILD_MESSAGES`, `FORUMS`), you need:

1. **Application Review**: Submit your bot for review in the QQ Developer Portal
2. **Use Case Justification**: Explain why your bot needs access to these events
3. **Privacy Compliance**: Ensure your bot complies with data protection requirements
4. **Scale Verification**: For large bots (100+ guilds), additional verification may be required

### Requesting Approval

1. Visit the QQ Developer Portal
2. Navigate to your bot's settings
3. Request privileged intent access
4. Provide detailed justification
5. Wait for approval (can take several days)

## Error Handling

### Missing Intents

If your bot doesn't receive expected events, verify your intents:

```rust
let intents = Intents::default();

// Check if required intents are enabled
if !intents.guild_members() {
    println!("Warning: Guild member events not enabled");
}

if !intents.public_guild_messages() {
    println!("Warning: Public guild messages not enabled");
}
```

### Privileged Intent Errors

```rust
impl EventHandler for MyBot {
    async fn error(&self, error: BotError) {
        match error {
            BotError::Forbidden(msg) if msg.contains("intent") => {
                eprintln!("Missing required intents or privileged intent not approved");
            }
            _ => {}
        }
    }
}
```

## Best Practices

### Intent Selection

1. **Minimal Principle**: Only enable intents you actually use
2. **Performance**: Fewer intents = better performance and lower bandwidth
3. **Privacy**: Avoid privileged intents unless absolutely necessary
4. **Documentation**: Document why each intent is needed

### Production Considerations

1. **Testing**: Test with minimal intents in development
2. **Monitoring**: Monitor for missing events that might indicate intent issues
3. **Approval Process**: Plan for privileged intent approval timeline
4. **Fallback**: Design graceful degradation when intents are missing

### Code Organization

```rust
// Define intents as constants for reuse
const BOT_INTENTS: Intents = Intents::new()
    .with_guilds()
    .with_public_guild_messages()
    .with_direct_message();

// Validate intents at startup
fn validate_intents(intents: Intents) -> Result<(), String> {
    if !intents.guilds() {
        return Err("Guild events are required".to_string());
    }
    
    if intents.has_privileged() {
        println!("Warning: Using privileged intents");
    }
    
    Ok(())
}
```

## See Also

- [Intents Guide]/guide/intents - Comprehensive guide to intent usage
- [`Client`]./client.md - Bot client configuration
- [`EventHandler`]./event-handler.md - Event handling with intents
- [Gateway Guide]/guide/gateway - Gateway connection and intents