Created
April 9, 2020 09:37
-
-
Save purpl3F0x/5f915d62e6185dddb0894b0a4e551caa to your computer and use it in GitHub Desktop.
roon-discord_rpc.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const RoonApi = require("node-roon-api"), | |
RoonApiStatus = require("node-roon-api-status"), | |
RoonApiTransport = require('node-roon-api-transport'); | |
const client = require('discord-rich-presence')('604340186326761512'); | |
var core; | |
var zones; | |
var roon = new RoonApi({ | |
extension_id: 'discord_rpc.purpl3F0x.com', | |
display_name: "Discord RPC", | |
display_version: "0.0.1", | |
publisher: 'Stavros Avramidis', | |
email: 'stavros9899@gmail.com', | |
website: '', | |
log_level: 'none', | |
core_paired: function (core_) { | |
current_zone_id = roon.load_config("current_zone_id"); | |
core = core_; | |
core.services.RoonApiTransport.subscribe_zones((response, msg) => { | |
if (response == "Subscribed") { | |
let zones_ = msg.zones.reduce((p, e) => (p[e.zone_id] = e) && p, {}); | |
zones = zones_; | |
} else if (response == "Changed") { | |
var z; | |
if (msg.zones_seek_changed) sendState(msg.zones_seek_changed[0]); | |
if (msg.zones_removed) msg.zones_removed.forEach(e => delete (zones[e.zone_id])); | |
if (msg.zones_added) msg.zones_added.forEach(e => zones[e.zone_id] = e); | |
if (msg.zones_changed){msg.zones_changed.forEach(e => zones[e.zone_id] = e); sendState(msg.zones_changed[0], true);}; | |
zones = zones; | |
} | |
}); | |
}, | |
core_unpaired: function (core_) { | |
core = undefined; | |
} | |
}); | |
var svc_status = new RoonApiStatus(roon); | |
roon.init_services({ | |
required_services: [RoonApiTransport], | |
provided_services: [svc_status,] | |
}); | |
svc_status.set_status("Extension enabled", false); | |
roon.start_discovery(); | |
function sendState(msg, isPaused = false) { | |
this.last_title; | |
if (zones[msg['zone_id']]["now_playing"] && (this.last_title != zones[msg['zone_id']]["now_playing"]["one_line"]['line1'] || isPaused)) { | |
if (!isPaused){ | |
this.last_title = zones[msg['zone_id']]["now_playing"]["one_line"]['line1']; | |
client.updatePresence({ | |
state: zones[msg['zone_id']]["now_playing"]["two_line"]['line2'].substring(0, 127), | |
details: zones[msg['zone_id']]["now_playing"]["two_line"]['line1'].substring(0, 127), | |
startTimestamp: Date.now() - zones[msg['zone_id']]["now_playing"]['seek_position'] * 1000, | |
endTimestamp: Date.now() + (zones[msg['zone_id']]["now_playing"]["length"] - zones[msg['zone_id']]["now_playing"]['seek_position']) * 1000, | |
largeImageKey: 'roon_logo', | |
instance: true | |
}); | |
}else { | |
this.last_title = ""; | |
client.updatePresence({ | |
state: zones[msg['zone_id']]["now_playing"]["two_line"]['line2'].substring(0, 127), | |
details: "(Paused) " + zones[msg['zone_id']]["now_playing"]["two_line"]['line1'].substring(0, 127), | |
largeImageKey: 'roon_logo', | |
smallImageKey: 'pause', | |
instance: true | |
}); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment