SteamRPC/index.js
Bingus_Violet 5caf86fd58 errors!
2023-11-29 22:07:10 -06:00

87 lines
2.5 KiB
JavaScript

const fs = require('fs')
var updateTime = (process.env.UPDATE_TIME || 30)
const clientId = process.env.CLIENT_ID
const steamID = process.env.STEAM_ID
if (clientId == undefined) {
console.error("No client ID was defined! Please check the documentation for how to add one.")
return
}
if (steamID == undefined) {
console.error("No Steam ID was defined! Please check the documentation for how to add one.")
return
}
var RPC = require("discord-rpc")
const client = new RPC.Client({ transport: 'websocket' })
client.login({ clientId })
var startTime = Date.now()
var prevGame = ""
function update() {
fetch(`https://steamcommunity.com/miniprofile/${steamID}`)
.then((data) => {
return data.text()
})
.then((html) => {
var json = {
"name": "nothing",
"presence": ""
}
if (html.includes("game_name")) {
var blah = html.substring(html.indexOf('game_name">') + 11, html.length)
json["name"] = blah.substring(0, blah.indexOf("</span>"))
}
if (html.includes("presence")) {
var blah = html.substring(html.indexOf('presence">') + 10, html.length)
json["presence"] = blah.substring(0, blah.indexOf("</span>"))
} else if (html.includes("game_state") && json.name != "nothing") {
var blah = html.substring(html.indexOf('game_state">') + 12, html.length)
json["presence"] = blah.substring(0, blah.indexOf("</span>"))
}
if (prevGame != json.name) {
prevGame = json.name
startTime = Date.now()
}
client.setActivity({
details: `Playing ${json.name}`,
state: json.presence,
startTimestamp: startTime,
largeImageKey: json.name.split(" ").join("").toLowerCase(),
largeImageText: json.name
});
if (json.gameName == "nothing") {
updateTime = (process.env.UPDATE_TIME || 30) * 2.5
} else {
updateTime = (process.env.UPDATE_TIME || 30)
}
})
setTimeout(() => {
update()
}, updateTime * 1000);
}
client.on('ready', () => {
update()
});
process.on('uncaughtException', (err, origin) => {
fs.writeSync(
process.stderr.fd,
`Caught exception: ${err}\n` +
`Exception origin: ${origin}`,
)
})