0
0
Fork 0

Add a result webhook push for being able to log the result in a Discord channel.

This commit is contained in:
Tyler-A 2020-04-21 21:50:18 -06:00
parent b288cbb1f8
commit 804e7c7bec
3 changed files with 59 additions and 5 deletions

View file

@ -1,6 +1,7 @@
const axios = require("axios").default; const axios = require("axios").default;
import { import {
QUOTE_URL, QUOTE_URL,
RESULT_WEBHOOK,
DISCORD_WEBHOOK, DISCORD_WEBHOOK,
DISCORD_API_BASE, DISCORD_API_BASE,
DISCORD_CHANNEL_ID, DISCORD_CHANNEL_ID,
@ -31,6 +32,7 @@ export const GET_QUOTE = async (count = 1): Promise<string> => {
} }
export const GET_MESSAGE = async (id: string): Promise<msg_meta> => { export const GET_MESSAGE = async (id: string): Promise<msg_meta> => {
return axios.get( return axios.get(
`${DISCORD_API_BASE}/channels/${DISCORD_CHANNEL_ID}/messages/${id}`, `${DISCORD_API_BASE}/channels/${DISCORD_CHANNEL_ID}/messages/${id}`,
@ -61,15 +63,60 @@ export const GET_MESSAGE = async (id: string): Promise<msg_meta> => {
export const WEBHOOK_PUSH = async (embed: any): Promise<string> => { const WEBHOOK_POST = async (webhook: string, username: string, embed: any): Promise<object> => {
console.log(2)
return await axios.post( return await axios.post(
`${DISCORD_WEBHOOK}?wait=true`, `${DISCORD_WEBHOOK}?wait=true`,
{ {
username: DISCORD_WEBHOOK_USERNAME, username: username,
embeds: [embed] embeds: [embed]
} }
) )
.then((response: any) => { .then((response: any) => {
return response.data.id; console.log(3)
return response.data;
}).catch((err:any) => {throw err}); }).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
}
]
}
);
};

View file

@ -11,6 +11,9 @@ export const DISCORD_WEBHOOK: string = ``;
// default name from within Discord // default name from within Discord
export const DISCORD_WEBHOOK_USERNAME: string|undefined = undefined; 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 // The file names for the database operations
export const DB_NAME: string = `used_quotes.json` export const DB_NAME: string = `used_quotes.json`
export const MSG_ID_FILE: string = `msg_id` export const MSG_ID_FILE: string = `msg_id`

View file

@ -1,5 +1,5 @@
import { LOAD_MSG_ID, WRITE_MSG_ID } from "./database"; 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"; import { EMOJI_A_ID, EMOJI_B_ID, EMOJI_B_NAME, EMOJI_A_NAME } from "./config";
const MAIN = async () => { const MAIN = async () => {
@ -47,6 +47,10 @@ const MAIN = async () => {
winning_emoji = winning_emoji!; 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; var new_quote_a: string, new_quote_b: string;
// Get new quotes // Get new quotes
switch (winning_emoji) { switch (winning_emoji) {
@ -91,7 +95,7 @@ const MAIN = async () => {
] ]
} }
// Post to webhook and store new message ID // 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); WRITE_MSG_ID(new_msg_id);
}; };