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
# Event Handling Example

This example demonstrates comprehensive event handling patterns for QQ Guild bots using BotRS.

## Overview

Event handling is the core of any bot application. BotRS provides a rich set of events that allow your bot to respond to various activities in guilds, channels, and direct messages. This example shows how to implement robust event handlers for different scenarios.

## Basic Event Handler

```rust
use botrs::{
    Client, Context, EventHandler, Intents, Message, Ready, Token, BotError,
    Guild, Channel, Member, DirectMessage, GroupMessage, C2CMessage,
    MessageAudit, PublicAudio, OpenThread
};
use async_trait::async_trait;
use tracing::{info, warn, error, debug};

struct ComprehensiveBot {
    startup_time: std::time::Instant,
}

impl ComprehensiveBot {
    pub fn new() -> Self {
        Self {
            startup_time: std::time::Instant::now(),
        }
    }
}

#[async_trait]
impl EventHandler for ComprehensiveBot {
    async fn ready(&self, ctx: Context, ready: Ready) {
        info!("πŸ€– Bot is ready!");
        info!("πŸ‘€ Logged in as: {}", ready.user.username);
        info!("πŸ†” Session ID: {}", ready.session_id);
        info!("πŸ”§ Gateway version: {}", ready.version);
        
        if let Some(shard) = ready.shard {
            info!("πŸ”€ Shard: {}/{}", shard[0], shard[1]);
        }

        // Set bot status or perform initialization tasks
        let startup_duration = self.startup_time.elapsed();
        info!("⚑ Startup completed in {:?}", startup_duration);
    }

    async fn message_create(&self, ctx: Context, message: Message) {
        // Ignore bot messages to prevent loops
        if message.is_from_bot() {
            return;
        }

        debug!(
            "πŸ“¨ Message received in channel {} from user {:?}",
            message.channel_id,
            message.author.as_ref().map(|a| &a.id)
        );

        if let Some(content) = &message.content {
            // Log message for monitoring
            info!("πŸ’¬ [{}] {}", message.channel_id, content);

            // Handle different message types
            match content.trim() {
                "!ping" => {
                    let _ = message.reply(&ctx.api, &ctx.token, "πŸ“ Pong!").await;
                }
                "!server" => {
                    self.handle_server_info(&ctx, &message).await;
                }
                "!channels" => {
                    self.handle_channel_list(&ctx, &message).await;
                }
                _ if content.starts_with("!echo ") => {
                    let echo_text = &content[6..];
                    let _ = message.reply(&ctx.api, &ctx.token, echo_text).await;
                }
                _ => {
                    // Handle other message patterns
                    self.handle_general_message(&ctx, &message, content).await;
                }
            }
        }

        // Handle messages with attachments
        if message.has_attachments() {
            self.handle_message_attachments(&ctx, &message).await;
        }

        // Handle mentions
        if message.has_mentions() {
            self.handle_message_mentions(&ctx, &message).await;
        }
    }

    async fn direct_message_create(&self, ctx: Context, message: DirectMessage) {
        info!(
            "πŸ“© Direct message received from user {:?}",
            message.author.as_ref().map(|a| &a.id)
        );

        if let Some(content) = &message.content {
            // Handle DM-specific commands
            match content.trim() {
                "!help" => {
                    let help_text = "πŸ†˜ **Bot Help (Direct Message)**\n\n\
                        Available commands:\n\
                        β€’ `!help` - Show this help message\n\
                        β€’ `!status` - Show bot status\n\
                        β€’ `!support` - Get support information";
                    
                    let _ = message.reply(&ctx.api, &ctx.token, help_text).await;
                }
                "!status" => {
                    let uptime = self.startup_time.elapsed();
                    let status = format!(
                        "πŸ€– **Bot Status**\n\n\
                         Status: βœ… Online\n\
                         Uptime: {:?}\n\
                         Ready for commands!",
                        uptime
                    );
                    let _ = message.reply(&ctx.api, &ctx.token, &status).await;
                }
                _ => {
                    let _ = message.reply(
                        &ctx.api, 
                        &ctx.token, 
                        "πŸ‘‹ Hello! Send `!help` for available commands."
                    ).await;
                }
            }
        }
    }

    async fn group_message_create(&self, ctx: Context, message: GroupMessage) {
        info!(
            "πŸ‘₯ Group message received in group {:?}",
            message.group_openid
        );

        if let Some(content) = &message.content {
            // Handle group-specific logic
            if content.contains("ζœΊε™¨δΊΊ") || content.contains("bot") {
                let response = "πŸ€– ζœ‰δΊΊεœ¨ε«ζˆ‘ε—οΌŸζˆ‘ζ˜― QQ ηΎ€ζœΊε™¨δΊΊοΌ";
                let _ = message.reply(&ctx.api, &ctx.token, response).await;
            }
        }
    }

    async fn c2c_message_create(&self, ctx: Context, message: C2CMessage) {
        info!("πŸ’¬ C2C message received");
        
        if let Some(content) = &message.content {
            // Handle C2C message logic
            let response = format!("ζ”Άεˆ°δ½ ηš„ C2C 梈息: {}", content);
            let _ = message.reply(&ctx.api, &ctx.token, &response).await;
        }
    }

    async fn guild_create(&self, ctx: Context, guild: Guild) {
        info!("🏰 Joined guild: {} (ID: {})", guild.name, guild.id);
        
        // Get guild statistics
        if let Ok(channels) = ctx.get_channels(&guild.id).await {
            info!("πŸ“Š Guild {} has {} channels", guild.name, channels.len());
        }

        // Welcome message or setup procedures could go here
    }

    async fn guild_update(&self, _ctx: Context, guild: Guild) {
        info!("πŸ”„ Guild updated: {} (ID: {})", guild.name, guild.id);
    }

    async fn guild_delete(&self, _ctx: Context, guild: Guild) {
        warn!("πŸ‘‹ Left guild: {} (ID: {})", guild.name, guild.id);
    }

    async fn channel_create(&self, _ctx: Context, channel: Channel) {
        info!(
            "πŸ“’ Channel created: {} in guild {} (Type: {:?})",
            channel.name, channel.guild_id, channel.channel_type
        );
    }

    async fn channel_update(&self, _ctx: Context, channel: Channel) {
        info!(
            "πŸ”„ Channel updated: {} in guild {}",
            channel.name, channel.guild_id
        );
    }

    async fn channel_delete(&self, _ctx: Context, channel: Channel) {
        warn!(
            "πŸ—‘οΈ Channel deleted: {} in guild {}",
            channel.name, channel.guild_id
        );
    }

    async fn guild_member_add(&self, ctx: Context, member: Member) {
        if let Some(user) = &member.user {
            info!("πŸ‘‹ New member joined: {} (ID: {})", 
                  user.username.as_deref().unwrap_or("Unknown"), user.id);
            
            // Send welcome message (you'd need to determine appropriate channel)
            // self.send_welcome_message(&ctx, &member).await;
        }
    }

    async fn guild_member_update(&self, _ctx: Context, member: Member) {
        if let Some(user) = &member.user {
            debug!("πŸ”„ Member updated: {} (ID: {})", 
                   user.username.as_deref().unwrap_or("Unknown"), user.id);
        }
    }

    async fn guild_member_remove(&self, _ctx: Context, member: Member) {
        if let Some(user) = &member.user {
            info!("πŸ‘‹ Member left: {} (ID: {})", 
                  user.username.as_deref().unwrap_or("Unknown"), user.id);
        }
    }

    async fn message_audit_pass(&self, _ctx: Context, audit: MessageAudit) {
        debug!("βœ… Message audit passed: {}", audit.message_id);
    }

    async fn message_audit_reject(&self, _ctx: Context, audit: MessageAudit) {
        warn!("❌ Message audit rejected: {}", audit.message_id);
    }

    async fn audio_or_live_channel_member_enter(&self, _ctx: Context, audio: PublicAudio) {
        if let (Some(channel_id), Some(user_id)) = (&audio.channel_id, &audio.user_id) {
            info!("🎀 User {} entered audio channel {}", user_id, channel_id);
        }
    }

    async fn audio_or_live_channel_member_exit(&self, _ctx: Context, audio: PublicAudio) {
        if let (Some(channel_id), Some(user_id)) = (&audio.channel_id, &audio.user_id) {
            info!("πŸ”‡ User {} left audio channel {}", user_id, channel_id);
        }
    }

    async fn open_forum_thread_create(&self, _ctx: Context, thread: OpenThread) {
        info!("🧡 New forum thread created: {}", thread.thread_info.title);
    }

    async fn open_forum_thread_update(&self, _ctx: Context, thread: OpenThread) {
        info!("πŸ”„ Forum thread updated: {}", thread.thread_info.title);
    }

    async fn open_forum_thread_delete(&self, _ctx: Context, thread: OpenThread) {
        info!("πŸ—‘οΈ Forum thread deleted: {}", thread.thread_info.title);
    }

    async fn error(&self, error: BotError) {
        error!("πŸ’₯ Bot error occurred: {}", error);
        
        // Implement error recovery logic based on error type
        match error {
            BotError::Network(_) => {
                warn!("🌐 Network error - connection may be unstable");
            }
            BotError::RateLimited(_) => {
                warn!("⏱️ Rate limited - backing off");
            }
            BotError::Authentication(_) => {
                error!("πŸ”’ Authentication error - check credentials");
            }
            _ => {
                warn!("❓ Unhandled error type: {}", error);
            }
        }
    }
}
```

## Advanced Event Handling Patterns

### Event Filtering and Routing

```rust
impl ComprehensiveBot {
    async fn handle_general_message(&self, ctx: &Context, message: &Message, content: &str) {
        // URL detection
        if content.contains("http://") || content.contains("https://") {
            self.handle_url_message(ctx, message, content).await;
        }

        // Question detection
        if content.ends_with('?') || content.contains("how") || content.contains("what") {
            self.handle_question_message(ctx, message, content).await;
        }

        // Keyword monitoring
        let monitored_keywords = ["bug", "issue", "problem", "help"];
        if monitored_keywords.iter().any(|&keyword| content.to_lowercase().contains(keyword)) {
            self.handle_support_request(ctx, message, content).await;
        }

        // Spam detection
        if self.is_potential_spam(content) {
            self.handle_potential_spam(ctx, message).await;
        }
    }

    async fn handle_url_message(&self, ctx: &Context, message: &Message, content: &str) {
        debug!("πŸ”— URL detected in message");
        
        // Extract URLs and validate them
        let urls = self.extract_urls(content);
        for url in urls {
            if self.is_safe_url(&url).await {
                debug!("βœ… Safe URL detected: {}", url);
            } else {
                warn!("⚠️ Potentially unsafe URL: {}", url);
                // Could add reaction or warning
            }
        }
    }

    async fn handle_question_message(&self, ctx: &Context, message: &Message, _content: &str) {
        // Add thinking reaction to show the bot is processing
        // Note: Reaction handling would need appropriate API calls
        debug!("❓ Question detected, processing...");
    }

    async fn handle_support_request(&self, ctx: &Context, message: &Message, content: &str) {
        info!("πŸ†˜ Support request detected: {}", content);
        
        let support_response = "🀝 I see you might need help! \
                               Our support team has been notified. \
                               You can also use `!help` for quick assistance.";
        
        let _ = message.reply(&ctx.api, &ctx.token, support_response).await;
    }

    async fn handle_potential_spam(&self, ctx: &Context, message: &Message) {
        warn!("🚫 Potential spam detected from user {:?}", 
              message.author.as_ref().map(|a| &a.id));
        
        // Log for moderation review
        // Could implement automatic actions based on confidence level
    }

    fn is_potential_spam(&self, content: &str) -> bool {
        // Simple spam detection logic
        let spam_indicators = [
            content.len() > 500 && content.chars().filter(|&c| c.is_uppercase()).count() > content.len() / 3,
            content.contains("FREE") && content.contains("CLICK"),
            content.chars().filter(|&c| c == '!').count() > 5,
        ];
        
        spam_indicators.iter().any(|&indicator| indicator)
    }

    fn extract_urls(&self, content: &str) -> Vec<String> {
        // Simple URL extraction - in practice, use a proper URL parsing library
        content.split_whitespace()
            .filter(|word| word.starts_with("http://") || word.starts_with("https://"))
            .map(|url| url.to_string())
            .collect()
    }

    async fn is_safe_url(&self, _url: &str) -> bool {
        // Implement URL safety checking (domain whitelist, reputation services, etc.)
        true // Simplified for example
    }
}
```

### Message Attachment Handling

```rust
impl ComprehensiveBot {
    async fn handle_message_attachments(&self, ctx: &Context, message: &Message) {
        info!("πŸ“Ž Processing {} attachment(s)", message.attachments.len());

        for (index, attachment) in message.attachments.iter().enumerate() {
            let filename = attachment.filename.as_deref().unwrap_or("unknown");
            let size = attachment.size.unwrap_or(0);
            
            info!("πŸ“„ Attachment {}: {} ({} bytes)", index + 1, filename, size);

            // Handle different file types
            if attachment.is_image() {
                self.handle_image_attachment(ctx, message, attachment).await;
            } else if attachment.is_video() {
                self.handle_video_attachment(ctx, message, attachment).await;
            } else if attachment.is_audio() {
                self.handle_audio_attachment(ctx, message, attachment).await;
            } else {
                self.handle_document_attachment(ctx, message, attachment).await;
            }
        }
    }

    async fn handle_image_attachment(&self, ctx: &Context, message: &Message, attachment: &botrs::models::message::MessageAttachment) {
        info!("πŸ–ΌοΈ Processing image: {:?}", attachment.filename);
        
        if let (Some(width), Some(height)) = (attachment.width, attachment.height) {
            let info = format!(
                "πŸ“Έ **Image Info**\n\
                 Size: {}x{}\n\
                 File size: {} bytes",
                width, height, attachment.size.unwrap_or(0)
            );
            
            let _ = message.reply(&ctx.api, &ctx.token, &info).await;
        }
    }

    async fn handle_video_attachment(&self, ctx: &Context, message: &Message, attachment: &botrs::models::message::MessageAttachment) {
        info!("πŸŽ₯ Processing video: {:?}", attachment.filename);
        
        let info = format!(
            "🎬 **Video received**\n\
             File: {}\n\
             Size: {} bytes",
            attachment.filename.as_deref().unwrap_or("unknown"),
            attachment.size.unwrap_or(0)
        );
        
        let _ = message.reply(&ctx.api, &ctx.token, &info).await;
    }

    async fn handle_audio_attachment(&self, ctx: &Context, message: &Message, attachment: &botrs::models::message::MessageAttachment) {
        info!("🎡 Processing audio: {:?}", attachment.filename);
        
        let info = "🎧 **Audio file received** - Thanks for sharing!";
        let _ = message.reply(&ctx.api, &ctx.token, info).await;
    }

    async fn handle_document_attachment(&self, ctx: &Context, message: &Message, attachment: &botrs::models::message::MessageAttachment) {
        info!("πŸ“‹ Processing document: {:?}", attachment.filename);
        
        let filename = attachment.filename.as_deref().unwrap_or("unknown");
        let extension = std::path::Path::new(filename)
            .extension()
            .and_then(|ext| ext.to_str())
            .unwrap_or("unknown");

        let doc_type = match extension.to_lowercase().as_str() {
            "pdf" => "PDF Document",
            "doc" | "docx" => "Word Document",
            "txt" => "Text File",
            "md" => "Markdown File",
            _ => "Document",
        };

        let info = format!("πŸ“„ **{}** received: {}", doc_type, filename);
        let _ = message.reply(&ctx.api, &ctx.token, &info).await;
    }
}
```

### Server Information Handlers

```rust
impl ComprehensiveBot {
    async fn handle_server_info(&self, ctx: &Context, message: &Message) {
        match ctx.get_guild(&message.guild_id).await {
            Ok(guild) => {
                let channels_result = ctx.get_channels(&guild.id).await;
                let channel_count = channels_result.map(|c| c.len()).unwrap_or(0);

                let info = format!(
                    "🏰 **Server Information**\n\n\
                     **Name:** {}\n\
                     **ID:** {}\n\
                     **Channels:** {}\n\
                     **Members:** {}\n\
                     **Owner:** {}",
                    guild.name,
                    guild.id,
                    channel_count,
                    guild.member_count.unwrap_or(0),
                    if guild.owner { "This bot" } else { &guild.owner_id }
                );

                let _ = message.reply(&ctx.api, &ctx.token, &info).await;
            }
            Err(e) => {
                error!("Failed to get guild info: {}", e);
                let _ = message.reply(&ctx.api, &ctx.token, "❌ Failed to get server information").await;
            }
        }
    }

    async fn handle_channel_list(&self, ctx: &Context, message: &Message) {
        match ctx.get_channels(&message.guild_id).await {
            Ok(channels) => {
                let mut channel_list = String::from("πŸ“‹ **Channel List**\n\n");
                
                for channel in channels.iter().take(10) { // Limit to first 10
                    let channel_type = match channel.channel_type {
                        botrs::models::channel::ChannelType::Text => "πŸ’¬",
                        botrs::models::channel::ChannelType::Voice => "πŸ”Š",
                        botrs::models::channel::ChannelType::Category => "πŸ“",
                        botrs::models::channel::ChannelType::Announcement => "πŸ“’",
                        botrs::models::channel::ChannelType::Forum => "🧡",
                        botrs::models::channel::ChannelType::Live => "πŸŽ₯",
                        botrs::models::channel::ChannelType::Application => "πŸ”§",
                    };
                    
                    channel_list.push_str(&format!("{} {}\n", channel_type, channel.name));
                }

                if channels.len() > 10 {
                    channel_list.push_str(&format!("\n... and {} more channels", channels.len() - 10));
                }

                let _ = message.reply(&ctx.api, &ctx.token, &channel_list).await;
            }
            Err(e) => {
                error!("Failed to get channels: {}", e);
                let _ = message.reply(&ctx.api, &ctx.token, "❌ Failed to get channel list").await;
            }
        }
    }
}
```

### Mention Handling

```rust
impl ComprehensiveBot {
    async fn handle_message_mentions(&self, ctx: &Context, message: &Message) {
        info!("πŸ‘₯ Message contains {} mention(s)", message.mentions.len());

        // Check if bot is mentioned
        if let Some(bot_info) = &ctx.bot_info {
            let bot_mentioned = message.mentions.iter()
                .any(|mention| mention.id == bot_info.id);

            if bot_mentioned {
                self.handle_bot_mention(ctx, message).await;
            }
        }

        // Handle other mentions
        for mention in &message.mentions {
            if let Some(username) = &mention.username {
                debug!("πŸ‘€ User mentioned: {} ({})", username, mention.id);
            }
        }
    }

    async fn handle_bot_mention(&self, ctx: &Context, message: &Message) {
        info!("πŸ€– Bot was mentioned in message");

        let responses = [
            "πŸ‘‹ Hello! You mentioned me!",
            "πŸ€– How can I help you?",
            "πŸ‘€ I'm here! What do you need?",
            "✨ You called?",
        ];

        let response = responses[fastrand::usize(..responses.len())];
        let _ = message.reply(&ctx.api, &ctx.token, response).await;
    }
}
```

## Event Statistics and Monitoring

```rust
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;

#[derive(Debug, Default)]
pub struct EventStatistics {
    pub messages_received: AtomicU64,
    pub commands_processed: AtomicU64,
    pub guilds_joined: AtomicU64,
    pub guilds_left: AtomicU64,
    pub members_joined: AtomicU64,
    pub members_left: AtomicU64,
    pub errors_encountered: AtomicU64,
}

struct MonitoredBot {
    stats: Arc<EventStatistics>,
    startup_time: std::time::Instant,
}

impl MonitoredBot {
    pub fn new() -> Self {
        Self {
            stats: Arc::new(EventStatistics::default()),
            startup_time: std::time::Instant::now(),
        }
    }
}

#[async_trait]
impl EventHandler for MonitoredBot {
    async fn ready(&self, _ctx: Context, ready: Ready) {
        info!("πŸ“Š Monitored bot ready: {}", ready.user.username);
    }

    async fn message_create(&self, ctx: Context, message: Message) {
        self.stats.messages_received.fetch_add(1, Ordering::Relaxed);

        if message.is_from_bot() {
            return;
        }

        if let Some(content) = &message.content {
            if content.starts_with('!') {
                self.stats.commands_processed.fetch_add(1, Ordering::Relaxed);
            }

            if content.trim() == "!stats" {
                self.send_statistics(&ctx, &message).await;
            }
        }
    }

    async fn guild_create(&self, _ctx: Context, guild: Guild) {
        self.stats.guilds_joined.fetch_add(1, Ordering::Relaxed);
        info!("πŸ“ˆ Guild joined: {} (Total: {})", 
              guild.name, 
              self.stats.guilds_joined.load(Ordering::Relaxed));
    }

    async fn guild_delete(&self, _ctx: Context, guild: Guild) {
        self.stats.guilds_left.fetch_add(1, Ordering::Relaxed);
        info!("πŸ“‰ Guild left: {} (Total left: {})", 
              guild.name, 
              self.stats.guilds_left.load(Ordering::Relaxed));
    }

    async fn guild_member_add(&self, _ctx: Context, _member: Member) {
        self.stats.members_joined.fetch_add(1, Ordering::Relaxed);
    }

    async fn guild_member_remove(&self, _ctx: Context, _member: Member) {
        self.stats.members_left.fetch_add(1, Ordering::Relaxed);
    }

    async fn error(&self, error: BotError) {
        self.stats.errors_encountered.fetch_add(1, Ordering::Relaxed);
        error!("πŸ“Š Error recorded: {} (Total errors: {})", 
               error, 
               self.stats.errors_encountered.load(Ordering::Relaxed));
    }
}

impl MonitoredBot {
    async fn send_statistics(&self, ctx: &Context, message: &Message) {
        let uptime = self.startup_time.elapsed();
        
        let stats_message = format!(
            "πŸ“Š **Bot Statistics**\n\n\
             **Uptime:** {:?}\n\
             **Messages Received:** {}\n\
             **Commands Processed:** {}\n\
             **Guilds Joined:** {}\n\
             **Guilds Left:** {}\n\
             **Members Joined:** {}\n\
             **Members Left:** {}\n\
             **Errors Encountered:** {}",
            uptime,
            self.stats.messages_received.load(Ordering::Relaxed),
            self.stats.commands_processed.load(Ordering::Relaxed),
            self.stats.guilds_joined.load(Ordering::Relaxed),
            self.stats.guilds_left.load(Ordering::Relaxed),
            self.stats.members_joined.load(Ordering::Relaxed),
            self.stats.members_left.load(Ordering::Relaxed),
            self.stats.errors_encountered.load(Ordering::Relaxed),
        );

        let _ = message.reply(&ctx.api, &ctx.token, &stats_message).await;
    }
}
```

## Main Application

```rust
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Initialize tracing
    tracing_subscriber::fmt()
        .with_env_filter("botrs=debug,event_handling=info")
        .init();

    info!("πŸš€ Starting comprehensive event handling bot...");

    // Get credentials
    let app_id = std::env::var("QQ_BOT_APP_ID")
        .expect("QQ_BOT_APP_ID environment variable required");
    let secret = std::env::var("QQ_BOT_SECRET")
        .expect("QQ_BOT_SECRET environment variable required");

    // Create and validate token
    let token = Token::new(app_id, secret);
    token.validate()?;

    // Configure intents for comprehensive event handling
    let intents = Intents::default()
        .with_public_guild_messages()
        .with_direct_message()
        .with_guilds()
        .with_guild_members()
        .with_audio_action()
        .with_forum_event()
        .with_message_audit()
        .with_c2c_group_at_messages();

    // Choose your bot implementation
    let handler = ComprehensiveBot::new();
    // let handler = MonitoredBot::new(); // Alternative with statistics

    let mut client = Client::new(token, intents, handler, false)?;

    info!("🎯 Event handling bot starting with comprehensive intents...");
    client.start().await?;

    Ok(())
}
```

## Usage Examples

### Basic Commands

```
# Test basic functionality
!ping

# Get server information
!server

# List channels
!channels

# Get bot statistics (with MonitoredBot)
!stats

# Echo messages
!echo Hello, world!
```

### Event Triggers

- **Message Events**: Send any message to trigger message_create
- **Guild Events**: Add/remove bot from servers
- **Member Events**: Users joining/leaving servers
- **Channel Events**: Creating/updating/deleting channels
- **Audio Events**: Users joining/leaving voice channels
- **Forum Events**: Creating/updating forum threads

## Best Practices

1. **Event Filtering**: Don't process unnecessary events to improve performance
2. **Error Handling**: Always handle errors gracefully in event handlers
3. **Logging**: Use structured logging to track event processing
4. **Rate Limiting**: Be mindful of API rate limits when responding to events
5. **Async Safety**: Use proper async patterns and avoid blocking operations
6. **Resource Management**: Clean up resources and avoid memory leaks
7. **Monitoring**: Track event statistics for performance insights

## Performance Considerations

- **Selective Processing**: Only process events you actually need
- **Batch Operations**: Group similar operations when possible
- **Caching**: Cache frequently accessed data
- **Background Tasks**: Use background tasks for heavy processing
- **Connection Pooling**: Reuse connections when making API calls

## Common Patterns

- **Command Routing**: Route different commands to specific handlers
- **Event Filtering**: Filter events based on content, user, or channel
- **State Management**: Track bot state across different events
- **Error Recovery**: Implement retry logic for failed operations
- **Audit Logging**: Log important events for compliance and debugging

## See Also

- [Command Handler]./command-handler.md - Structured command processing
- [Interactive Messages]./interactive-messages.md - User interaction patterns
- [Error Recovery]./error-recovery.md - Advanced error handling
- [Getting Started]./getting-started.md - Basic bot setup