0
0
Fork 0

Add an endpoint for viewing the guild bracket data through a GET request

This commit is contained in:
Oliver-Akins 2021-07-26 23:30:15 -06:00
parent 53a6e7f350
commit b2e121aa8e
3 changed files with 43 additions and 7 deletions

View file

@ -0,0 +1,33 @@
import { Request, ResponseToolkit } from "@hapi/hapi";
import { DISCORD_API_URI } from "@/constants";
import { config, db } from "@/main";
import axios from "axios";
export default {
method: `GET`, path: `/{guild_id}/bracket`,
async handler(request: Request, h: ResponseToolkit) {
let { guild_id: gID } = request.params;
let { convert_ids } = request.query;
// See if we are adding the user's conversion table to the response
let users: {[index: string]: string} = {};
if (convert_ids.toLowerCase() === `true` && config.guilds[gID].bot_token) {
for (var k in db[gID].bracket.users) {
let r = await axios.get(
`${DISCORD_API_URI}/users/${k}`,
{
headers: {
Authorization: `Bot ${config.guilds[gID].bot_token}`
}
}
);
users[k] = `${r.data.username}#${r.data.discriminator}`
};
};
return h.response({
db: db[gID].bracket,
user_conversion: users
});
},
}

View file

@ -2,6 +2,7 @@ interface channel_config {
password: string; password: string;
api_base: string; api_base: string;
quote_max: number; quote_max: number;
bot_token?: string;
delete_mode: "remove_components" | "delete_message"; delete_mode: "remove_components" | "delete_message";
params: { [index: string]: any }; params: { [index: string]: any };
} }

View file

@ -1,16 +1,18 @@
interface bracket_data {
msg: string;
channel: string;
quotes: string[];
votes: { [index: number]: number };
users: { [index: string]: number };
}
interface database { interface database {
[index: string]: { [index: string]: {
webhook: { webhook: {
id: string; id: string;
token: string; token: string;
}; };
bracket: { bracket: bracket_data;
msg: string;
channel: string;
quotes: string[];
votes: { [index: number]: number };
users: { [index: string]: number };
};
} }
} }