0
0
Fork 0
Quote-Bracket/src/endpoints/management/create_bracket.ts
2021-08-29 15:14:05 -06:00

139 lines
No EOL
3.4 KiB
TypeScript

import { Request, ResponseToolkit } from "@hapi/hapi";
import { loadHistory, saveHistory } from "@/utils/data";
import { getQuote } from "@/utils/quotes";
import { db, config } from "@/main";
import { BRACKET_DATA, DISCORD_API_URI } from "@/constants";
import axios from "axios";
export default {
method: `POST`, path: `/{guild_id}/bracket`,
async handler(request: Request, h: ResponseToolkit) {
let { guild_id: gID } = request.params;
let wh = db[gID].webhook;
// Create the very first quote bracket
let quotes: string[];
if (!db[gID].bracket.msg) {
quotes = await getQuote(gID, config.guilds[gID].quote_max);
} else {
await request.server.inject({
method: `DELETE`,
url: `/${gID}/bracket/${config.guilds[gID].delete_mode}`,
auth: request.auth,
});
let pastBrackets = await loadHistory(gID);
pastBrackets.push({
quotes: db[gID].bracket.quotes,
votes: db[gID].bracket.votes,
});
saveHistory(gID, pastBrackets);
// Calculate the winners from the previous bracket
let r = await request.server.inject({
url: `/${gID}/bracket/winners`,
auth: request.auth,
});;
var data = JSON.parse(r.payload);
var winner_count = data.count;
// Check if we are getting rid of all winners
if (data.eliminate_all) {
quotes = [];
winner_count = 0;
} else {
quotes = data.winners;
};
// Get enough quotes to meet the maximum for the guild
let new_quotes = await getQuote(
gID,
config.guilds[gID].quote_max - quotes.length
);
quotes.push(...new_quotes);
};
// Setup the database for the new bracket
db[gID].bracket = JSON.parse(JSON.stringify(BRACKET_DATA));
db[gID].bracket.quotes = quotes;
let message = {
content: `New Quote Bracket!`,
embeds: [
{
description: `Note: If **more than ${Math.floor(config.guilds[gID].quote_max / 2)}** of the quotes tie, they will all be eliminated, otherwise, the ones that tie will move on to the next bracket.`,
fields: quotes.map((quote, i) => { return {
name: `${i < winner_count ? '👑 ' : ''}Quote ${i + 1}:`,
value: quote,
}}),
}
],
components: [
{
type: 1,
components: [
{
type: 3,
custom_id: `quote`,
placeholder: `Choose Your Favourite Quote`,
options:quotes.map((_, i) => {
return {
label: `Quote ${i + 1}`,
value: i,
emoji: i < winner_count ? {
name: `👑`
} : null
}
}),
}
]
},
{
type: 1,
components: [
{
type: 2,
style: 1,
label: `What Did I Vote For?`,
custom_id: `showMyVote`
},
{
type: 2,
style: 4,
label: `Remove Vote`,
custom_id: `deleteVote`
}
]
}
]
};
// Add the development-only buttons if needed
if (config.discord.dev_buttons) {
message.components.push({
type: 1,
components: [
{
type: 2,
style: 1,
label: `See Count`,
custom_id: `showCount`,
},
{
type: 2,
style: 1,
label: `See Database Object`,
custom_id: `viewDB`,
}
]
});
};
let url = `${DISCORD_API_URI}/webhooks/${wh.id}/${wh.token}`;
let r = await axios.post(url, message, { params: { wait: true } });
db[gID].bracket.msg = r.data.id;
db[gID].bracket.channel = r.data.channel_id;
return h.response(r.data).code(r.status);
},
}