botrs 0.2.9

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
# Client & Event Handler

This guide covers the core concepts of BotRS: the `Client` and `EventHandler`. These two components form the foundation of every bot application, handling connections, authentication, and event processing.

## Understanding the Client

The `Client` is the main orchestrator of your bot. It manages the WebSocket connection to QQ's servers, handles authentication, and dispatches events to your event handler.

### Client Lifecycle

```rust
use botrs::{Client, EventHandler, Intents, Token};

// 1. Create token with credentials
let token = Token::new("your_app_id", "your_secret");

// 2. Configure intents (what events to receive)
let intents = Intents::default().with_public_guild_messages();

// 3. Create your event handler
struct MyBot;

#[async_trait::async_trait]
impl EventHandler for MyBot {
    // Define how to handle events
}

// 4. Create and start the client
let mut client = Client::new(token, intents, MyBot, false)?;
client.start().await?; // This blocks until the bot stops
```

### Client Configuration

#### Environment Selection

```rust
// Production environment
let client = Client::new(token, intents, handler, false)?;

// Sandbox environment (for testing)
let client = Client::new(token, intents, handler, true)?;
```

#### Connection Management

The client automatically handles:
- WebSocket connection establishment
- Authentication with QQ servers
- Heartbeat maintenance
- Automatic reconnection on network issues
- Rate limiting compliance

## Understanding Event Handlers

The `EventHandler` trait defines how your bot responds to events from QQ Guild. You implement this trait to define your bot's behavior.

### Basic Event Handler

```rust
use botrs::{Context, EventHandler, Message, Ready};

struct MyBot;

#[async_trait::async_trait]
impl EventHandler for MyBot {
    // Called once when bot connects
    async fn ready(&self, _ctx: Context, ready: Ready) {
        println!("Bot {} is ready!", ready.user.username);
    }

    // Called when someone mentions your bot
    async fn message_create(&self, ctx: Context, message: Message) {
        if let Some(content) = &message.content {
            if content == "!ping" {
                let _ = message.reply(&ctx.api, &ctx.token, "Pong!").await;
            }
        }
    }
}
```

### Event Handler with State

For more complex bots, you can maintain state within your event handler:

```rust
use std::sync::Arc;
use tokio::sync::RwLock;
use std::collections::HashMap;

struct StatefulBot {
    // Shared state between events
    user_data: Arc<RwLock<HashMap<String, UserInfo>>>,
    config: BotConfig,
}

impl StatefulBot {
    fn new(config: BotConfig) -> Self {
        Self {
            user_data: Arc::new(RwLock::new(HashMap::new())),
            config,
        }
    }
    
    async fn get_user_info(&self, user_id: &str) -> Option<UserInfo> {
        let data = self.user_data.read().await;
        data.get(user_id).cloned()
    }
    
    async fn update_user_info(&self, user_id: String, info: UserInfo) {
        let mut data = self.user_data.write().await;
        data.insert(user_id, info);
    }
}

#[async_trait::async_trait]
impl EventHandler for StatefulBot {
    async fn message_create(&self, ctx: Context, message: Message) {
        // Access shared state
        if let Some(author) = &message.author {
            if let Some(user_id) = &author.id {
                // Update user information
                let info = UserInfo {
                    last_message: chrono::Utc::now(),
                    message_count: self.get_user_info(user_id)
                        .await
                        .map(|u| u.message_count + 1)
                        .unwrap_or(1),
                };
                self.update_user_info(user_id.clone(), info).await;
            }
        }
    }
}
```

## The Context Parameter

Every event handler method receives a `Context` parameter that provides access to essential bot functionality:

```rust
pub struct Context {
    pub api: BotApi,     // API client for making requests
    pub token: Token,    // Authentication token
    // Additional context data...
}
```

### Using Context

```rust
async fn message_create(&self, ctx: Context, message: Message) {
    // Send a message
    let params = MessageParams::new_text("Hello!");
    ctx.api.post_message_with_params(&ctx.token, &channel_id, params).await?;
    
    // Get guild information
    let guild = ctx.api.get_guild(&ctx.token, &guild_id).await?;
    
    // Manage channel permissions
    ctx.api.modify_channel_permissions(&ctx.token, &channel_id, &permissions).await?;
}
```

## Event Types

### Core Events

#### Ready Event
```rust
async fn ready(&self, ctx: Context, ready: Ready) {
    // Bot is connected and ready
    // Access bot user info: ready.user
    // Access initial guild list: ready.guilds
}
```

#### Message Events
```rust
// Guild message with @mention
async fn message_create(&self, ctx: Context, message: Message) {
    // Handle @ mentions in guild channels
}

// Direct messages
async fn direct_message_create(&self, ctx: Context, message: DirectMessage) {
    // Handle private messages
}

// Group messages
async fn group_message_create(&self, ctx: Context, message: GroupMessage) {
    // Handle group chat messages
}
```

### Guild Events

```rust
// Guild lifecycle
async fn guild_create(&self, ctx: Context, guild: Guild) {
    // Bot joined a guild or guild became available
}

async fn guild_update(&self, ctx: Context, guild: Guild) {
    // Guild information changed
}

async fn guild_delete(&self, ctx: Context, guild: Guild) {
    // Bot left guild or guild became unavailable
}
```

### Channel Events

```rust
async fn channel_create(&self, ctx: Context, channel: Channel) {
    // New channel created
}

async fn channel_update(&self, ctx: Context, channel: Channel) {
    // Channel updated
}

async fn channel_delete(&self, ctx: Context, channel: Channel) {
    // Channel deleted
}
```

### Member Events

```rust
async fn guild_member_add(&self, ctx: Context, member: Member) {
    // New member joined
}

async fn guild_member_update(&self, ctx: Context, member: Member) {
    // Member information updated
}

async fn guild_member_remove(&self, ctx: Context, member: Member) {
    // Member left or was removed
}
```

## Error Handling in Event Handlers

### Basic Error Handling

```rust
async fn message_create(&self, ctx: Context, message: Message) {
    if let Some(content) = &message.content {
        match self.process_command(content).await {
            Ok(response) => {
                if let Err(e) = message.reply(&ctx.api, &ctx.token, &response).await {
                    eprintln!("Failed to send reply: {}", e);
                }
            }
            Err(e) => {
                eprintln!("Error processing command: {}", e);
                let _ = message.reply(&ctx.api, &ctx.token, "Sorry, something went wrong!").await;
            }
        }
    }
}
```

### Centralized Error Handling

```rust
async fn error(&self, error: BotError) {
    match error {
        BotError::Network(e) => {
            eprintln!("Network error: {}", e);
            // Maybe implement reconnection logic
        }
        BotError::RateLimited(info) => {
            println!("Rate limited for {} seconds", info.retry_after);
            // Wait and retry logic
        }
        BotError::Authentication(e) => {
            eprintln!("Auth error: {}", e);
            // Handle authentication issues
        }
        _ => {
            eprintln!("Unexpected error: {}", error);
        }
    }
}
```

## Best Practices

### Performance

1. **Keep event handlers lightweight**
   ```rust
   async fn message_create(&self, ctx: Context, message: Message) {
       // Spawn heavy work in background
       let api = ctx.api.clone();
       let token = ctx.token.clone();
       
       tokio::spawn(async move {
           // Heavy computation here
           let result = heavy_computation().await;
           // Send result back to channel
       });
   }
   ```

2. **Use appropriate data structures for state**
   ```rust
   // For read-heavy workloads
   use std::sync::Arc;
   use tokio::sync::RwLock;
   
   // For simple atomic operations
   use std::sync::atomic::{AtomicU64, Ordering};
   
   // For concurrent collections
   use dashmap::DashMap;
   ```

### Error Recovery

1. **Graceful degradation**
   ```rust
   async fn message_create(&self, ctx: Context, message: Message) {
       match self.get_user_permissions(&ctx, &message).await {
           Ok(perms) if perms.can_execute_commands() => {
               // Execute command
           }
           Ok(_) => {
               // User doesn't have permission
               let _ = message.reply(&ctx.api, &ctx.token, "Permission denied").await;
           }
           Err(_) => {
               // Fallback: allow command but log the error
               eprintln!("Failed to check permissions, allowing command");
           }
       }
   }
   ```

2. **Retry logic for transient failures**
   ```rust
   async fn send_with_retry(&self, ctx: &Context, channel_id: &str, content: &str) -> Result<(), BotError> {
       for attempt in 1..=3 {
           match ctx.api.post_message_with_params(
               &ctx.token, 
               channel_id, 
               MessageParams::new_text(content)
           ).await {
               Ok(response) => return Ok(()),
               Err(BotError::Network(_)) if attempt < 3 => {
                   tokio::time::sleep(Duration::from_millis(1000 * attempt)).await;
                   continue;
               }
               Err(e) => return Err(e),
           }
       }
       unreachable!()
   }
   ```

### Resource Management

1. **Limit concurrent operations**
   ```rust
   use tokio::sync::Semaphore;
   
   struct MyBot {
       semaphore: Arc<Semaphore>,
   }
   
   impl MyBot {
       fn new() -> Self {
           Self {
               semaphore: Arc::new(Semaphore::new(10)), // Max 10 concurrent operations
           }
       }
   }
   
   #[async_trait::async_trait]
   impl EventHandler for MyBot {
       async fn message_create(&self, ctx: Context, message: Message) {
           let _permit = self.semaphore.acquire().await.unwrap();
           // Process message with limited concurrency
       }
   }
   ```

## Complete Example

Here's a comprehensive example that demonstrates these concepts:

```rust
use botrs::{Client, Context, EventHandler, Intents, Message, Ready, Token, BotError};
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::RwLock;
use tracing::{info, warn, error};

#[derive(Clone)]
struct UserStats {
    message_count: u64,
    last_active: chrono::DateTime<chrono::Utc>,
}

struct ComprehensiveBot {
    stats: Arc<RwLock<HashMap<String, UserStats>>>,
    start_time: chrono::DateTime<chrono::Utc>,
}

impl ComprehensiveBot {
    fn new() -> Self {
        Self {
            stats: Arc::new(RwLock::new(HashMap::new())),
            start_time: chrono::Utc::now(),
        }
    }
    
    async fn update_user_stats(&self, user_id: &str) {
        let mut stats = self.stats.write().await;
        let entry = stats.entry(user_id.to_string()).or_insert(UserStats {
            message_count: 0,
            last_active: chrono::Utc::now(),
        });
        entry.message_count += 1;
        entry.last_active = chrono::Utc::now();
    }
    
    async fn handle_command(&self, ctx: &Context, message: &Message, command: &str, args: &[&str]) -> Result<String, Box<dyn std::error::Error + Send + Sync>> {
        match command {
            "ping" => Ok("Pong! 🏓".to_string()),
            "uptime" => {
                let uptime = chrono::Utc::now() - self.start_time;
                Ok(format!("Bot uptime: {} seconds", uptime.num_seconds()))
            }
            "stats" => {
                if let Some(author) = &message.author {
                    if let Some(user_id) = &author.id {
                        let stats = self.stats.read().await;
                        if let Some(user_stats) = stats.get(user_id) {
                            Ok(format!("Messages sent: {}, Last active: {}", 
                                     user_stats.message_count, 
                                     user_stats.last_active.format("%Y-%m-%d %H:%M:%S")))
                        } else {
                            Ok("No stats available".to_string())
                        }
                    } else {
                        Ok("Could not identify user".to_string())
                    }
                } else {
                    Ok("No author information".to_string())
                }
            }
            "help" => Ok("Available commands: !ping, !uptime, !stats, !help".to_string()),
            _ => Ok(format!("Unknown command: {}. Type !help for available commands.", command)),
        }
    }
}

#[async_trait::async_trait]
impl EventHandler for ComprehensiveBot {
    async fn ready(&self, _ctx: Context, ready: Ready) {
        info!("🤖 Bot is ready! Logged in as: {}", ready.user.username);
        info!("📊 Connected to {} guilds", ready.guilds.len());
    }
    
    async fn message_create(&self, ctx: Context, message: Message) {
        // Skip bot messages
        if message.is_from_bot() {
            return;
        }
        
        // Update user statistics
        if let Some(author) = &message.author {
            if let Some(user_id) = &author.id {
                self.update_user_stats(user_id).await;
            }
        }
        
        // Process commands
        if let Some(content) = &message.content {
            let content = content.trim();
            if let Some(command_text) = content.strip_prefix('!') {
                let parts: Vec<&str> = command_text.split_whitespace().collect();
                if !parts.is_empty() {
                    let command = parts[0];
                    let args = &parts[1..];
                    
                    match self.handle_command(&ctx, &message, command, args).await {
                        Ok(response) => {
                            if let Err(e) = message.reply(&ctx.api, &ctx.token, &response).await {
                                warn!("Failed to send reply: {}", e);
                            }
                        }
                        Err(e) => {
                            error!("Error handling command '{}': {}", command, e);
                            let _ = message.reply(&ctx.api, &ctx.token, "Sorry, something went wrong!").await;
                        }
                    }
                }
            }
        }
    }
    
    async fn guild_create(&self, _ctx: Context, guild: Guild) {
        info!("📥 Joined guild: {}", guild.name.as_deref().unwrap_or("Unknown"));
    }
    
    async fn guild_delete(&self, _ctx: Context, guild: Guild) {
        info!("📤 Left guild: {}", guild.name.as_deref().unwrap_or("Unknown"));
    }
    
    async fn error(&self, error: BotError) {
        match error {
            BotError::Network(ref e) => {
                warn!("🌐 Network error: {}", e);
            }
            BotError::RateLimited(ref info) => {
                warn!("⏰ Rate limited for {} seconds", info.retry_after);
            }
            BotError::Authentication(ref e) => {
                error!("🔐 Authentication error: {}", e);
            }
            _ => {
                error!("❌ Unexpected error: {}", error);
            }
        }
    }
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Initialize logging
    tracing_subscriber::fmt()
        .with_env_filter("botrs=info,comprehensive_bot=info")
        .init();
    
    // Load configuration
    let token = Token::new(
        std::env::var("QQ_BOT_APP_ID")?,
        std::env::var("QQ_BOT_SECRET")?,
    );
    
    // Configure intents
    let intents = Intents::default()
        .with_public_guild_messages()
        .with_guilds();
    
    // Create and start bot
    let mut client = Client::new(token, intents, ComprehensiveBot::new(), false)?;
    
    info!("🚀 Starting comprehensive bot...");
    client.start().await?;
    
    Ok(())
}
```

This example demonstrates:
- Stateful event handling with user statistics
- Command processing with error handling
- Proper logging and monitoring
- Resource management with async operations
- Comprehensive event coverage

## Next Steps

- [Messages & Responses]./messages.md - Learn about sending different types of messages
- [Intents System]./intents.md - Understand event filtering and permissions
- [Configuration]./configuration.md - Advanced configuration options
- [Error Handling]./error-handling.md - Robust error handling patterns