From 804e7c7bec3654b136e56a69d86dd5cb6c236733 Mon Sep 17 00:00:00 2001 From: Tyler-A Date: Tue, 21 Apr 2020 21:50:18 -0600 Subject: [PATCH] Add a result webhook push for being able to log the result in a Discord channel. --- src/api.ts | 53 +++++++++++++++++++++++++++++++++++++++--- src/config.template.ts | 3 +++ src/main.ts | 8 +++++-- 3 files changed, 59 insertions(+), 5 deletions(-) diff --git a/src/api.ts b/src/api.ts index 798f490..0fff18a 100644 --- a/src/api.ts +++ b/src/api.ts @@ -1,6 +1,7 @@ const axios = require("axios").default; import { QUOTE_URL, + RESULT_WEBHOOK, DISCORD_WEBHOOK, DISCORD_API_BASE, DISCORD_CHANNEL_ID, @@ -31,6 +32,7 @@ export const GET_QUOTE = async (count = 1): Promise => { } + export const GET_MESSAGE = async (id: string): Promise => { return axios.get( `${DISCORD_API_BASE}/channels/${DISCORD_CHANNEL_ID}/messages/${id}`, @@ -61,15 +63,60 @@ export const GET_MESSAGE = async (id: string): Promise => { -export const WEBHOOK_PUSH = async (embed: any): Promise => { +const WEBHOOK_POST = async (webhook: string, username: string, embed: any): Promise => { + console.log(2) return await axios.post( `${DISCORD_WEBHOOK}?wait=true`, { - username: DISCORD_WEBHOOK_USERNAME, + username: username, embeds: [embed] } ) .then((response: any) => { - return response.data.id; + console.log(3) + return response.data; }).catch((err:any) => {throw err}); +}; + + + +export const POST_NEW_BRACKET = async (embed: any) => { + // @ts-ignore + return (await WEBHOOK_POST(DISCORD_WEBHOOK, DISCORD_WEBHOOK_USERNAME, embed)).id +} + + +export const POST_WINNING_QUOTE = async (ctx: msg_meta, won: reaction|"TIE"|"NO_DATA") => { + let result: string; + console.log(1) + switch (won) { + case "NO_DATA": + case "TIE": + result = won; + break; + default: + result = `<:${won.emoji.name}:${won.emoji.id}>` + break; + } + console.log(4) + WEBHOOK_POST( + RESULT_WEBHOOK, + "Bracket Result", + { + title: "Bracket Results", + description: `Bracket Result: ${result}`, + fields: [ + { + name: `Quote A:`, + value: ctx.quote_a.value, + inline: false + }, + { + name: `Quote B:`, + value: ctx.quote_b.value, + inline: false + } + ] + } + ); }; \ No newline at end of file diff --git a/src/config.template.ts b/src/config.template.ts index fd06e5b..9c71364 100644 --- a/src/config.template.ts +++ b/src/config.template.ts @@ -11,6 +11,9 @@ export const DISCORD_WEBHOOK: string = ``; // default name from within Discord export const DISCORD_WEBHOOK_USERNAME: string|undefined = undefined; +// The Discord webhook that the results of each quote +export const RESULT_WEBHOOK: string = ``; + // The file names for the database operations export const DB_NAME: string = `used_quotes.json` export const MSG_ID_FILE: string = `msg_id` diff --git a/src/main.ts b/src/main.ts index 032e549..5099b87 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,5 +1,5 @@ import { LOAD_MSG_ID, WRITE_MSG_ID } from "./database"; -import { GET_MESSAGE, GET_QUOTE, WEBHOOK_PUSH } from "./api"; +import { GET_MESSAGE, GET_QUOTE, POST_WINNING_QUOTE, POST_NEW_BRACKET } from "./api"; import { EMOJI_A_ID, EMOJI_B_ID, EMOJI_B_NAME, EMOJI_A_NAME } from "./config"; const MAIN = async () => { @@ -47,6 +47,10 @@ const MAIN = async () => { winning_emoji = winning_emoji!; + // Push the winning quote to the result webhook + await POST_WINNING_QUOTE(msg, winning_emoji); + + var new_quote_a: string, new_quote_b: string; // Get new quotes switch (winning_emoji) { @@ -91,7 +95,7 @@ const MAIN = async () => { ] } // Post to webhook and store new message ID - let new_msg_id = await WEBHOOK_PUSH(embed); + let new_msg_id = await POST_NEW_BRACKET(embed); WRITE_MSG_ID(new_msg_id); };