From 8b9d2cc80c439f31ea1a2c242a45dc847df62911 Mon Sep 17 00:00:00 2001 From: Oliver-Akins Date: Mon, 19 Jul 2021 01:08:10 -0600 Subject: [PATCH] Add an endpoint to check to see if there's a tie, and if so alert Discord --- src/endpoints/management/check_for_tie.ts | 38 +++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 src/endpoints/management/check_for_tie.ts diff --git a/src/endpoints/management/check_for_tie.ts b/src/endpoints/management/check_for_tie.ts new file mode 100644 index 0000000..80b078f --- /dev/null +++ b/src/endpoints/management/check_for_tie.ts @@ -0,0 +1,38 @@ +import { Request, ResponseToolkit } from "@hapi/hapi"; +import { DISCORD_API_URI } from "@/constants"; +import { config, db } from "@/main"; +import axios from "axios"; + +export default { + method: `GET`, path: `/bracket/isTie`, + async handler(request: Request, h: ResponseToolkit) { + let r = await request.server.inject(`/bracket/winners`); + let winners = JSON.parse(r.payload).winners; + + if (winners.length >= 2) { + + let r = await axios.get( + `${DISCORD_API_URI}/webhooks/${db.webhook.id}/${db.webhook.token}` + ); + let { guild_id, channel_id } = r.data; + + let content = `The bracket currently has a tie between:\n> ${winners.join('\n~~------------------------------------~~\n> ')}`; + + if (winners.length > Math.floor(config.discord.quote_max / 2)) { + content += `\n\n**If this tie is not broken, all of the quotes will be eliminated**` + }; + + // Assert that the guild and channel are both properly defined + if (guild_id && channel_id) { + content += `\n\n[Click Here To Jump To The Bracket](https://discord.com/channels/${guild_id}/${channel_id}/${db.bracket.msg})` + } + + await axios.post( + `${DISCORD_API_URI}/webhooks/${db.webhook.id}/${db.webhook.token}`, + { content } + ); + }; + + return { status: 200 } + }, +} \ No newline at end of file