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
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
# Command Handler Example

This example demonstrates how to build a robust command handling system for your QQ Guild bot using BotRS.

## Overview

A command handler provides a structured way to process user commands, validate permissions, and organize bot functionality. This example shows how to create a flexible command system that can be easily extended.

## Basic Command Structure

```rust
use botrs::{Client, Context, EventHandler, Intents, Message, Ready, Token, BotError};
use std::collections::HashMap;
use async_trait::async_trait;

#[derive(Clone)]
pub struct Command {
    pub name: String,
    pub description: String,
    pub usage: String,
    pub min_args: usize,
    pub max_args: Option<usize>,
    pub requires_permission: Option<String>,
}

pub struct CommandHandler {
    pub commands: HashMap<String, Command>,
    pub prefix: String,
}

impl CommandHandler {
    pub fn new(prefix: &str) -> Self {
        let mut handler = Self {
            commands: HashMap::new(),
            prefix: prefix.to_string(),
        };
        
        // Register built-in commands
        handler.register_default_commands();
        handler
    }
    
    fn register_default_commands(&mut self) {
        self.register_command(Command {
            name: "ping".to_string(),
            description: "Test bot responsiveness".to_string(),
            usage: "!ping".to_string(),
            min_args: 0,
            max_args: Some(0),
            requires_permission: None,
        });
        
        self.register_command(Command {
            name: "help".to_string(),
            description: "Show available commands".to_string(),
            usage: "!help [command]".to_string(),
            min_args: 0,
            max_args: Some(1),
            requires_permission: None,
        });
        
        self.register_command(Command {
            name: "echo".to_string(),
            description: "Echo back the provided text".to_string(),
            usage: "!echo <text>".to_string(),
            min_args: 1,
            max_args: None,
            requires_permission: None,
        });
        
        self.register_command(Command {
            name: "kick".to_string(),
            description: "Kick a member from the guild".to_string(),
            usage: "!kick <@user> [reason]".to_string(),
            min_args: 1,
            max_args: None,
            requires_permission: Some("kick_members".to_string()),
        });
        
        self.register_command(Command {
            name: "mute".to_string(),
            description: "Mute a member in voice channels".to_string(),
            usage: "!mute <@user> [duration_seconds]".to_string(),
            min_args: 1,
            max_args: Some(2),
            requires_permission: Some("manage_channels".to_string()),
        });
    }
    
    pub fn register_command(&mut self, command: Command) {
        self.commands.insert(command.name.clone(), command);
    }
    
    pub fn parse_command(&self, content: &str) -> Option<ParsedCommand> {
        if !content.starts_with(&self.prefix) {
            return None;
        }
        
        let without_prefix = &content[self.prefix.len()..];
        let parts: Vec<&str> = without_prefix.split_whitespace().collect();
        
        if parts.is_empty() {
            return None;
        }
        
        Some(ParsedCommand {
            name: parts[0].to_lowercase(),
            args: parts[1..].iter().map(|s| s.to_string()).collect(),
            raw_args: if parts.len() > 1 {
                without_prefix[parts[0].len()..].trim().to_string()
            } else {
                String::new()
            },
        })
    }
}

#[derive(Debug)]
pub struct ParsedCommand {
    pub name: String,
    pub args: Vec<String>,
    pub raw_args: String,
}
```

## Bot Implementation

```rust
pub struct CommandBot {
    command_handler: CommandHandler,
}

impl CommandBot {
    pub fn new() -> Self {
        Self {
            command_handler: CommandHandler::new("!"),
        }
    }
}

#[async_trait]
impl EventHandler for CommandBot {
    async fn ready(&self, _ctx: Context, ready: Ready) {
        println!("Command bot is ready! Logged in as: {}", ready.user.username);
        println!("Commands available with prefix: {}", self.command_handler.prefix);
    }
    
    async fn message_create(&self, ctx: Context, message: Message) {
        // Ignore bot messages
        if message.is_from_bot() {
            return;
        }
        
        // Get message content
        let content = match &message.content {
            Some(content) => content,
            None => return,
        };
        
        // Parse command
        let parsed_command = match self.command_handler.parse_command(content) {
            Some(cmd) => cmd,
            None => return, // Not a command
        };
        
        // Get command definition
        let command = match self.command_handler.commands.get(&parsed_command.name) {
            Some(cmd) => cmd,
            None => {
                let _ = message.reply(
                    &ctx.api,
                    &ctx.token,
                    &format!("Unknown command: `{}`. Use `{}help` for available commands.", 
                            parsed_command.name, self.command_handler.prefix)
                ).await;
                return;
            }
        };
        
        // Validate arguments
        if let Err(error_msg) = self.validate_command_args(command, &parsed_command) {
            let _ = message.reply(&ctx.api, &ctx.token, &error_msg).await;
            return;
        }
        
        // Check permissions if required
        if let Some(required_perm) = &command.requires_permission {
            if let Err(error_msg) = self.check_permission(&ctx, &message, required_perm).await {
                let _ = message.reply(&ctx.api, &ctx.token, &error_msg).await;
                return;
            }
        }
        
        // Execute command
        if let Err(e) = self.execute_command(&ctx, &message, command, &parsed_command).await {
            eprintln!("Command execution failed: {}", e);
            let _ = message.reply(
                &ctx.api,
                &ctx.token,
                "An error occurred while executing the command."
            ).await;
        }
    }
    
    async fn error(&self, error: BotError) {
        eprintln!("Bot error: {}", error);
    }
}

impl CommandBot {
    fn validate_command_args(&self, command: &Command, parsed: &ParsedCommand) -> Result<(), String> {
        let arg_count = parsed.args.len();
        
        if arg_count < command.min_args {
            return Err(format!(
                "Not enough arguments. Usage: `{}`",
                command.usage
            ));
        }
        
        if let Some(max_args) = command.max_args {
            if arg_count > max_args {
                return Err(format!(
                    "Too many arguments. Usage: `{}`",
                    command.usage
                ));
            }
        }
        
        Ok(())
    }
    
    async fn check_permission(
        &self,
        ctx: &Context,
        message: &Message,
        required_permission: &str,
    ) -> Result<(), String> {
        // Get the user ID from the message author
        let user_id = match &message.author {
            Some(author) => &author.id,
            None => return Err("Cannot determine user permissions".to_string()),
        };
        
        // Check user permissions (simplified)
        match ctx.get_channel_user_permissions(&message.channel_id, user_id).await {
            Ok(permissions) => {
                if self.has_permission(&permissions.permissions, required_permission) {
                    Ok(())
                } else {
                    Err(format!("You don't have the required permission: {}", required_permission))
                }
            }
            Err(_) => Err("Failed to check permissions".to_string()),
        }
    }
    
    fn has_permission(&self, permissions: &str, required: &str) -> bool {
        // Simplified permission checking
        let perm_value: u64 = permissions.parse().unwrap_or(0);
        let required_bit = match required {
            "kick_members" => 1 << 1,
            "manage_channels" => 1 << 4,
            "manage_messages" => 1 << 13,
            _ => return false,
        };
        (perm_value & required_bit) != 0
    }
    
    async fn execute_command(
        &self,
        ctx: &Context,
        message: &Message,
        command: &Command,
        parsed: &ParsedCommand,
    ) -> Result<(), BotError> {
        match command.name.as_str() {
            "ping" => self.handle_ping(ctx, message).await,
            "help" => self.handle_help(ctx, message, parsed).await,
            "echo" => self.handle_echo(ctx, message, parsed).await,
            "kick" => self.handle_kick(ctx, message, parsed).await,
            "mute" => self.handle_mute(ctx, message, parsed).await,
            _ => {
                message.reply(ctx.api, ctx.token, "Command not implemented yet.").await?;
                Ok(())
            }
        }
    }
    
    async fn handle_ping(&self, ctx: &Context, message: &Message) -> Result<(), BotError> {
        let start_time = std::time::Instant::now();
        let response = message.reply(&ctx.api, &ctx.token, "Pong! 🏓").await?;
        let latency = start_time.elapsed();
        
        // Edit the response to include latency (if API supports editing)
        let latency_msg = format!("Pong! 🏓 Latency: {:?}", latency);
        message.reply(&ctx.api, &ctx.token, &latency_msg).await?;
        
        Ok(())
    }
    
    async fn handle_help(&self, ctx: &Context, message: &Message, parsed: &ParsedCommand) -> Result<(), BotError> {
        if parsed.args.is_empty() {
            // Show all commands
            let mut help_text = format!("**Available Commands** (prefix: `{}`)\n\n", self.command_handler.prefix);
            
            for (_, command) in &self.command_handler.commands {
                help_text.push_str(&format!(
                    "**{}** - {}\n",
                    command.name,
                    command.description
                ));
            }
            
            help_text.push_str(&format!("\nUse `{}help <command>` for detailed usage information.", self.command_handler.prefix));
            message.reply(&ctx.api, &ctx.token, &help_text).await?;
        } else {
            // Show specific command help
            let command_name = &parsed.args[0];
            
            match self.command_handler.commands.get(command_name) {
                Some(command) => {
                    let help_text = format!(
                        "**{}**\n\n**Description:** {}\n**Usage:** `{}`{}",
                        command.name,
                        command.description,
                        command.usage,
                        if let Some(perm) = &command.requires_permission {
                            format!("\n**Required Permission:** {}", perm)
                        } else {
                            String::new()
                        }
                    );
                    message.reply(&ctx.api, &ctx.token, &help_text).await?;
                }
                None => {
                    message.reply(
                        &ctx.api,
                        &ctx.token,
                        &format!("Command `{}` not found.", command_name)
                    ).await?;
                }
            }
        }
        
        Ok(())
    }
    
    async fn handle_echo(&self, ctx: &Context, message: &Message, parsed: &ParsedCommand) -> Result<(), BotError> {
        let echo_text = if parsed.raw_args.is_empty() {
            "Echo! (no text provided)".to_string()
        } else {
            parsed.raw_args.clone()
        };
        
        message.reply(&ctx.api, &ctx.token, &echo_text).await?;
        Ok(())
    }
    
    async fn handle_kick(&self, ctx: &Context, message: &Message, parsed: &ParsedCommand) -> Result<(), BotError> {
        // Extract user ID from mention (simplified)
        let user_mention = &parsed.args[0];
        let user_id = self.extract_user_id_from_mention(user_mention)?;
        
        let reason = if parsed.args.len() > 1 {
            parsed.args[1..].join(" ")
        } else {
            "No reason provided".to_string()
        };
        
        // Kick the member
        match ctx.kick_member(&message.guild_id, &user_id, Some(1), Some(&reason)).await {
            Ok(_) => {
                message.reply(
                    &ctx.api,
                    &ctx.token,
                    &format!("User {} has been kicked. Reason: {}", user_mention, reason)
                ).await?;
            }
            Err(e) => {
                message.reply(
                    &ctx.api,
                    &ctx.token,
                    &format!("Failed to kick user: {}", e)
                ).await?;
            }
        }
        
        Ok(())
    }
    
    async fn handle_mute(&self, ctx: &Context, message: &Message, parsed: &ParsedCommand) -> Result<(), BotError> {
        let user_mention = &parsed.args[0];
        let user_id = self.extract_user_id_from_mention(user_mention)?;
        
        let duration = if parsed.args.len() > 1 {
            parsed.args[1].parse::<u64>().unwrap_or(300) // Default 5 minutes
        } else {
            300
        };
        
        // Find a voice channel (simplified - in practice you'd track voice states)
        // For this example, we'll assume the message channel is voice-capable
        match ctx.mute_member(&message.channel_id, &user_id, Some(duration), Some("Muted by command")).await {
            Ok(_) => {
                message.reply(
                    &ctx.api,
                    &ctx.token,
                    &format!("User {} has been muted for {} seconds.", user_mention, duration)
                ).await?;
            }
            Err(e) => {
                message.reply(
                    &ctx.api,
                    &ctx.token,
                    &format!("Failed to mute user: {}", e)
                ).await?;
            }
        }
        
        Ok(())
    }
    
    fn extract_user_id_from_mention(&self, mention: &str) -> Result<String, BotError> {
        // Parse user mentions like <@123456789> or <@!123456789>
        if mention.starts_with("<@") && mention.ends_with('>') {
            let id_part = mention.trim_start_matches("<@").trim_start_matches('!').trim_end_matches('>');
            if id_part.chars().all(|c| c.is_ascii_digit()) {
                Ok(id_part.to_string())
            } else {
                Err(BotError::InvalidInput("Invalid user mention format".to_string()))
            }
        } else {
            Err(BotError::InvalidInput("Please mention a user with @".to_string()))
        }
    }
}
```

## Main Function

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

    println!("Starting command handler 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 token and validate
    let token = Token::new(app_id, secret);
    token.validate()?;

    // Set up intents
    let intents = Intents::default()
        .with_public_guild_messages()
        .with_guilds()
        .with_guild_members();

    // Create bot with command handler
    let handler = CommandBot::new();
    let mut client = Client::new(token, intents, handler, false)?;

    println!("Command bot starting...");
    client.start().await?;

    Ok(())
}
```

## Advanced Features

### Custom Command Registration

```rust
impl CommandBot {
    pub fn register_custom_command(&mut self, command: Command) {
        self.command_handler.register_command(command);
    }
    
    pub fn create_custom_commands(&mut self) {
        // Server info command
        self.register_custom_command(Command {
            name: "serverinfo".to_string(),
            description: "Display server information".to_string(),
            usage: "!serverinfo".to_string(),
            min_args: 0,
            max_args: Some(0),
            requires_permission: None,
        });
        
        // User info command
        self.register_custom_command(Command {
            name: "userinfo".to_string(),
            description: "Display user information".to_string(),
            usage: "!userinfo [@user]".to_string(),
            min_args: 0,
            max_args: Some(1),
            requires_permission: None,
        });
        
        // Clear messages command
        self.register_custom_command(Command {
            name: "clear".to_string(),
            description: "Clear recent messages".to_string(),
            usage: "!clear <count>".to_string(),
            min_args: 1,
            max_args: Some(1),
            requires_permission: Some("manage_messages".to_string()),
        });
    }
}
```

### Cooldown System

```rust
use std::collections::HashMap;
use std::time::{Duration, Instant};

pub struct CooldownManager {
    cooldowns: HashMap<String, Instant>,
}

impl CooldownManager {
    pub fn new() -> Self {
        Self {
            cooldowns: HashMap::new(),
        }
    }
    
    pub fn check_cooldown(&mut self, key: &str, duration: Duration) -> bool {
        let now = Instant::now();
        
        if let Some(&last_used) = self.cooldowns.get(key) {
            if now.duration_since(last_used) < duration {
                return false; // Still on cooldown
            }
        }
        
        self.cooldowns.insert(key.to_string(), now);
        true
    }
    
    pub fn get_remaining_cooldown(&self, key: &str, duration: Duration) -> Option<Duration> {
        if let Some(&last_used) = self.cooldowns.get(key) {
            let elapsed = Instant::now().duration_since(last_used);
            if elapsed < duration {
                return Some(duration - elapsed);
            }
        }
        None
    }
}
```

### Command Middleware

```rust
pub trait CommandMiddleware {
    async fn before_command(
        &self,
        ctx: &Context,
        message: &Message,
        command: &Command,
    ) -> Result<bool, BotError>; // Return false to stop execution
    
    async fn after_command(
        &self,
        ctx: &Context,
        message: &Message,
        command: &Command,
        result: &Result<(), BotError>,
    );
}

pub struct LoggingMiddleware;

#[async_trait]
impl CommandMiddleware for LoggingMiddleware {
    async fn before_command(
        &self,
        _ctx: &Context,
        message: &Message,
        command: &Command,
    ) -> Result<bool, BotError> {
        println!(
            "Executing command '{}' from user {} in channel {}",
            command.name,
            message.author.as_ref().map(|a| &a.id).unwrap_or(&"unknown".to_string()),
            message.channel_id
        );
        Ok(true)
    }
    
    async fn after_command(
        &self,
        _ctx: &Context,
        _message: &Message,
        command: &Command,
        result: &Result<(), BotError>,
    ) {
        match result {
            Ok(_) => println!("Command '{}' executed successfully", command.name),
            Err(e) => println!("Command '{}' failed: {}", command.name, e),
        }
    }
}
```

## Usage Examples

### Basic Commands

```
# Test bot responsiveness
!ping

# Get help
!help
!help ping

# Echo text
!echo Hello, world!

# Server moderation (requires permissions)
!kick @user Spamming
!mute @user 300
```

### Error Handling

The command handler includes comprehensive error handling:

- **Invalid syntax**: Shows usage information
- **Missing permissions**: Informs user about required permissions
- **Invalid arguments**: Validates argument count and format
- **API failures**: Gracefully handles and reports errors

## Best Practices

1. **Command Validation**: Always validate arguments before execution
2. **Permission Checks**: Verify user permissions for moderation commands
3. **Error Handling**: Provide user-friendly error messages
4. **Logging**: Log command execution for debugging and audit purposes
5. **Rate Limiting**: Implement cooldowns to prevent spam
6. **Extensibility**: Design the system to easily add new commands

## See Also

- [Getting Started]./getting-started.md - Basic bot setup
- [Rich Messages]./rich-messages.md - Advanced message formatting
- [Event Handling]./event-handling.md - Comprehensive event processing
- [Error Recovery]./error-recovery.md - Advanced error handling patterns