presenceforge 0.1.0

A library for Discord Rich Presence (IPC) integration
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
# API Reference

API reference for PresenceForge (work-in-progress; APIs may change).

> **Note:** PresenceForge v0.1.0 is an early development release.  
> It’s functional, but features may change or be incomplete.

## Table of Contents

- [DiscordIpcClient]#discordipclient
- [ActivityBuilder]#activitybuilder
- [Activity]#activity
- [PipeConfig]#pipeconfig
- [IpcConnection]#ipcconnection
- [Error Types]#error-types
- [Async Clients]#async-clients

---

## DiscordIpcClient

The synchronous Discord IPC client for managing Rich Presence.

### Creating a Client

#### `DiscordIpcClient::new(client_id: impl Into<String>) -> Result<Self>`

Creates a new Discord IPC client with automatic pipe discovery.

```rust
use presenceforge::sync::DiscordIpcClient;

let client = DiscordIpcClient::new("your_client_id")?;
```

**Parameters:**

- `client_id` - Your Discord Application ID (from Developer Portal)

**Returns:** `Result<DiscordIpcClient, DiscordIpcError>`

**Errors:**

- `DiscordIpcError::ConnectionFailed` - Could not find or connect to Discord

---

#### `DiscordIpcClient::new_with_config(client_id: impl Into<String>, config: Option<PipeConfig>) -> Result<Self>`

Creates a new Discord IPC client with custom pipe configuration.

```rust
use presenceforge::PipeConfig;
use presenceforge::sync::DiscordIpcClient;

// Auto-discovery (equivalent to ::new())
let client = DiscordIpcClient::new_with_config("client_id", None)?;

// Connect to specific pipe path
let client = DiscordIpcClient::new_with_config(
    "client_id",
    Some(PipeConfig::CustomPath("/tmp/discord-ipc-0".to_string()))
)?;
```

**Parameters:**

- `client_id` - Your Discord Application ID
- `config` - Optional pipe configuration. If `None`, uses auto-discovery

**Returns:** `Result<DiscordIpcClient, DiscordIpcError>`

---

### Connection Methods

#### `connect(&mut self) -> Result<serde_json::Value>`

Establishes connection and performs handshake with Discord.

```rust
let mut client = DiscordIpcClient::new("client_id")?;
let _handshake = client.connect()?; // JSON handshake payload
```

**Must be called before** setting or clearing activities.

**Returns:** `Result<serde_json::Value, DiscordIpcError>`

**Errors:**

- `DiscordIpcError::ConnectionFailed` - Could not connect to IPC
- `DiscordIpcError::HandshakeFailed` - Invalid response/opcode or error from Discord

---

// (No sync reconnect method)
// If the connection is lost, recreate the client and call connect() again.

---

### Activity Methods

#### `set_activity(&mut self, activity: &Activity) -> Result<()>`

Sets the current Rich Presence activity.

```rust
use presenceforge::ActivityBuilder;

let activity = ActivityBuilder::new()
    .state("Playing")
    .details("Main Menu")
    .build();

client.set_activity(&activity)?;
```

**Parameters:**

- `activity` - The activity to display (typically built with `ActivityBuilder`)

**Returns:** `Result<(), DiscordIpcError>`

**Note:** You can call this multiple times to update the presence.

---

#### `clear_activity(&mut self) -> Result<serde_json::Value>`

Clears the current Rich Presence activity.

```rust
let _resp = client.clear_activity()?;
```

**Returns:** `Result<serde_json::Value, DiscordIpcError>`

**Note:** This removes the Rich Presence from your profile entirely.

---

## ActivityBuilder

Builder pattern for creating Rich Presence activities.

### Creating a Builder

#### `ActivityBuilder::new() -> Self`

Creates a new activity builder.

```rust
use presenceforge::ActivityBuilder;

let activity = ActivityBuilder::new()
    .state("Hello!")
    .build();
```

---

### Text Methods

#### `state(self, state: impl Into<String>) -> Self`

Sets the state text (smaller text, first line).

```rust
.state("In a Match")
```

**Character limit:** 128 characters

---

#### `details(self, details: impl Into<String>) -> Self`

Sets the details text (larger text, second line).

```rust
.details("Competitive Mode")
```

**Character limit:** 128 characters

---

### Image Methods

#### `large_image(self, key: impl Into<String>) -> Self`

Sets the large image by asset key.

```rust
.large_image("game_logo")
```

**Note:** Asset keys must be uploaded to Discord Developer Portal under Rich Presence → Art Assets.

---

#### `large_text(self, text: impl Into<String>) -> Self`

Sets the hover text for the large image.

```rust
.large_text("My Awesome Game v1.0")
```

---

#### `small_image(self, key: impl Into<String>) -> Self`

Sets the small image (circular overlay on large image).

```rust
.small_image("character_icon")
```

---

#### `small_text(self, text: impl Into<String>) -> Self`

Sets the hover text for the small image.

```rust
.small_text("Level 50 Warrior")
```

---

### Timestamp Methods

#### `start_timestamp(self, timestamp: u64) -> Self`

Sets the start timestamp (shows elapsed time).

```rust
use std::time::{SystemTime, UNIX_EPOCH};

let now = SystemTime::now()
    .duration_since(UNIX_EPOCH)
    .unwrap()
    .as_secs();

.start_timestamp(now)
```

**Parameter:** Unix timestamp in seconds

**Display:** Shows as "XX:XX elapsed" in Discord

---

#### `start_timestamp_now(self) -> Result<Self>`

Sets the start timestamp to the current time.

```rust
.start_timestamp_now()?  // returns Result<Self>
```

**Equivalent to:**

```rust
.start_timestamp(SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs())
```

---

#### `end_timestamp(self, timestamp: i64) -> Self`

Sets the end timestamp (shows remaining time).

```rust
use std::time::{SystemTime, UNIX_EPOCH};

let in_10_min = SystemTime::now()
    .duration_since(UNIX_EPOCH)
    .unwrap()
    .as_secs() as i64 + 600;

.end_timestamp(in_10_min)
```

**Parameter:** Unix timestamp in seconds

**Display:** Shows as "XX:XX left" in Discord

---

### Button Methods

#### `button(self, label: impl Into<String>, url: impl Into<String>) -> Self`

Adds a button to the Rich Presence (max 2 buttons).

```rust
.button("Watch Stream", "https://twitch.tv/username")
.button("GitHub", "https://github.com/username")
```

**Parameters:**

- `label` - Button text (max 32 characters)
- `url` - URL to open when clicked

**Note:** Only the first 2 buttons will be displayed.

---

### Party Methods

#### `party(self, id: impl Into<String>, current_size: u32, max_size: u32) -> Self`

Sets the party information (ID and size) in a single method.

```rust
.party("party_12345", 2, 4)  // Shows "2 of 4"
```

**Parameters:**

- `id` - Unique party identifier for grouping players
- `current_size` - Current number of players
- `max_size` - Maximum number of players

---

### Secret Methods

**Note:** Secrets are for join/spectate invitations (advanced feature, partial implementation).

#### `match_secret(self, secret: impl Into<String>) -> Self`

Sets the match secret for joining.

```rust
.match_secret("match_secret_xyz")
```

---

#### `join_secret(self, secret: impl Into<String>) -> Self`

Sets the join secret.

```rust
.join_secret("join_secret_abc")
```

---

#### `spectate_secret(self, secret: impl Into<String>) -> Self`

Sets the spectate secret.

```rust
.spectate_secret("spectate_secret_def")
```

---

### Other Methods

#### `instance(self, is_instance: bool) -> Self`

Sets whether this is a game instance.

```rust
.instance(true)
```

**Default:** `false`

---

#### `build(self) -> Activity`

Builds the final `Activity` object.

```rust
let activity = ActivityBuilder::new()
    .state("Hello")
    .build();
```

**Returns:** `Activity` ready to be sent to Discord

---

## Activity

Represents a Rich Presence activity. Typically created via `ActivityBuilder`.

### Manual Creation

```rust
use presenceforge::activity::{Activity, Assets, Timestamps};

let activity = Activity {
    state: Some("Playing".to_string()),
    details: Some("Main Menu".to_string()),
    timestamps: Some(Timestamps {
        start: Some(1234567890),
        end: None,
    }),
    assets: Some(Assets {
        large_image: Some("logo".to_string()),
        large_text: Some("Game".to_string()),
        small_image: None,
        small_text: None,
    }),
    ..Default::default()
};
```

**Recommendation:** Use `ActivityBuilder` instead of manual creation.

---

## PipeConfig

Configuration for Discord IPC pipe selection.

### Variants

#### `PipeConfig::Auto`

Automatically discovers and connects to the first available Discord pipe.

```rust
use presenceforge::PipeConfig;

let config = PipeConfig::Auto;
```

**Default behavior** when using `DiscordIpcClient::new()`.

---

#### `PipeConfig::CustomPath(String)`

Connects to a specific pipe path.

```rust
use presenceforge::PipeConfig;

// Linux/macOS
let config = PipeConfig::CustomPath("/tmp/discord-ipc-0".to_string());

// Windows
let config = PipeConfig::CustomPath(r"\\.\pipe\discord-ipc-0".to_string());

// Flatpak (Linux)
let config = PipeConfig::CustomPath(
    format!("/run/user/{}/app/com.discordapp.Discord/discord-ipc-0",
    std::env::var("UID").unwrap_or_else(|_| "1000".to_string()))
);
```

---

## IpcConnection

Low-level IPC connection management.

### Discovery Methods

#### `IpcConnection::discover_pipes() -> Vec<DiscoveredPipe>`

Discovers all available Discord IPC pipes on the system.

```rust
use presenceforge::IpcConnection;

let pipes = IpcConnection::discover_pipes();
for pipe in pipes {
    println!("Found pipe {}: {}", pipe.pipe_number, pipe.path);
}
```

**Returns:** Vector of `DiscoveredPipe` structs

**Note:** Automatically checks standard Discord and Flatpak locations on Linux.

---

### DiscoveredPipe

Information about a discovered Discord pipe.

```rust
pub struct DiscoveredPipe {
    pub pipe_number: u8,  // Pipe number (0-9)
    pub path: String,     // Full path to the pipe
}
```

**Example usage:**

```rust
let pipes = IpcConnection::discover_pipes();
if let Some(pipe) = pipes.first() {
    let client = DiscordIpcClient::new_with_config(
        "client_id",
        Some(PipeConfig::CustomPath(pipe.path.clone()))
    )?;
}
```

---

## Error Types

### DiscordIpcError

Main error type for Discord IPC operations.

```rust
pub enum DiscordIpcError {
    ConnectionFailed(String),
    ProtocolError(String),
    SerializationError(String),
    InvalidClientId,
    NotConnected,
    IoError(std::io::Error),
    // ... other variants
}
```

### Common Errors

#### `ConnectionFailed(String)`

Failed to connect to Discord.

**Common causes:**

- Discord is not running
- No Discord pipes available
- Permission issues with IPC socket/pipe

**Example:**

```rust
match DiscordIpcClient::new("client_id") {
    Err(DiscordIpcError::ConnectionFailed(msg)) => {
        eprintln!("Connection failed: {}. Is Discord running?", msg);
    }
    Ok(client) => { /* ... */ }
    Err(e) => eprintln!("Other error: {}", e),
}
```

---

#### `ProtocolError(String)`

Discord IPC protocol error.

**Common causes:**

- Invalid response from Discord
- Protocol version mismatch
- Corrupted data

---

#### `SerializationError(String)`

Failed to serialize/deserialize JSON data.

**Common causes:**

- Invalid activity structure
- Missing required fields
- Type mismatches

---

#### `InvalidClientId`

The provided client ID is invalid.

**Solution:** Verify your Application ID from Discord Developer Portal.

---

#### `NotConnected`

Attempted to send command before connecting.

**Solution:** Call `client.connect()` before setting activities.

```rust
let mut client = DiscordIpcClient::new("client_id")?;
client.connect()?;  // Don't forget this!
client.set_activity(&activity)?;
```

---

### Error Helper Methods

#### `category(&self) -> ErrorCategory`

Returns the error category.

```rust
let category = error.category();
match category {
    ErrorCategory::Connection => println!("Connection problem"),
    ErrorCategory::Protocol => println!("Protocol issue"),
    // ...
}
```

**Categories:**

- `Connection` - Connection-related errors
- `Protocol` - IPC protocol errors
- `Serialization` - JSON serialization errors
- `Application` - Discord application errors
- `Other` - Unspecified errors

---

#### `is_connection_error(&self) -> bool`

Checks if error is connection-related.

```rust
if error.is_connection_error() {
    println!("Tip: Make sure Discord is running!");
}
```

---

#### `is_recoverable(&self) -> bool`

Checks if error might be recoverable.

```rust
if error.is_recoverable() {
    println!("You can try reconnecting");
}
```

---

## Async Clients

Async versions of the IPC client for different runtimes.

### Tokio

```rust
use presenceforge::async_io::tokio::client::new_discord_ipc_client;

#[tokio::main]
async fn main() -> presenceforge::Result {
    let mut client = new_discord_ipc_client("client_id").await?;
    client.connect().await?;

    let activity = ActivityBuilder::new()
        .state("Async Hello!")
        .build();

    client.set_activity(&activity).await?;

    tokio::time::sleep(tokio::time::Duration::from_secs(10)).await;

    client.clear_activity().await?;
    Ok(())
}
```

**Feature flag:** `tokio-runtime`

---

### async-std

```rust
use presenceforge::async_io::async_std::client::new_discord_ipc_client;

#[async_std::main]
async fn main() -> presenceforge::Result {
    let mut client = new_discord_ipc_client("client_id").await?;
    client.connect().await?;

    let activity = ActivityBuilder::new()
        .state("Async Hello!")
        .build();

    client.set_activity(&activity).await?;

    async_std::task::sleep(std::time::Duration::from_secs(10)).await;

    client.clear_activity().await?;
    Ok(())
}
```

**Feature flag:** `async-std-runtime`

---

### smol

```rust
use presenceforge::async_io::smol::client::new_discord_ipc_client;

fn main() -> presenceforge::Result {
    smol::block_on(async {
        let mut client = new_discord_ipc_client("client_id").await?;
        client.connect().await?;

        let activity = ActivityBuilder::new()
            .state("Async Hello!")
            .build();

        client.set_activity(&activity).await?;

        smol::Timer::after(std::time::Duration::from_secs(10)).await;

        client.clear_activity().await?;
        Ok(())
    })
}
```

**Feature flag:** `smol-runtime`

---

## Type Aliases

```rust
pub type Result<T = (), E = DiscordIpcError> = std::result::Result<T, E>;
```

Convenient type alias for Results with `DiscordIpcError`.

**Usage:**

```rust
use presenceforge::Result;

fn my_function() -> Result {
    // Returns Result<(), DiscordIpcError>
    Ok(())
}

fn returns_value() -> Result<String> {
    // Returns Result<String, DiscordIpcError>
    Ok("Hello".to_string())
}
```

---

## See Also

- [Getting Started Guide]GETTING_STARTED.md
- [Activity Builder Reference]ACTIVITY_BUILDER_REFERENCE.md
- [Async Runtimes Guide]ASYNC_RUNTIMES.md
- [Error Handling Guide]ERROR_HANDLING.md
- [Examples]../examples/