Last active
June 27, 2023 08:13
-
-
Save Hay1tsme/54dfdb45d28c8f7b22e3ce0a53b45c54 to your computer and use it in GitHub Desktop.
Simple node.js app that uses Discords webhook feature to push match updates about a challonge.com tournament. Requires request, fs and timers are global
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
//Program by Kevin 'Hay1tsme' Trocolli | |
//Uses Discord webhooks to push updates about a Challonge tournament to a discord server via text message | |
const request = require('request'); | |
const fs = require('fs'); | |
const timer = require('timers') | |
const hook = "<Discord Webhook Here>" | |
const chal = "https://api.challonge.com/v1/tournaments/<subdomain>-<tournimentURL>/matches.json?api_key=<apiKey>&state=complete" | |
//Sends the data to Discord, team1 and team2 are participants, wTeam is true if Team1 won and false if Team2 won, s1 and s2 are the scores of Team1 and Team2, and isFinal determins weather or not the series is the Grand Final | |
function sendData(team1, team2, wTeam, s1, s2, isFinal) { | |
var dat; | |
var total = s1 + s2; | |
//Define all possible conditions, filling with as much info as possible | |
//Team 1 wins and leads the series | |
if (wTeam && s1 > s2) { | |
dat = { | |
content: "Congratulations, " + team1 + " on taking down " + team2 + " in game " + total + "! The series is now " + s1 + " - " + s2 + " in favor of " + team1, | |
}; | |
console.log("Team 1 wins and leads the series"); | |
} | |
//Team 1 wins, but 2 leads the series | |
else if (wTeam && s2 > s1) { | |
dat = { | |
content: "Congratulations, " + team1 + " on taking down " + team2 + " in game " + total + "! The series is now " + s2 + " - " + s1 + " in favor of " + team2, | |
}; | |
console.log("Team 1 wins, but 2 leads the series"); | |
} | |
//Team 2 wins, but 1 leads the series | |
else if (!wTeam && s1 > s2) { | |
dat = { | |
content: "Congratulations, " + team2 + " on taking down " + team1 + " in game " + total + "! The series is now " + s1 + " - " + s2 + " in favor of " + team1, | |
}; | |
} | |
//Team 2 wins and leads the series | |
else if (!wTeam && s2 > s1) { | |
dat = { | |
content: "Congratulations, " + team2 + " on taking down " + team1 + " in game " + total + "! The series is now " + s2 + " - " + s1 + " in favor of " + team2, | |
}; | |
} | |
//Team 1 wins and the series is tied | |
else if (wTeam && s2 == s1) { | |
dat = { | |
content: "Congratulations, " + team1 + " on taking down " + team2 + " in game " + total + "! The series is now tied at " + s1 + " - " + s2, | |
}; | |
} | |
//Team 2 wins and the series is tied | |
else if (!wTeam && s2 == s1) { | |
dat = { | |
content: "Congratulations, " + team2 + " on taking down " + team1 + " in game " + total + "! The series is now tied at " + s1 + " - " + s2, | |
}; | |
} | |
//POST the data to Discord | |
request.post({ url: hook, formData: dat }); | |
//Hopefully this code is only read after the last game in a series! | |
//If it's the grand final and Team1 has 3 games (meaning they win) | |
if (isFinal && s1 == 3) { | |
dat = { | |
content: "Congratulations " + team1 + " on being the winners of this tournament! A thanks to all thoes who participated, and we hope to see you all again next time!", | |
} | |
} | |
//If it's the grand final and Team2 has 3 games (meaning they win) | |
else if (isFinal && s2 == 3) { | |
dat = { | |
content: "Congratulations " + team2 + " on being the winners of this tournament! A thanks to all thoes who participated, and we hope to see you all again next time!", | |
} | |
} | |
//If it's not the grand final and Team1 has 3 games (meaning they win) | |
else if (!isFinal && s1 == 2) { | |
dat = { | |
content: "Congratulations " + team1 + " on besting " + team2 + " in the series! Final score: " + s1 + " - " + s2, | |
} | |
} | |
//If it's not the grand final and Team2 has 3 games (meaning they win) | |
else if (!isFinal && s2 == 2) { | |
dat = { | |
content: "Congratulations " + team2 + " on besting " + team1 + " in the series! Final score: " + s2 + " - " + s1, | |
} | |
} | |
//If it's the end of theGrand Final, or the final game in a series, send an additional message | |
if (isFinal || (!isFinal && s1 == 2 || s2 == 2)) { | |
request.post({ url: hook, formData: dat }); | |
} | |
} | |
function update() { | |
var ids; | |
var site = request.get(chal); | |
//TODO: make ids an array of existant match IDs and if the index of that is less then the index of the site, we know we have a new completed match | |
} | |
timer.setInterval(update, 5000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment