SteamRPC/index.js

109 lines
3.6 KiB
JavaScript
Raw Permalink Normal View History

2023-12-03 22:15:48 -06:00
const fs = require('fs'),
prompt = require("prompt-sync")()
2023-11-29 22:07:10 -06:00
2023-12-15 13:07:32 -06:00
var updateTime = 45
2023-11-22 11:46:09 -06:00
2023-11-30 00:37:20 -06:00
var clientId = "1176317740512985119"
2023-12-03 22:15:48 -06:00
const steamID = process.env.STEAM_ID || prompt("What is your Steam ID?: ")
2023-11-22 11:46:09 -06:00
2024-01-04 17:33:53 -06:00
const thumborURL = "https://thumbor-production-0e82.up.railway.app/unsafe/fit-in/512x512/filters:fill(transparent):background_color(rgb(50,%2050,%2050))/"
2023-11-30 00:37:20 -06:00
2023-12-15 13:07:32 -06:00
if (updateTime < 30) { // Please, refrain from changing this! This program is already effectively DDoSing valve servers, I dont want it to go any further!
2023-12-03 16:21:29 -06:00
console.error("Update time must be above 30! Please change your settings!")
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": "",
2023-12-07 12:59:19 -06:00
"pfp": "",
2023-11-30 00:37:20 -06:00
"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-12-07 12:59:19 -06:00
json.pfp = html.substring(html.indexOf('<img src="https://avatars') + 10, html.indexOf("medium")) + "full.jpg"
2023-11-30 00:37:20 -06:00
}
2023-11-22 11:46:09 -06:00
if (prevGame != json.name) {
prevGame = json.name
startTime = Date.now()
}
2023-12-07 13:06:05 -06:00
if (json.name == "nothing") {
client.clearActivity()
2023-12-07 12:59:19 -06:00
updateTime = (process.env.UPDATE_TIME || 45) * 2.5
2023-11-22 11:46:09 -06:00
} else {
2023-12-07 13:06:05 -06:00
client.setActivity({
details: `Playing ${json.name}`,
state: json.presence,
startTimestamp: startTime,
largeImageText: json.name,
2024-01-04 17:33:53 -06:00
largeImageKey: json.img,
2023-12-07 13:06:05 -06:00
smallImageText: json.username,
smallImageKey: json.pfp || null,
});
2023-12-07 12:59:19 -06:00
updateTime = (process.env.UPDATE_TIME || 45)
2023-11-22 11:46:09 -06:00
}
})
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
})