Skip to content

Instantly share code, notes, and snippets.

@midknightmare666
Created July 23, 2020 03:30
Show Gist options
  • Save midknightmare666/cd0c99c0791e4ff160c8e3b638be0eb7 to your computer and use it in GitHub Desktop.
Save midknightmare666/cd0c99c0791e4ff160c8e3b638be0eb7 to your computer and use it in GitHub Desktop.
/** Import getPlace function **/
const { getPlace } = require('someFilePath...')
/**
* @function getData
* @description requires object of { placeName: placeId }, fetches game player stats and sets
* discord voice channel name to placeName: amountOfPlayers
**/
const maps = {
robloxPlace1: 'robloxPlaceID',
robloxPlace2: 'robloxPlaceID',
}
const getData = () => {
try {
Object.entries(maps).forEach(async ([key, value]) => {
const { ok, placeName, data } = await getPlace(value, key)
if (ok === false) return
const added = data.map((obj) => obj.playing).reduce((a, b) => a + b, 0) || 0
const guild = <Discord.Client>.guilds.find((g) => g.id === 'discordGuildID')
if (!guild || guild.available === false) return
await guild.channels.find((c) => c.name.includes(placeName) && c.type === 'voice')
.setName(`${placeName}: ${added}`)
})
} catch (er) {
console.log(er)
}
}
setInterval(() => getData(), 60000)
const fetch = require('node-fetch')
module.exports = {
/**
* @method getPlace
* @description Get roblox game place data
* @async
* @param {String} placeId - Roblox place id
* @param {String} placeName - Place/Game name
* @return {Promise<Object>}
*/
getPlace: async (placeId, placeName) => {
if (placeId.constructor !== String || placeName.constructor !== String)
return { ok: false, error: 'Parameters Must Be A String' }
const url = `https://games.roblox.com/v1/games/${placeId}/servers/Public?sortOrder=Asc&limit=10`
try {
const response = await fetch(url)
const json = await response.json()
return { ok: true, placeId, placeName, ...json }
} catch (err) {
return { ok: false, ...err }
}
},
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment