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
# v0.2.0 Message API Migration Guide

This guide helps you migrate from the old message API (v0.1.x) to the new structured parameter API introduced in v0.2.0. The new API eliminates the confusing multiple `None` parameters and provides a cleaner, more maintainable interface.

## Overview of Changes

### What Changed

- **Structured Parameters**: Introduction of `MessageParams`, `GroupMessageParams`, `C2CMessageParams`, and `DirectMessageParams`
- **Builder Pattern**: Fluent API with methods like `.with_reply()`, `.with_file_image()`, etc.
- **Type Safety**: Better compile-time checks and reduced runtime errors
- **Cleaner Code**: No more long parameter lists with multiple `None` values

### What's Deprecated

The old multi-parameter message methods are deprecated but still functional:

```rust
// DEPRECATED - Don't use in new code
api.post_message(
    token, 
    "channel_id", 
    Some("Hello!"),
    None, None, None, None, None, None, None, None, None
).await?;
```

These methods will be removed in v0.3.0.

## Migration Examples

### Basic Text Messages

**Old API (v0.1.x):**
```rust
// Confusing with many None parameters
api.post_message(
    &token,
    "channel_123",
    Some("Hello, world!"),
    None, None, None, None, None, None, None, None, None
).await?;
```

**New API (v0.2.0+):**
```rust
// Clean and readable
let params = MessageParams::new_text("Hello, world!");
api.post_message_with_params(&token, "channel_123", params).await?;
```

### Reply Messages

**Old API (v0.1.x):**
```rust
// Hard to understand which parameter is the reply
api.post_message(
    &token,
    "channel_123",
    Some("Thanks for your message!"),
    None,
    Some(MessageReference {
        message_id: Some("msg_456".to_string()),
        ..Default::default()
    }),
    None, None, None, None, None, None, None
).await?;
```

**New API (v0.2.0+):**
```rust
// Clear intent with builder pattern
let params = MessageParams::new_text("Thanks for your message!")
    .with_reply("msg_456");
api.post_message_with_params(&token, "channel_123", params).await?;
```

### Embed Messages

**Old API (v0.1.x):**
```rust
// Which parameter is the embed?
let embed = Embed {
    title: Some("News Update".to_string()),
    description: Some("Latest updates from our team".to_string()),
    ..Default::default()
};

api.post_message(
    &token,
    "channel_123",
    Some("Check out this update:"),
    None, None,
    Some(embed),
    None, None, None, None, None, None
).await?;
```

**New API (v0.2.0+):**
```rust
// Self-documenting structure
let embed = Embed {
    title: Some("News Update".to_string()),
    description: Some("Latest updates from our team".to_string()),
    ..Default::default()
};

let params = MessageParams {
    content: Some("Check out this update:".to_string()),
    embed: Some(embed),
    ..Default::default()
};
api.post_message_with_params(&token, "channel_123", params).await?;
```

### File Attachments

**Old API (v0.1.x):**
```rust
// Unclear parameter ordering
api.post_message(
    &token,
    "channel_123",
    Some("Here's your file:"),
    None, None, None,
    Some(image_data),
    None, None, None, None, None
).await?;
```

**New API (v0.2.0+):**
```rust
// Explicit and chainable
let params = MessageParams::new_text("Here's your file:")
    .with_file_image(&image_data);
api.post_message_with_params(&token, "channel_123", params).await?;
```

### Markdown Messages

**Old API (v0.1.x):**
```rust
let markdown = MarkdownPayload {
    content: Some("# Hello\n\nThis is **bold** text".to_string()),
    ..Default::default()
};

api.post_message(
    &token,
    "channel_123",
    None, None, None, None, None,
    Some(markdown),
    None, None, None, None
).await?;
```

**New API (v0.2.0+):**
```rust
let markdown = MarkdownPayload {
    content: Some("# Hello\n\nThis is **bold** text".to_string()),
    ..Default::default()
};

let params = MessageParams {
    markdown: Some(markdown),
    ..Default::default()
};
api.post_message_with_params(&token, "channel_123", params).await?;
```

## Message Type Specific APIs

### Group Messages

**Old API:**
```rust
api.post_group_message(
    &token,
    "group_123",
    Some("Hello group!"),
    None, None, None, None, None, None
).await?;
```

**New API:**
```rust
let params = GroupMessageParams::new_text("Hello group!");
api.post_group_message_with_params(&token, "group_123", params).await?;
```

### C2C (Client-to-Client) Messages

**Old API:**
```rust
api.post_c2c_message(
    &token,
    "user_123",
    Some("Hello there!"),
    None, None, None, None, None, None
).await?;
```

**New API:**
```rust
let params = C2CMessageParams::new_text("Hello there!");
api.post_c2c_message_with_params(&token, "user_123", params).await?;
```

### Direct Messages

**Old API:**
```rust
api.post_dms(
    &token,
    "guild_123",
    Some("Direct message!"),
    None, None, None, None, None, None
).await?;
```

**New API:**
```rust
let params = DirectMessageParams::new_text("Direct message!");
api.post_dms_with_params(&token, "guild_123", params).await?;
```

## Migration Strategies

### Gradual Migration

You can migrate gradually since the old API still works:

```rust
impl EventHandler for MyBot {
    async fn message_create(&self, ctx: Context, msg: Message) {
        // Migrate one command at a time
        match msg.content.as_deref() {
            Some("!new") => {
                // Use new API for new features
                let params = MessageParams::new_text("Using new API!");
                ctx.api.post_message_with_params(&ctx.token, &msg.channel_id, params).await.ok();
            }
            Some("!old") => {
                // Keep old API for now (but plan to migrate)
                ctx.api.post_message(
                    &ctx.token, &msg.channel_id, 
                    Some("Still using old API"), 
                    None, None, None, None, None, None, None, None, None
                ).await.ok();
            }
            _ => {}
        }
    }
}
```

### Helper Functions

Create helper functions to ease migration:

```rust
// Migration helpers
fn simple_text(content: &str) -> MessageParams {
    MessageParams::new_text(content)
}

fn reply_text(content: &str, reply_to: &str) -> MessageParams {
    MessageParams::new_text(content).with_reply(reply_to)
}

fn embed_message(content: &str, embed: Embed) -> MessageParams {
    MessageParams {
        content: Some(content.to_string()),
        embed: Some(embed),
        ..Default::default()
    }
}

// Usage
let params = reply_text("Thanks!", &message_id);
api.post_message_with_params(&token, &channel_id, params).await?;
```

### Bulk Migration Script

For large codebases, consider creating a script to help with migration:

```rust
// Example migration pattern matching
fn migrate_message_call(old_call: &str) -> String {
    // This is a simplified example - real implementation would need
    // proper parsing and AST manipulation
    if old_call.contains("post_message(") && old_call.contains("Some(") {
        // Extract content and transform to new API
        // This would be much more complex in practice
        "MessageParams::new_text(content)".to_string()
    } else {
        old_call.to_string()
    }
}
```

## Event Handler Migration

### Old Event Handling

```rust
impl EventHandler for MyBot {
    async fn message_create(&self, ctx: Context, msg: Message) {
        // Old response pattern
        ctx.api.post_message(
            &ctx.token,
            &msg.channel_id,
            Some("Hello!"),
            None, None, None, None, None, None, None, None, None
        ).await.ok();
    }
}
```

### New Event Handling

```rust
impl EventHandler for MyBot {
    async fn message_create(&self, ctx: Context, msg: Message) {
        // New response pattern
        let params = MessageParams::new_text("Hello!");
        ctx.api.post_message_with_params(&ctx.token, &msg.channel_id, params).await.ok();
    }
}
```

## Builder Pattern Benefits

The new API supports a fluent builder pattern:

```rust
let params = MessageParams::new_text("Check this out!")
    .with_reply(&original_message_id)
    .with_file_image(&image_data)
    .with_embed(my_embed);

api.post_message_with_params(&token, &channel_id, params).await?;
```

This is equivalent to the old API but much more readable:

```rust
// Old equivalent (don't use)
api.post_message(
    &token,
    &channel_id,
    Some("Check this out!"),
    None,
    Some(MessageReference {
        message_id: Some(original_message_id.clone()),
        ..Default::default()
    }),
    Some(my_embed),
    Some(image_data),
    None, None, None, None, None
).await?;
```

## Common Migration Issues

### Issue 1: Parameter Order Confusion

**Problem:**
```rust
// Which parameter is which?
api.post_message(token, channel, content, embed, None, None, None, None, None, None, None, None).await?;
```

**Solution:**
```rust
// Self-documenting
let params = MessageParams {
    content: Some(content),
    embed: Some(embed),
    ..Default::default()
};
api.post_message_with_params(token, channel, params).await?;
```

### Issue 2: Forgetting to Update Method Names

**Problem:**
```rust
// Still using old method name
api.post_message(&token, &channel_id, params).await?; // Wrong!
```

**Solution:**
```rust
// Use the new method name
api.post_message_with_params(&token, &channel_id, params).await?; // Correct!
```

### Issue 3: Mixing Old and New Patterns

**Problem:**
```rust
// Don't mix old None parameters with new struct
api.post_message(
    &token, &channel_id, 
    None, None, None, 
    Some(MessageParams::new_text("Hello")), // Wrong approach
    None, None, None, None, None
).await?;
```

**Solution:**
```rust
// Use the new API consistently
let params = MessageParams::new_text("Hello");
api.post_message_with_params(&token, &channel_id, params).await?;
```

## Testing Your Migration

### Unit Tests

```rust
#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_message_params_creation() {
        let params = MessageParams::new_text("Hello");
        assert_eq!(params.content, Some("Hello".to_string()));
        assert!(params.embed.is_none());
    }

    #[test]
    fn test_reply_message() {
        let params = MessageParams::new_text("Reply")
            .with_reply("msg_123");
        
        assert_eq!(params.content, Some("Reply".to_string()));
        assert!(params.message_reference.is_some());
    }
}
```

### Integration Tests

```rust
#[tokio::test]
async fn test_new_api_integration() {
    let params = MessageParams::new_text("Test message");
    
    // Test with mock API
    let result = mock_api.post_message_with_params(&token, &channel_id, params).await;
    assert!(result.is_ok());
}
```

## Performance Considerations

The new API has several performance benefits:

1. **Reduced Allocations**: Fewer `Option<T>` allocations for unused parameters
2. **Better Caching**: Structured parameters are easier to cache and reuse
3. **Compile-time Optimization**: Better dead code elimination for unused fields

## Backwards Compatibility

- Old API methods are marked as `#[deprecated]` but still functional
- No breaking changes in v0.2.x series
- Old methods will be removed in v0.3.0
- Compile-time warnings guide migration

## Timeline and Support

- **v0.2.0**: New API introduced, old API deprecated
- **v0.2.x**: Both APIs supported with deprecation warnings
- **v0.3.0**: Old API removed (planned)

## Getting Help

If you encounter issues during migration:

1. Check the [examples directory]/examples/getting-started for usage patterns
2. Review the [API documentation]/api/client
3. Open an issue on [GitHub]https://github.com/YinMo19/botrs/issues
4. Join our community discussions

## Conclusion

The v0.2.0 message API provides a cleaner, more maintainable way to send messages. While migration requires some work, the benefits include:

- **Better Code Readability**: Self-documenting parameter names
- **Reduced Errors**: Type safety and clear intent
- **Easier Maintenance**: Structured parameters are easier to modify
- **Future-Proof**: Foundation for upcoming features

Start migrating incrementally and take advantage of the migration helpers to make the process smooth.