Files
cell-discord/examples/discord_example.ce

183 lines
5.3 KiB
Plaintext

// Discord Social SDK Example
// Demonstrates: Authentication, Status Monitoring, Friends List, Rich Presence
//
// Configuration is loaded from config.cm
// Get your own app at: https://discord.com/developers/applications
var discord = use("discord")
var config = use("tests/config")
var time = use('time')
// ============================================================================
// CONFIGURATION - Loaded from config.cm (or override here)
// ============================================================================
def APPLICATION_ID = config.APPLICATION_ID
def CALLBACK_INTERVAL = config.CALLBACK_INTERVAL
// ============================================================================
// STATE
// ============================================================================
var authenticated = false
var ready = false
var access_token = null
var refresh_token = null
// ============================================================================
// DISCORD CALLBACKS
// ============================================================================
function on_status_changed(status, error, detail) {
log.console(`Discord status: ${status}`)
if (status == "ready") {
ready = true
log.console("Discord client is ready!")
on_ready()
} else if (error != "none") {
log.console(`Discord error: ${error} (detail: ${detail})`)
}
}
function on_log(message, severity) {
log.console(`[Discord ${severity}] ${message}`)
}
function on_ready() {
// Get and display current user info
var user = discord.get_current_user()
if (user) {
log.console(`Logged in as: ${user.display_name} (@${user.username})`)
log.console(`User ID: ${user.id}`)
log.console(`Status: ${user.status}`)
if (user.avatar_url) log.console(`Avatar: ${user.avatar_url}`)
}
// Get friends count
var friends_count = discord.get_friends_count()
log.console(`Friends count: ${friends_count}`)
// Get full relationships list
var relationships = discord.get_relationships()
log.console(`Total relationships: ${relationships.length}`)
for (var i = 0; i < relationships.length && i < 5; i++) {
var rel = relationships[i]
if (rel.user) {
log.console(` - ${rel.user.display_name} (${rel.type})`)
}
}
if (relationships.length > 5) {
log.console(` ... and ${relationships.length - 5} more`)
}
// Set rich presence
set_rich_presence()
}
function set_rich_presence() {
log.console("Setting rich presence...")
discord.update_rich_presence({
type: "playing",
state: "In Main Menu",
details: "Exploring the game",
start_timestamp: time.number()
}, function(success, error) {
if (success) {
log.console("Rich presence updated successfully!")
} else {
log.console(`Rich presence update failed: ${error}`)
}
})
}
// ============================================================================
// AUTHENTICATION FLOW
// ============================================================================
function start_auth() {
log.console("Starting Discord authorization...")
log.console("A browser window should open for Discord login.")
discord.authorize(function(success, code, redirect_uri, error) {
if (success) {
log.console("Authorization successful! Exchanging code for token...")
exchange_token(code, redirect_uri)
} else {
log.console(`Authorization failed: ${error}`)
}
})
}
function exchange_token(code, redirect_uri) {
discord.get_token(code, redirect_uri, function(result) {
if (result.success) {
log.console("Token received!")
access_token = result.access_token
refresh_token = result.refresh_token
log.console(`Token expires in: ${result.expires_in} seconds`)
// Update the client with the token and connect
update_and_connect(result.access_token)
} else {
log.console(`Token exchange failed: ${result.error}`)
}
})
}
function update_and_connect(token) {
log.console("Updating token and connecting...")
discord.update_token("bearer", token, function(success, error) {
if (success) {
log.console("Token updated, connecting to Discord...")
authenticated = true
discord.connect()
} else {
log.console(`Token update failed: ${error}`)
}
})
}
// ============================================================================
// MAIN LOOP - Process Discord callbacks
// ============================================================================
function discord_tick() {
discord.run_callbacks()
$_.delay(discord_tick, CALLBACK_INTERVAL)
}
// ============================================================================
// INITIALIZATION
// ============================================================================
function main() {
log.console("=== Discord Social SDK Example ===")
log.console(`Application ID: ${APPLICATION_ID}`)
// Initialize Discord client
var init_result = discord.init(APPLICATION_ID)
if (!init_result) {
log.console("Failed to initialize Discord client")
$_.stop()
return
}
log.console("Discord client initialized")
// Set up callbacks
discord.on_status_changed(on_status_changed)
discord.on_log(on_log, "info")
// Start the callback processing loop
discord_tick()
// Start authentication
start_auth()
log.console("Waiting for Discord events...")
log.console("Press Ctrl+C to exit")
}
// Run main
main()