SteamRPC/index.js

105 lines
3.3 KiB
JavaScript
Raw Normal View History

2023-11-29 22:07:10 -06:00
const fs = require('fs')
2023-11-22 11:46:09 -06:00
var updateTime = (process.env.UPDATE_TIME || 30)
2023-11-30 00:37:20 -06:00
var clientId = "1176317740512985119"
2023-11-22 11:46:09 -06:00
const steamID = process.env.STEAM_ID
2023-11-30 00:37:20 -06:00
const thumborURL = "https://thumbor-production-0e82.up.railway.app/unsafe/fit-in/512x512/filters:fill(transparent)/"
// if (clientId == undefined) {
// console.error("No client ID was defined! Please check the documentation for how to add one.")
// return
// }
2023-11-22 11:46:09 -06:00
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() {
2023-11-22 16:06:48 -06:00
fetch(`https://steamcommunity.com/miniprofile/${steamID}`)
2023-11-22 11:46:09 -06:00
.then((data) => {
return data.text()
})
.then((html) => {
var json = {
"name": "nothing",
2023-11-30 00:37:20 -06:00
"presence": "",
"img": "",
"pfp": html.substring(html.indexOf('<img src="https://avatars') + 10, html.indexOf("medium")) + "full.jpg",
"username": ""
2023-11-22 11:46:09 -06:00
}
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>"))
2023-11-22 16:10:09 -06:00
} else if (html.includes("game_state") && json.name != "nothing") {
var blah = html.substring(html.indexOf('game_state">') + 12, html.length)
2023-11-22 16:06:48 -06:00
json["presence"] = blah.substring(0, blah.indexOf("</span>"))
2023-11-22 11:46:09 -06:00
}
2023-11-30 00:37:20 -06:00
if (html.includes("persona")) {
var blah = html.substring(html.indexOf("persona"), html.length)
json.username = blah.substring(blah.indexOf(">") + 1, blah.indexOf("</span>"))
}
if (html.includes("game_logo")) {
var blah = html.substring(html.indexOf('game_logo" src="') + 'game_logo" src="'.length + 8, html.indexOf("capsule"))
json.img = thumborURL + blah + "logo.png"
}
2023-11-22 11:46:09 -06:00
if (prevGame != json.name) {
prevGame = json.name
startTime = Date.now()
}
2023-11-30 00:37:20 -06:00
2023-11-22 11:46:09 -06:00
client.setActivity({
details: `Playing ${json.name}`,
state: json.presence,
startTimestamp: startTime,
2023-11-30 00:37:20 -06:00
largeImageText: json.name,
largeImageKey: json.img,
smallImageText: json.username,
smallImageKey: json.pfp,
});
2023-11-22 11:46:09 -06:00
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()
2023-11-29 22:07:10 -06:00
});
process.on('uncaughtException', (err, origin) => {
2023-11-30 00:37:20 -06:00
fs.writeSync(
process.stderr.fd,
`Caught exception: ${err}\n` +
`Exception origin: ${origin}`,
)
2023-11-29 22:07:10 -06:00
})