|
1 | | -'use strict' |
| 1 | +const log = require('fancy-log'); |
2 | 2 |
|
3 | | -const log = require('fancy-log') |
4 | | - |
5 | | -log.info('INFO: Loading...') |
| 3 | +log.info('INFO: Loading...'); |
6 | 4 |
|
7 | 5 | const snekfetch = require('snekfetch'), |
8 | 6 | { Client } = require('discord-rpc'), |
9 | 7 | updatePresence = require('./core'), |
10 | 8 | events = require('events'), |
11 | 9 | config = require('./config'), |
12 | | - clientID = '427863248734388224' |
| 10 | + clientID = '427863248734388224'; |
13 | 11 |
|
14 | 12 | let mediaEmitter = new events.EventEmitter(), |
15 | 13 | active = false, |
16 | 14 | discordRPCLoop, |
17 | 15 | mpcServerLoop, |
18 | | - rpc |
| 16 | + rpc; |
19 | 17 |
|
20 | 18 | // Checks if port set in config.js is valid. |
21 | 19 | if (isNaN(config.port)) { |
22 | | - throw new Error('Port is empty or invalid! Please set a valid port number in \'config.js\' file.') |
| 20 | + throw new Error('Port is empty or invalid! Please set a valid port number in \'config.js\' file.'); |
23 | 21 | } |
24 | 22 |
|
25 | | -const uri = `http://localhost:${config.port}/variables.html` |
| 23 | +const uri = `http://localhost:${config.port}/variables.html`; |
26 | 24 |
|
27 | | -log.info('INFO: Fully ready. Trying to connect to Discord client...') |
| 25 | +log.info('INFO: Fully ready. Trying to connect to Discord client...'); |
28 | 26 |
|
29 | 27 | // When it succesfully connects to MPC Web Interface, it begins checking MPC |
30 | 28 | // every 5 seconds, getting its playback data and sending it to Discord Rich Presence |
31 | 29 | // through updatePresence() function from core.js. |
32 | 30 | mediaEmitter.on('CONNECTED', res => { |
33 | | - clearInterval(mpcServerLoop) |
34 | | - mpcServerLoop = setInterval(checkMPCEndpoint, 5000) |
| 31 | + clearInterval(mpcServerLoop); |
| 32 | + mpcServerLoop = setInterval(checkMPCEndpoint, 5000); |
35 | 33 | if (!active) { |
36 | | - log.info(`INFO: Connected to ${res.headers.server}`) |
| 34 | + log.info(`INFO: Connected to ${res.headers.server}`); |
37 | 35 | } |
38 | | - active = updatePresence(res, rpc) |
39 | | -}) |
| 36 | + active = updatePresence(res, rpc); |
| 37 | +}); |
40 | 38 |
|
41 | 39 | // When connection to MPC fails it attempts to connect |
42 | 40 | // to MPC again every 15 seconds. |
43 | 41 | mediaEmitter.on('CONN_ERROR', code => { |
44 | 42 | log.error(`ERROR: Unable to connect to Media Player Classic on port ${config.port}. ` + |
45 | | - `Make sure MPC is running, Web Interface is enabled and the port set in 'config.js' file is correct.\n` + code) |
| 43 | + `Make sure MPC is running, Web Interface is enabled and the port set in 'config.js' file is correct.\n` + code); |
46 | 44 | // If MPC was previously connected (ie. MPC gets closed while script is running) |
47 | 45 | // the whole process is killed and restarted by Forever in order to clean MPC Rich Presence |
48 | 46 | // from user's profile, as destroyRPC() apparently can't do so. |
49 | 47 | if (active) { |
50 | | - log.warn('WARN: Killing process to clean Rich Presence from your profile...') |
51 | | - process.exit(0) |
| 48 | + log.warn('WARN: Killing process to clean Rich Presence from your profile...'); |
| 49 | + process.exit(0); |
52 | 50 | } |
53 | 51 | if (mpcServerLoop._onTimeout !== checkMPCEndpoint) { |
54 | | - clearInterval(mpcServerLoop) |
55 | | - mpcServerLoop = setInterval(checkMPCEndpoint, 15000) |
| 52 | + clearInterval(mpcServerLoop); |
| 53 | + mpcServerLoop = setInterval(checkMPCEndpoint, 15000); |
56 | 54 | } |
57 | | -}) |
| 55 | +}); |
58 | 56 |
|
59 | 57 | // If RPC successfully connects to Discord client, |
60 | 58 | // it will attempt to connect to MPC Web Interface every 15 seconds. |
61 | 59 | mediaEmitter.on('discordConnected', () => { |
62 | | - clearInterval(discordRPCLoop) |
63 | | - log.info('INFO: Connected to Discord. Listening MPC on ' + uri) |
64 | | - checkMPCEndpoint() |
65 | | - mpcServerLoop = setInterval(checkMPCEndpoint, 15000) |
66 | | -}) |
| 60 | + clearInterval(discordRPCLoop); |
| 61 | + log.info('INFO: Connected to Discord. Listening MPC on ' + uri); |
| 62 | + checkMPCEndpoint(); |
| 63 | + mpcServerLoop = setInterval(checkMPCEndpoint, 15000); |
| 64 | +}); |
67 | 65 |
|
68 | 66 | // If RPC gets disconnected from Discord Client, |
69 | 67 | // it will stop checking MPC playback data. |
70 | 68 | mediaEmitter.on('discordDisconnected', () => { |
71 | | - clearInterval(mpcServerLoop) |
72 | | -}) |
| 69 | + clearInterval(mpcServerLoop); |
| 70 | +}); |
73 | 71 |
|
74 | 72 | // Tries to connect to MPC Web Interface and, |
75 | 73 | // if connected, fetches its data. |
76 | 74 | function checkMPCEndpoint() { |
77 | 75 | snekfetch.get(uri) |
78 | | - .then(function (res) { |
79 | | - mediaEmitter.emit('CONNECTED', res) |
80 | | - }) |
81 | | - .catch(function (err) { |
82 | | - mediaEmitter.emit('CONN_ERROR', err) |
| 76 | + .then(res => { |
| 77 | + mediaEmitter.emit('CONNECTED', res); |
83 | 78 | }) |
| 79 | + .catch(err => { |
| 80 | + mediaEmitter.emit('CONN_ERROR', err); |
| 81 | + }); |
84 | 82 | } |
85 | 83 |
|
86 | 84 | // Initiates a new RPC connection to Discord client. |
87 | 85 | function initRPC(clientID) { |
88 | | - rpc = new Client({ transport: 'ipc' }) |
| 86 | + rpc = new Client({ transport: 'ipc' }); |
89 | 87 | rpc.on('ready', () => { |
90 | | - clearInterval(discordRPCLoop) |
91 | | - mediaEmitter.emit('discordConnected') |
| 88 | + clearInterval(discordRPCLoop); |
| 89 | + mediaEmitter.emit('discordConnected'); |
92 | 90 | rpc.transport.once('close', async () => { |
93 | 91 | await destroyRPC(); |
94 | 92 | log.error('ERROR: Connection to Discord client was closed. Trying again in 10 seconds...'); |
95 | | - mediaEmitter.emit('discordDisconnected') |
| 93 | + mediaEmitter.emit('discordDisconnected'); |
96 | 94 | discordRPCLoop = setInterval(initRPC, 10000, clientID); |
97 | 95 | }); |
98 | | - }) |
| 96 | + }); |
99 | 97 |
|
100 | 98 | // Log in to the RPC server on Discord client, and check whether or not it errors. |
101 | | - rpc.login(clientID).catch(error => { |
| 99 | + rpc.login(clientID).catch(() => { |
102 | 100 | log.warn('WARN: Connection to Discord has failed. Trying again in 10 seconds...'); |
103 | | - }) |
| 101 | + }); |
104 | 102 | } |
105 | 103 |
|
106 | 104 | // Destroys any active RPC connection. |
|
0 commit comments