Skip to content

Instantly share code, notes, and snippets.

@ezeholz
Last active October 9, 2022 10:55
Show Gist options
  • Save ezeholz/0b127cbd90be4ba38b0aa49d53096db1 to your computer and use it in GitHub Desktop.
Save ezeholz/0b127cbd90be4ba38b0aa49d53096db1 to your computer and use it in GitHub Desktop.
Free Game Javascript

Free Game Javascript

Free Game Javascript is part of the code I used to make bots for Discord and any other, that will grab a RSS Feed and modify it to show the game and the link to them.

Code

var tituloJuegos = Feed.newFeedItemMatches.EntryTitle.split(' - ')[0]
var juegos:Array<string> = [], result:string, prov:string

try{
  result = Feed.newFeedItemMatches.EntryTitle.split(' - ')[1]
  prov = result.replace(/(free|game|on|[^\w\s]|drm)/gi,'').trim().toLowerCase().split(' ')[0]
} catch(err){
  prov=""
}
if (tituloJuegos.split(/,| and | & /g).length>1) {
  for (let i=0;i<tituloJuegos.split(/,| and | & /g).length;i++){
    juegos.push(tituloJuegos.split(/,| and | & /g)[i].trim())
  }
} else {
  juegos.push(tituloJuegos.trim())
}

const regex = RegExp('https?:\/\/store\.steampowered\.com\/app\/[0-9]+\/*.*?(?=")|https?:\/\/(www\.|store\.)epicgames.com\/.*?(?=")|https?:\/\/(www\.)?humblebundle.com\/store\/.*?(?=")|https?:\/\/(www\.)?gog\.com\/(game\/)?.*?(?=")|https?:\/\/(www\.|.*\.)?ubisoft.com\/.*?(?=")', 'gi');

let preurls:Array<string> = [],url

while ((url = regex.exec(Feed.newFeedItemMatches.EntryContent)) !== null) {
  preurls.push(url[0])
}

let urls:Array<string> = []

juegos.forEach(function(valor, x){
  let anadido = 0
  preurls.forEach(function(value, i){
    if(!anadido) {
      let regex = RegExp('.*'+valor.toLowerCase().split(" ")[0].replace(/[^\w\s]|_/g,"")+'.*','g'), bool
      if((bool = regex.exec(value)) !== null){
        urls.push(value)
        anadido++
      } else if (i===+preurls.length-1) {
        if (prov.toLowerCase() === 'steam'||preurls.length===juegos.length) {
         urls.push(preurls[x])
         anadido++
        } else {
         urls.push(Feed.newFeedItemMatches.EntryUrl)
         anadido++
       }
      }
    }
    if(anadido) preurls.splice(i,1); return
  })
  if(!anadido){
    urls.push(Feed.newFeedItemMatches.EntryUrl)
  }
})


var color:number = 0,name:string

switch(prov.toLowerCase()){
  case 'steam': color=51; break;
  case 'epic': color=16448250; break;
  case 'uplay': color=6143; break;
  case 'gog': color=11075748; prov=prov.toUpperCase(); break;
  default: color=63244; break;
}

if (prov!=='') {
  name=prov.charAt(0).toUpperCase() + prov.slice(1)+ ' Free Game'
  if(juegos.length>1) name=name+'s'
}

var body:string = `{"username":"${name}","content":"||@here|| New free games!", "embeds": [`

for (let i=0;i<juegos.length;i++) {
  body += `{"title":"${juegos[i]}","url":"${urls[i]}","color":${color}}`
  if(i<juegos.length-1){body += ','}
}

body += ']}'

MakerWebhooks.makeWebRequest.setBody(body)

Usage

I use this code with the GrabFreeGames RSS from Steam Announcements, but you can use any you want. ;D

Paste it on your code, modify what you actually need, and be happy. :)

License

Apache License 2.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment