botrs 0.2.7

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
# Guilds and Channels Models API Reference

This module provides data structures for guilds (servers) and channels within the QQ Guild Bot API.

## Guild Types

### `Guild`

Represents a QQ guild (server) that the bot has access to.

```rust
pub struct Guild {
    pub id: String,
    pub name: String,
    pub icon: Option<String>,
    pub owner_id: String,
    pub owner: bool,
    pub member_count: Option<u32>,
    pub max_members: Option<u32>,
    pub description: Option<String>,
    pub joined_at: Option<String>,
    pub features: Vec<String>,
    pub op_user_id: Option<String>,
}
```

#### Fields

- `id`: Unique identifier for the guild
- `name`: Guild name
- `icon`: Guild icon URL (optional)
- `owner_id`: User ID of the guild owner
- `owner`: Whether the bot is the owner of the guild
- `member_count`: Current number of members
- `max_members`: Maximum allowed members
- `description`: Guild description
- `joined_at`: Timestamp when the bot joined the guild
- `features`: List of guild features enabled
- `op_user_id`: Operator user ID

#### Example

```rust
async fn handle_guild_create(ctx: Context, guild: Guild) {
    println!("Joined guild: {} (ID: {})", guild.name, guild.id);
    println!("Member count: {}", guild.member_count.unwrap_or(0));
    
    if guild.owner {
        println!("Bot is the owner of this guild");
    }
}
```

### `GuildRole`

Represents a role within a guild.

```rust
pub struct GuildRole {
    pub id: String,
    pub name: String,
    pub color: Option<u32>,
    pub hoist: Option<bool>,
    pub number: Option<u32>,
    pub member_limit: Option<u32>,
}
```

#### Fields

- `id`: Unique identifier for the role
- `name`: Role name
- `color`: Role color (as hex value)
- `hoist`: Whether the role is displayed separately in member list
- `number`: Role position/priority
- `member_limit`: Maximum number of members that can have this role

#### Example

```rust
async fn list_guild_roles(ctx: Context, guild_id: &str) -> Result<()> {
    let roles = ctx.get_guild_roles(guild_id).await?;
    
    for role in roles.roles {
        println!("Role: {} (ID: {})", role.name, role.id);
        if let Some(color) = role.color {
            println!("  Color: #{:06X}", color);
        }
        if let Some(limit) = role.member_limit {
            println!("  Member limit: {}", limit);
        }
    }
    
    Ok(())
}
```

### `GuildRoles`

Container for guild role information.

```rust
pub struct GuildRoles {
    pub guild_id: String,
    pub roles: Vec<GuildRole>,
    pub role_num_limit: Option<u32>,
}
```

#### Fields

- `guild_id`: ID of the guild these roles belong to
- `roles`: List of roles in the guild
- `role_num_limit`: Maximum number of roles allowed in the guild

## Channel Types

### `Channel`

Represents a channel within a guild.

```rust
pub struct Channel {
    pub id: String,
    pub guild_id: String,
    pub name: String,
    pub channel_type: ChannelType,
    pub sub_type: Option<ChannelSubType>,
    pub position: Option<u32>,
    pub parent_id: Option<String>,
    pub owner_id: Option<String>,
    pub private_type: Option<u32>,
    pub speak_permission: Option<u32>,
    pub application_id: Option<String>,
    pub permissions: Option<String>,
}
```

#### Fields

- `id`: Unique identifier for the channel
- `guild_id`: ID of the guild this channel belongs to
- `name`: Channel name
- `channel_type`: Type of channel (text, voice, etc.)
- `sub_type`: Channel subtype for additional classification
- `position`: Channel position in the channel list
- `parent_id`: ID of parent channel (for channel categories)
- `owner_id`: ID of the channel owner
- `private_type`: Privacy setting for the channel
- `speak_permission`: Speaking permission requirements
- `application_id`: Associated application ID
- `permissions`: Channel-specific permissions

#### Example

```rust
async fn handle_channel_create(ctx: Context, channel: Channel) {
    println!("New channel created: {} (Type: {:?})", channel.name, channel.channel_type);
    
    match channel.channel_type {
        ChannelType::Text => {
            println!("Text channel created in guild {}", channel.guild_id);
        }
        ChannelType::Voice => {
            println!("Voice channel created in guild {}", channel.guild_id);
        }
        ChannelType::Category => {
            println!("Category created in guild {}", channel.guild_id);
        }
        _ => {
            println!("Other channel type created");
        }
    }
}
```

### `ChannelType`

Enumeration of different channel types.

```rust
pub enum ChannelType {
    Text = 0,
    Voice = 1,
    Category = 4,
    Announcement = 5,
    Forum = 10,
    Live = 11,
    Application = 12,
}
```

#### Variants

- `Text`: Text channel for messages
- `Voice`: Voice channel for audio communication
- `Category`: Category to organize channels
- `Announcement`: Announcement channel
- `Forum`: Forum channel for threaded discussions
- `Live`: Live streaming channel
- `Application`: Application-specific channel

#### Example

```rust
async fn create_text_channel(ctx: Context, guild_id: &str, name: &str) -> Result<Channel> {
    let channel = ctx.create_channel(
        guild_id,
        name,
        ChannelType::Text,
        None, // sub_type
        None, // position
        None, // parent_id
        None, // private_type
        None, // speak_permission
        None, // application_id
    ).await?;
    
    println!("Created text channel: {}", channel.name);
    Ok(channel)
}
```

### `ChannelSubType`

Enumeration of channel subtypes for additional classification.

```rust
pub enum ChannelSubType {
    Chat = 0,
    Announcement = 1,
    Guide = 2,
    Game = 3,
}
```

#### Variants

- `Chat`: General chat channel
- `Announcement`: Announcement-specific channel
- `Guide`: Guide or help channel
- `Game`: Gaming-related channel

## Member Types

### `Member`

Represents a member of a guild.

```rust
pub struct Member {
    pub user: Option<User>,
    pub nick: Option<String>,
    pub roles: Vec<String>,
    pub joined_at: Option<String>,
    pub deaf: Option<bool>,
    pub mute: Option<bool>,
}
```

#### Fields

- `user`: User information for this member
- `nick`: Member's nickname in the guild
- `roles`: List of role IDs assigned to the member
- `joined_at`: Timestamp when the member joined the guild
- `deaf`: Whether the member is deafened in voice channels
- `mute`: Whether the member is muted in voice channels

#### Example

```rust
async fn handle_member_update(ctx: Context, member: Member) {
    if let Some(user) = &member.user {
        println!("Member updated: {}", user.username.as_deref().unwrap_or("Unknown"));
        
        if let Some(nick) = &member.nick {
            println!("Nickname: {}", nick);
        }
        
        println!("Roles: {:?}", member.roles);
    }
}
```

### `User`

Represents user information.

```rust
pub struct User {
    pub id: String,
    pub username: Option<String>,
    pub avatar: Option<String>,
    pub bot: Option<bool>,
    pub union_openid: Option<String>,
    pub union_user_account: Option<String>,
}
```

#### Fields

- `id`: Unique user identifier
- `username`: User's display name
- `avatar`: Avatar image URL
- `bot`: Whether this user is a bot
- `union_openid`: Union OpenID for cross-platform identification
- `union_user_account`: Union user account identifier

## Channel Management

### Creating Channels

```rust
async fn setup_guild_channels(ctx: Context, guild_id: &str) -> Result<()> {
    // Create a category
    let category = ctx.create_channel(
        guild_id,
        "General",
        ChannelType::Category,
        None,
        Some(0), // position at top
        None,
        None,
        None,
        None,
    ).await?;
    
    // Create text channels under the category
    let general_chat = ctx.create_channel(
        guild_id,
        "general",
        ChannelType::Text,
        Some(ChannelSubType::Chat),
        Some(1),
        Some(&category.id), // parent category
        None,
        None,
        None,
    ).await?;
    
    let announcements = ctx.create_channel(
        guild_id,
        "announcements",
        ChannelType::Text,
        Some(ChannelSubType::Announcement),
        Some(2),
        Some(&category.id),
        None,
        None,
        None,
    ).await?;
    
    println!("Created category '{}' with channels:", category.name);
    println!("  - {}", general_chat.name);
    println!("  - {}", announcements.name);
    
    Ok(())
}
```

### Channel Permissions

```rust
async fn manage_channel_permissions(ctx: Context, channel_id: &str, user_id: &str) -> Result<()> {
    // Get current permissions for a user
    let permissions = ctx.get_channel_user_permissions(channel_id, user_id).await?;
    println!("User permissions: {}", permissions.permissions);
    
    // Get permissions for a role
    let role_id = "role_id_here";
    let role_permissions = ctx.get_channel_role_permissions(channel_id, role_id).await?;
    println!("Role permissions: {}", role_permissions.permissions);
    
    Ok(())
}
```

### Guild Member Management

```rust
async fn manage_guild_members(ctx: Context, guild_id: &str) -> Result<()> {
    // Get guild members
    let members = ctx.get_guild_members(guild_id, Some(100), None).await?;
    println!("Guild has {} members", members.len());
    
    for member in &members {
        if let Some(user) = &member.user {
            println!("Member: {}", user.username.as_deref().unwrap_or("Unknown"));
            println!("  Roles: {:?}", member.roles);
            
            if let Some(joined) = &member.joined_at {
                println!("  Joined: {}", joined);
            }
        }
    }
    
    // Get specific member
    let user_id = "specific_user_id";
    let member = ctx.get_guild_member(guild_id, user_id).await?;
    if let Some(user) = &member.user {
        println!("Found member: {}", user.username.as_deref().unwrap_or("Unknown"));
    }
    
    Ok(())
}
```

### Role Management

```rust
async fn manage_roles(ctx: Context, guild_id: &str) -> Result<()> {
    // Create a new role
    let new_role = ctx.create_guild_role(
        guild_id,
        "Moderator",
        Some(0x0099ff), // Blue color
        Some(true),     // Hoist (display separately)
        Some(100),      // Member limit
    ).await?;
    
    println!("Created role: {} (ID: {})", new_role.name, new_role.id);
    
    // Assign role to a user
    let user_id = "user_id_here";
    ctx.add_guild_role_member(guild_id, &new_role.id, user_id, None).await?;
    println!("Assigned role to user");
    
    // Update role
    let updated_role = ctx.update_guild_role(
        guild_id,
        &new_role.id,
        "Senior Moderator",
        Some(0xff9900), // Orange color
        Some(true),
        Some(50), // Reduced member limit
    ).await?;
    
    println!("Updated role: {}", updated_role.name);
    
    // Remove role from user
    ctx.remove_guild_role_member(guild_id, &new_role.id, user_id, None).await?;
    println!("Removed role from user");
    
    // Delete role
    ctx.delete_guild_role(guild_id, &new_role.id).await?;
    println!("Deleted role");
    
    Ok(())
}
```

## Common Usage Patterns

### Guild Discovery

```rust
async fn explore_guilds(ctx: Context) -> Result<()> {
    let guilds = ctx.get_guilds(None, None).await?;
    
    for guild in guilds {
        println!("Guild: {} (ID: {})", guild.name, guild.id);
        
        // Get channels for this guild
        let channels = ctx.get_channels(&guild.id).await?;
        println!("  Channels ({}):", channels.len());
        
        for channel in channels {
            let type_name = match channel.channel_type {
                ChannelType::Text => "Text",
                ChannelType::Voice => "Voice",
                ChannelType::Category => "Category",
                ChannelType::Announcement => "Announcement",
                ChannelType::Forum => "Forum",
                ChannelType::Live => "Live",
                ChannelType::Application => "Application",
            };
            
            println!("    {} - {} ({})", channel.name, type_name, channel.id);
        }
    }
    
    Ok(())
}
```

### Channel Organization

```rust
async fn organize_channels(ctx: Context, guild_id: &str) -> Result<()> {
    let channels = ctx.get_channels(guild_id).await?;
    
    // Group channels by category
    let mut categories = std::collections::HashMap::new();
    let mut orphaned_channels = Vec::new();
    
    for channel in channels {
        match (channel.channel_type, &channel.parent_id) {
            (ChannelType::Category, _) => {
                categories.insert(channel.id.clone(), (channel, Vec::new()));
            }
            (_, Some(parent_id)) => {
                if let Some((_, children)) = categories.get_mut(parent_id) {
                    children.push(channel);
                }
            }
            (_, None) => {
                orphaned_channels.push(channel);
            }
        }
    }
    
    // Print organized structure
    for (_, (category, children)) in categories {
        println!("Category: {}", category.name);
        for child in children {
            println!("  └─ {}", child.name);
        }
    }
    
    if !orphaned_channels.is_empty() {
        println!("Uncategorized channels:");
        for channel in orphaned_channels {
            println!("  - {}", channel.name);
        }
    }
    
    Ok(())
}
```

## See Also

- [Client API]../client.md - Main client for guild and channel operations
- [Context API]../context.md - Context object for API access
- [Messages]./messages.md - Message types and handling
- [Users & Members]./users-members.md - User and member management