Created
September 10, 2017 10:00
-
-
Save TheBITLINK/6a6a248bc9df04930d54e22eace85301 to your computer and use it in GitHub Desktop.
Update discord's game status according to foobar2k's window title.
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
/** | |
* @file Updates Discord's game status according to foobar2k's window title. | |
* @author TheBITLINK aka BIT <[email protected]> | |
* @license MIT | |
* | |
* To use: | |
* - Set foobar2k's window title to "%artist% - %title%" (Preferences -> Display -> Default User Interface) | |
* - Edit the line bellow with your discord user token | |
* - Run those commands (in the same folder discordfb2k.js is in): | |
* - npm install discord.js node-process-windows | |
* - node discordfb2k | |
* | |
* Notes: | |
* - Node.js 7.8+ is required | |
* - You won't be able to see your own status, but others will | |
* - Don't set the update interval too low | |
*/ | |
const Discord = require('discord.js') | |
const Windows = require('node-process-windows') | |
// Discord Token | |
const TOKEN = "mfa.." | |
// Update interval (seconds) | |
const INTERVAL = 10 | |
let currentTitle | |
let update | |
// Update title | |
setInterval(() => Windows.getProcesses((err, ps) => { | |
const f2k = ps.find(p => p.processName === 'foobar2000') | |
if (f2k) { | |
currentTitle = f2k.mainWindowTitle.replace(/\[foobar2000.*]/, '').trim() | |
} else { | |
currentTitle = undefined | |
} | |
if (update) update() | |
}), INTERVAL * 1000) | |
// Discord client | |
const client = new Discord.Client() | |
client.on('ready', () => { | |
console.log(`Connected to Discord! (@${client.user.username}#${client.user.discriminator})`) | |
update = async () => { | |
if (!client.user.presence.game || client.user.presence.game.name !== currentTitle) { | |
try { | |
await client.user.setGame(currentTitle) | |
console.log(`Set status to: ${currentTitle}`) | |
} catch (e) { | |
console.error(`Couldn't change status. (${e.message})`) | |
} | |
} | |
} | |
}) | |
client.on('error', err => { | |
console.error(`Couldn't connect to Discord: ${e.message}.`) | |
}) | |
client.login(TOKEN) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment