152 lines
4.2 KiB
Markdown
152 lines
4.2 KiB
Markdown
# Cell Discord SDK
|
|
|
|
Cell bindings for the Discord Social SDK.
|
|
|
|
## Setup
|
|
|
|
1. Download the Discord Social SDK from the Discord Developer Portal
|
|
2. Extract to `discord_social_sdk/` in this directory
|
|
3. Configure your application in `config.cm`
|
|
|
|
## Configuration
|
|
|
|
Edit `config.cm` with your Discord application details:
|
|
|
|
```javascript
|
|
return {
|
|
APPLICATION_ID: 1234567890123456789n, // Your app ID (as BigInt)
|
|
PUBLIC_KEY: "your_public_key_here",
|
|
REDIRECT_URI: "http://127.0.0.1/callback",
|
|
CALLBACK_INTERVAL: 16
|
|
}
|
|
```
|
|
|
|
## API Reference
|
|
|
|
### Core Functions
|
|
|
|
- `discord.init(application_id)` - Initialize the Discord client
|
|
- `discord.run_callbacks()` - Process Discord callbacks (call regularly)
|
|
- `discord.shutdown()` - Disconnect and clean up
|
|
- `discord.get_status()` - Get connection status: "not_initialized", "disconnected", "connecting", "connected", "ready", "reconnecting"
|
|
- `discord.is_authenticated()` - Check if authenticated
|
|
- `discord.connect()` - Connect to Discord
|
|
- `discord.disconnect()` - Disconnect from Discord
|
|
|
|
### Callback Registration
|
|
|
|
- `discord.on_status_changed(callback)` - Status change callback: `function(status, error, detail)`
|
|
- `discord.on_log(callback, min_severity)` - Log callback: `function(message, severity)`
|
|
|
|
### Authentication
|
|
|
|
- `discord.get_default_scopes()` - Get default OAuth2 scopes
|
|
- `discord.authorize(callback)` - Start OAuth2 flow: `function(success, code, redirect_uri, error)`
|
|
- `discord.get_token(code, redirect_uri, callback)` - Exchange code for token: `function(result)`
|
|
- `discord.update_token(type, token, callback)` - Update access token: `function(success, error)`
|
|
- `discord.refresh_token(refresh_token, callback)` - Refresh access token: `function(result)`
|
|
|
|
### User API
|
|
|
|
- `discord.get_current_user()` - Get current user object
|
|
- `discord.get_user(user_id)` - Get user by ID
|
|
|
|
User object properties:
|
|
- `id` - BigInt user ID
|
|
- `username` - Username
|
|
- `display_name` - Display name
|
|
- `global_name` - Global name (optional)
|
|
- `avatar_url` - Avatar URL
|
|
- `status` - "online", "offline", "idle", "dnd"
|
|
- `is_provisional` - Is provisional account
|
|
|
|
### Relationships API
|
|
|
|
- `discord.get_relationships()` - Get all relationships (friends list)
|
|
- `discord.get_friends_count()` - Get number of friends
|
|
|
|
Relationship object properties:
|
|
- `id` - BigInt user ID
|
|
- `type` - "none", "friend", "blocked", "pending_incoming", "pending_outgoing"
|
|
- `user` - User object
|
|
|
|
### Rich Presence API
|
|
|
|
- `discord.update_rich_presence(activity, callback)` - Update rich presence
|
|
- `discord.clear_rich_presence()` - Clear rich presence
|
|
|
|
Activity object properties:
|
|
- `type` - "playing", "streaming", "listening", "watching", "competing"
|
|
- `state` - Current state text
|
|
- `details` - Details text
|
|
- `large_image` - Large image key
|
|
- `large_text` - Large image tooltip
|
|
- `small_image` - Small image key
|
|
- `small_text` - Small image tooltip
|
|
- `start_timestamp` - Start time (Unix ms)
|
|
- `end_timestamp` - End time (Unix ms)
|
|
- `party_id` - Party ID
|
|
- `party_size` - Current party size
|
|
- `party_max` - Max party size
|
|
- `join_secret` - Join secret for invites
|
|
|
|
## Example Usage
|
|
|
|
```javascript
|
|
var discord = use("discord")
|
|
var config = use("config")
|
|
|
|
// Initialize
|
|
discord.init(config.APPLICATION_ID)
|
|
|
|
// Set up callbacks
|
|
discord.on_status_changed(function(status, error, detail) {
|
|
log.console(`Status: ${status}`)
|
|
if (status == "ready") {
|
|
var user = discord.get_current_user()
|
|
log.console(`Logged in as: ${user.display_name}`)
|
|
|
|
// Set rich presence
|
|
discord.update_rich_presence({
|
|
type: "playing",
|
|
state: "In Game",
|
|
details: "Level 5"
|
|
})
|
|
}
|
|
})
|
|
|
|
// Start auth flow
|
|
discord.authorize(function(success, code, redirect_uri, error) {
|
|
if (success) {
|
|
discord.get_token(code, redirect_uri, function(result) {
|
|
if (result.success) {
|
|
discord.update_token("bearer", result.access_token, function(ok) {
|
|
if (ok) discord.connect()
|
|
})
|
|
}
|
|
})
|
|
}
|
|
})
|
|
|
|
// Main loop - call regularly
|
|
function tick() {
|
|
discord.run_callbacks()
|
|
$_.delay(tick, 16)
|
|
}
|
|
tick()
|
|
```
|
|
|
|
## Running Tests
|
|
|
|
```bash
|
|
cd cell-discord
|
|
cell test
|
|
```
|
|
|
|
## Running Example
|
|
|
|
```bash
|
|
cd cell-discord
|
|
cell examples/discord_example
|
|
```
|