Add a result webhook push for being able to log the result in a Discord channel.
This commit is contained in:
parent
b288cbb1f8
commit
804e7c7bec
3 changed files with 59 additions and 5 deletions
53
src/api.ts
53
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<string> => {
|
|||
}
|
||||
|
||||
|
||||
|
||||
export const GET_MESSAGE = async (id: string): Promise<msg_meta> => {
|
||||
return axios.get(
|
||||
`${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(
|
||||
`${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
|
||||
}
|
||||
]
|
||||
}
|
||||
);
|
||||
};
|
||||
|
|
@ -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`
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue