# Discord Rich Presence Module

The AeThex Engine includes built-in Discord Rich Presence integration, allowing your games to display activity status on Discord.

## Setup

### Creating a Discord Application

1. Go to [Discord Developer Portal](https://discord.com/developers/applications)
2. Click "New Application" and give it a name (e.g., your game's name)
3. Copy the **Application ID** from the General Information tab
4. Go to "Rich Presence" → "Art Assets" to upload images:
   - Upload a large image (512x512 or 1024x1024)
   - Upload a small image for secondary status
5. Note the asset keys you create

### AeThex Engine Default App

For games made with AeThex Engine, you can use the default AeThex presence:
- **Application ID**: `YOUR_APP_ID_HERE` (replace with your registered app)

## Usage in GDScript

### Basic Initialization

```gdscript
extends Node

func _ready():
    # Initialize with your Discord Application ID
    DiscordRPC.initialize("YOUR_APPLICATION_ID")
    
    # Wait for connection
    await get_tree().create_timer(1.0).timeout
    
    if DiscordRPC.is_connected():
        print("Connected to Discord!")
        _update_presence()

func _update_presence():
    DiscordRPC.set_details("Playing My Awesome Game")
    DiscordRPC.set_state("In Main Menu")
    DiscordRPC.set_large_image("game_logo", "My Awesome Game")
    DiscordRPC.set_small_image("menu_icon", "Main Menu")
    DiscordRPC.set_timestamps(Time.get_unix_time_from_system(), 0)
    DiscordRPC.update_presence()

func _exit_tree():
    DiscordRPC.shutdown()
```

### Showing Game Progress

```gdscript
# When entering a level
func on_level_started(level_name: String):
    DiscordRPC.set_details("Playing: " + level_name)
    DiscordRPC.set_state("Score: 0")
    DiscordRPC.set_large_image("game_logo", "My Game")
    DiscordRPC.set_small_image("level_icon", level_name)
    DiscordRPC.update_presence()

# Update score
func on_score_changed(score: int):
    DiscordRPC.set_state("Score: " + str(score))
    DiscordRPC.update_presence()
```

### Multiplayer Party

```gdscript
func on_joined_party(party_id: String, current_size: int, max_size: int):
    DiscordRPC.set_party(party_id, current_size, max_size)
    DiscordRPC.set_state("In Party")
    DiscordRPC.update_presence()

# Enable join requests (requires additional handling)
func enable_join_requests(join_secret: String):
    DiscordRPC.set_secrets("", join_secret, "")
    DiscordRPC.update_presence()
```

### Using Helper Methods

```gdscript
# Quick setup for game presence
func setup_game_presence():
    DiscordRPC.set_game_presence("My Awesome Game", "Loading...")

# This automatically sets:
# - details: "My Awesome Game"
# - state: "Loading..."
# - large_image: "aethex_engine" with "Made with AeThex Engine"
# - start_timestamp: current time
```

## API Reference

### Methods

| Method | Description |
|--------|-------------|
| `initialize(app_id: String)` | Connect to Discord with the given Application ID |
| `shutdown()` | Disconnect from Discord |
| `is_connected() -> bool` | Check if connected to Discord |
| `get_connection_state() -> ConnectionState` | Get detailed connection state |
| `set_details(text: String)` | Set the first line of presence text |
| `set_state(text: String)` | Set the second line of presence text |
| `set_large_image(key: String, text: String = "")` | Set the large image and tooltip |
| `set_small_image(key: String, text: String = "")` | Set the small image and tooltip |
| `set_timestamps(start: int, end: int = 0)` | Set elapsed/remaining time (Unix timestamps) |
| `set_party(id: String, size: int, max: int)` | Set party info for multiplayer |
| `set_secrets(match: String, join: String, spectate: String)` | Set invite secrets |
| `update_presence()` | Send the presence update to Discord |
| `clear_presence()` | Clear all presence data |
| `set_editor_presence(project: String, scene: String = "")` | Quick setup for editor mode |
| `set_game_presence(game: String, activity: String = "")` | Quick setup for game mode |

### Connection States

- `DISCONNECTED` - Not connected to Discord
- `CONNECTING` - Attempting to connect
- `CONNECTED` - Successfully connected
- `DISCONNECTING` - Shutting down connection

## Best Practices

1. **Don't spam updates** - Only call `update_presence()` when something meaningful changes
2. **Use timestamps** - Players like to see how long they've been playing
3. **Be informative** - Show the current game mode, level, or activity
4. **Handle disconnection** - Discord may not be running; always check `is_connected()`
5. **Clean up** - Always call `shutdown()` when your game closes

## Troubleshooting

### Presence not showing?

1. Make sure Discord desktop app is running
2. Check "Display current activity" is enabled in Discord settings
3. Verify your Application ID is correct
4. Make sure image asset keys match what you uploaded to Discord

### Connection failing?

- Discord might not be running
- Another application might be using the IPC connection
- Check the `get_connection_state()` for detailed status

## AeThex Engine Editor Integration

The AeThex Engine editor automatically shows Discord presence when you're working on a project. This is enabled by default and shows:

- Project name you're working on
- Current scene being edited
- Time elapsed since opening the project

To disable editor Discord presence, go to **Editor Settings → Discord → Enable Presence** and uncheck it.
