forked from jtpox/discord-presence-roon
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
191 lines (164 loc) · 6 KB
/
index.js
File metadata and controls
191 lines (164 loc) · 6 KB
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
#!/usr/bin/env node
/** @module main */
const { version, author, homepage } = require('../package.json');
const RoonApi = require('@roonlabs/node-roon-api');
const RoonApiTransport = require('node-roon-api-transport');
const RoonApiImage = require('node-roon-api-image');
const RoonSettings = require('./settings');
const Discord = require('./discord');
const Discogs = require('./discogs');
const Imgur = require('./imgur');
var roon = new RoonApi({
extension_id: 'com.jtpox.discord-roon',
display_name: `Discord Presence Integration`,
display_version: version,
publisher: author.name,
email: author.email,
website: homepage,
core_paired: Paired,
core_unpaired: Unpaired,
});
/** @type {import('./settings').TSettings} */
let Settings;
/**
* The package constructor.
* @function Initiate
*/
function Initiate() {
RoonSettings.Initiate(roon);
InitiateIntegrations();
roon.init_services({
required_services: [RoonApiTransport, RoonApiImage],
provided_services: [RoonSettings.Service(InitiateIntegrations)],
});
roon.start_discovery();
}
/**
* Initiate Discord, Discogs and Imgur integrations.
* @function InitiateIntegrations
*/
function InitiateIntegrations() {
console.log('Extension: Reloading settings');
Settings = roon.load_config('settings') || RoonSettings.DefaultSettings;
Discord.Initiate(Settings);
Discogs.Initiate(roon, Settings);
Imgur.Initiate(roon, Settings);
}
/**
* Paired event callback for Roon.
* @function Paired
* @param {object} core The Roon core.
*/
function Paired(core) {
let transport = core.services.RoonApiTransport;
transport.subscribe_zones((cmd, data) => {
if(Discord.Self() === undefined) return;
if(cmd === 'Changed' && data.hasOwnProperty('zones_changed')) {
const zones_to_check = Settings.roonZones.split(',');
const zones = data.zones_changed.filter((data) => zones_to_check.includes(data.display_name));
const priority_zone = zones.sort((a, b) => zones_to_check.indexOf(a.display_name) - zones_to_check.indexOf(b.display_name));
if(priority_zone.length > 0) SongChanged(core, priority_zone[0]);
}
if(cmd === 'Changed' && data.hasOwnProperty('zones_removed')) Discord.Self().clearActivity();
});
}
/**
* Unpaired event callback for Roon.
* @param {object} core The Roon core.
*/
function Unpaired(core) {
if(Discord.Self() === undefined) return;
Discord.Self().clearActivity();
}
let PreviousAlbumArt = {
imageKey: 'roon_labs_logo',
imageUrl: '',
};
/**
* Song changed event which will update the Discord user activity.
* @function SongChanged
* @async
* @param {object} core The Roon core.
* @param {object} data The data provided by Roon zones.
*/
async function SongChanged(core, data) {
if(data.state === 'paused') {
Discord.Self().clearActivity();
}
if(data.state === 'playing') {
const { image_key, length, seek_position } = data.now_playing;
const endTimestamp = Math.round((new Date().getTime() / 1000) + length - seek_position);
const state = (data.now_playing.three_line.line3.substring(0, 128)
+ (data.now_playing.three_line.line3.length === 1 ? " " : "")) || undefined;
const activity = {
type: 2, // Doesn't work. (https://discord-api-types.dev/api/discord-api-types-v10/enum/ActivityType)
details: data.now_playing.one_line.line1.substring(0, 128),
state,
endTimestamp,
instance: false,
largeImageKey: (image_key === PreviousAlbumArt.imageKey)? PreviousAlbumArt.imageUrl : 'roon_labs_logo',
largeImageText: `Listening at: ${data.display_name}`,
};
Discord.Self().setActivity(activity);
/**
* Imgur API doesn't update immediately, so images might be uploaded more than once.
* This will help prevent the upload or search of the same album images multiple times.
*/
if(
data.now_playing.image_key !== PreviousAlbumArt.imageKey
&& (Settings.imgurEnable || Settings.discogsEnable)
) {
if(Settings.imgurEnable) {
Imgur.GetAlbumArt(image_key, GetImage(new RoonApiImage(core))).then((art) => {
PreviousAlbumArt.imageKey = image_key;
PreviousAlbumArt.imageUrl = art;
activity.largeImageKey = art;
Discord.Self().setActivity(activity);
}).catch(() => {});
}
if(
Settings.discogsEnable
&& !Settings.imgurEnable
) {
Discogs.Search(data.now_playing.three_line.line2, data.now_playing.three_line.line1).then((result) => {
if(result.cover_image) {
PreviousAlbumArt.imageKey = image_key;
PreviousAlbumArt.imageUrl = result.cover_image;
activity.largeImageKey = result.cover_image;
Discord.Self().setActivity(activity);
}
}).catch(() => {});
}
}
}
}
/**
* Get buffer of image from Roon's API.
* @function GetImage
* @param {RoonApiImage} api Instance of RoonApiImage (node-roon-api-image)
* @return {GetBuffer}
*/
function GetImage(api) {
/**
* Inner function for GetImage. Uses the RoonAPIImage instance to get the file buffer of the album image.
* @function
* @param {string} image_key Key of the album image.
* @return {Promise<Buffer|string>} File buffer of the album image.
*/
const GetBuffer = function(image_key) {
return new Promise((resolve, reject) => {
api.get_image(image_key, (error, content_type, image) => {
if(error) reject(error);
resolve(Buffer.from(image));
});
});
}
return GetBuffer;
}
if(require.main === module) {
Initiate();
}
module.exports = {
Initiate,
InitiateIntegrations,
}