From 49c1b848b663b408c25ac3d37fdb8c8deea63f9b Mon Sep 17 00:00:00 2001 From: Oliver-Akins Date: Sun, 1 Aug 2021 02:25:17 -0600 Subject: [PATCH] Add the ability to have the tie alert go to a thread of the quote bracket message --- src/endpoints/management/is_tied.ts | 46 ++++++++++++++++++++++++++--- 1 file changed, 42 insertions(+), 4 deletions(-) diff --git a/src/endpoints/management/is_tied.ts b/src/endpoints/management/is_tied.ts index 466974e..a178edf 100644 --- a/src/endpoints/management/is_tied.ts +++ b/src/endpoints/management/is_tied.ts @@ -6,7 +6,8 @@ import axios from "axios"; export default { method: `GET`, path: `/{guild_id}/bracket/isTied`, async handler(request: Request, h: ResponseToolkit) { - let gID = request.params.guild_id; + let { guild_id: gID } = request.params; + let { try_thread } = request.query; let r = await request.server.inject({ url: `/${gID}/bracket/winners`, @@ -15,6 +16,7 @@ export default { let data = JSON.parse(r.payload); if (data.count >= 2) { + let bracket = db[gID].bracket; // Construct the primary body of the message let content = `The bracket currently has a tie between:\n> ${data.winners.join('\n~~------------------------------------~~\n> ')}`; @@ -26,16 +28,52 @@ export default { content += `\n\n**All of these quotes will advance if the tie isn't broken.**`; }; + // Define the query params that are needed all the time + let params: execute_webhook_query_params = { wait: true }; + + // Check if the user is wanting to use a thread notification + if (try_thread === `true` && config.guilds[gID].bot_token) { + try { + await axios.get( + `${DISCORD_API_URI}/channels/${bracket.msg}`, + { + headers: { + Authorization: `Bot ${config.guilds[gID].bot_token}` + } + } + ); + params.thread_id = bracket.msg; + } catch (err) { + try { + await axios.post( + `${DISCORD_API_URI}/channels/${bracket.channel}/messages/${bracket.msg}/threads`, + { + name: config.guilds[gID].thread_name ?? `Quote Bracket Discussion`, + auto_archive_duration: 1440, + }, + { + headers: { + Authorization: `Bot ${config.guilds[gID].bot_token}` + } + } + ).then(response => { + params.thread_id = bracket.msg + }); + } catch (err) {}; + }; + }; + // Add link if we know what channel the message was posted in - if (db[gID].bracket.channel) { - content += `\n\n[Jump To Bracket](https://discord.com/channels/${gID}/${db[gID].bracket.channel}/${db[gID].bracket.msg})` + let use_jump_link = config.guilds[gID].include_jump_link_for_threads ?? true; + if (db[gID].bracket.channel && (use_jump_link && params.thread_id)) { + content += `\n\n[Jump To Bracket](https://discord.com/channels/${gID}/${bracket.channel}/${bracket.msg})` }; let wh = db[gID].webhook; let r = await axios.post( `${DISCORD_API_URI}/webhooks/${wh.id}/${wh.token}`, { content }, - { params: { wait: true } } + { params } ); return h.response(r.data).code(r.status); };