From a66d4bec1ef5aa4c28a9b45e897c8f14402dfc69 Mon Sep 17 00:00:00 2001 From: ichenglin Date: Sat, 25 Feb 2023 17:23:08 +0800 Subject: [PATCH] Sorted Awards Command Interface --- commands/button_verify.ts | 2 ++ commands/command_awards.ts | 13 +++++++------ commands/command_nick.ts | 1 + index.ts | 3 ++- objects/robotevent.ts | 34 ++++++++++++++++++++++------------ utilities/priority.ts | 12 ++++++++++++ 6 files changed, 46 insertions(+), 19 deletions(-) create mode 100644 utilities/priority.ts diff --git a/commands/button_verify.ts b/commands/button_verify.ts index 1a7db53..d668271 100644 --- a/commands/button_verify.ts +++ b/commands/button_verify.ts @@ -28,11 +28,13 @@ export default class VerifyButton extends VerificationButton { .setCustomId("application_team") .setLabel("Team ID (Example: 69420A)") .setMaxLength(8) + .setMinLength(2) .setStyle(TextInputStyle.Short); const verify_input_nick = new TextInputBuilder() .setCustomId("application_nick") .setLabel("Nickname") .setMaxLength(16) + .setMinLength(1) .setStyle(TextInputStyle.Short); const verify_input_reason = new TextInputBuilder() .setCustomId("application_reason") diff --git a/commands/command_awards.ts b/commands/command_awards.ts index 207bde4..0572a06 100644 --- a/commands/command_awards.ts +++ b/commands/command_awards.ts @@ -1,4 +1,5 @@ import { ChatInputCommandInteraction, EmbedBuilder, SlashCommandBuilder } from "discord.js"; +import VerificationPriority from "../utilities/priority"; import RobotEvent from "../objects/robotevent"; import VerificationCommand from "../templates/template_command"; @@ -41,19 +42,19 @@ export default class AwardsCommand extends VerificationCommand { await command_interaction.editReply({embeds: [invalid_embed]}); return; } - const team_awards_sorted = new Map(); + const team_awards_classified = new Map(); for (let award_index = 0; award_index < team_awards.length; award_index++) { const award_data = team_awards[award_index]; - if (!team_awards_sorted.has(award_data.award_name)) team_awards_sorted.set(award_data.award_name, []); - team_awards_sorted.get(award_data.award_name)?.push(award_data.award_event); + if (!team_awards_classified.has(award_data.award_name)) team_awards_classified.set(award_data.award_name, []); + team_awards_classified.get(award_data.award_name)?.push(award_data.award_event.event_name); } - + const team_awards_sorted = Array.from(team_awards_classified.entries()).map(award_data => ({award_name: award_data[0], award_events: award_data[1]})).sort((award_a, award_b) => VerificationPriority.priority_awards(award_b.award_name) - VerificationPriority.priority_awards(award_a.award_name)); const awards_embed = new EmbedBuilder() .setTitle(`🏅 ${team_data.team_name}'s Awards 🏅`) .setDescription(`**${team_data.team_name} (${team_data.team_number})** had won a total of **${team_awards.length} awards**, below are the details of the awards and their events.\n\u200B`) .addFields( - ...Array.from(team_awards_sorted.entries()).flatMap((award_data) => [ - {name: `🎖️ ${award_data[0]} x${award_data[1].length} 🎖️`, value: award_data[1].map((event_data, event_index) => `▪️ \`${event_data}\``).join("\n")} + ...team_awards_sorted.flatMap((award_data) => [ + {name: `🎖️ ${award_data.award_name} x${award_data.award_events.length} 🎖️`, value: award_data.award_events.map((event_data, event_index) => `▪️ \`${event_data}\``).join("\n")} ])) .setColor("#84cc16"); await command_interaction.editReply({embeds: [awards_embed]}); diff --git a/commands/command_nick.ts b/commands/command_nick.ts index cee5dac..26b1f9b 100644 --- a/commands/command_nick.ts +++ b/commands/command_nick.ts @@ -13,6 +13,7 @@ export default class NickCommand extends VerificationCommand { .setName("name") .setDescription("The new nickname of the user.") .setMaxLength(16) + .setMinLength(1) .setRequired(true) ); return command_builder; diff --git a/index.ts b/index.ts index 0f9d2a8..fd08f32 100644 --- a/index.ts +++ b/index.ts @@ -11,6 +11,7 @@ import VerifyModal from "./commands/modal_verify"; import InteractionCreateEvent from "./events/event_interaction_create"; import ReadyEvent from "./events/event_ready"; import Registry from "./objects/registry"; +import RobotEvent from "./objects/robotevent"; dotenv.config(); @@ -42,7 +43,7 @@ discord_rest.setToken(process.env.APPLICATION_TOKEN as string); // client const discord_client = new Client({intents: [GatewayIntentBits.Guilds]}); -for (const event_signature of verification_registry.event_signatures()) discord_client.on(event_signature.event_configuration().name, event_signature.event_trigger); +for (const event_signature of verification_registry.event_signatures()) discord_client.on(event_signature.event_configuration().name, async (...args) => await event_signature.event_trigger(...args)); (async () => { await discord_rest.put(Routes.applicationCommands(process.env.APPLICATION_ID as string), {body: verification_registry.command_signatures()}); diff --git a/objects/robotevent.ts b/objects/robotevent.ts index c22d7f5..98f092e 100644 --- a/objects/robotevent.ts +++ b/objects/robotevent.ts @@ -19,20 +19,27 @@ export default class RobotEvent { } public static async get_team_awards(team_id: number): Promise { - const api_response = await fetch(`https://www.robotevents.com/api/v2/teams/${team_id}/awards`, {headers: this.get_authorization()}).then(response => response.json()) as any; + return (await this.get_response(`teams/${team_id}/awards`)).map((award_data: any) => ({ + award_id: award_data.id, + award_name: award_data.title.match(/^([^\(]+)\s\(/)[1], + award_event: { + event_id: award_data.event.id, + event_name: award_data.event.name + } + } as TeamAward)); + } + + private static async get_response(api_path: string): Promise { + const api_response = await fetch(`https://www.robotevents.com/api/v2/${api_path}`, {headers: this.get_authorization()}).then(response => response.json()) as any; if (api_response.data.length <= 0) return []; - const team_awards: any[] = api_response.data; + const api_data: any[] = api_response.data; const api_response_continued = await Promise.all(new Array(api_response.meta.last_page - 1).fill(0).map((zero_lol, page_index) => - fetch(`https://www.robotevents.com/api/v2/teams/${team_id}/awards?page=${api_response.meta.last_page - page_index}`, {headers: this.get_authorization()}).then(response => response.json()) + fetch(`https://www.robotevents.com/api/v2/${api_path}?page=${api_response.meta.last_page - page_index}`, {headers: this.get_authorization()}).then(response => response.json()) )); for (let page_index = 0; page_index < (api_response.meta.last_page - 1); page_index++) { - team_awards.push(...api_response_continued[page_index].data); + api_data.push(...api_response_continued[page_index].data); } - return team_awards.map((award_data: any) => ({ - award_id: award_data.id, - award_name: award_data.title.match(/^([^\(]+)\s\(/)[1], - award_event: award_data.event.name - } as TeamAward)); + return api_data; } private static get_authorization() { @@ -55,7 +62,10 @@ export interface TeamData { } export interface TeamAward { - award_id: number, - award_name: string, - award_event: string + award_id: number, + award_name: string, + award_event: { + event_id: number, + event_name: string + } } \ No newline at end of file diff --git a/utilities/priority.ts b/utilities/priority.ts new file mode 100644 index 0000000..48be465 --- /dev/null +++ b/utilities/priority.ts @@ -0,0 +1,12 @@ +export default class VerificationPriority { + + public static priority_awards(award_name: string) { + const awards_priority = [/^World/, /^Division/, /^Tournament/, /^Robot Skills/, /Award$/]; + for (let pattern_index = 0; pattern_index < awards_priority.length; pattern_index++) { + if (award_name.match(awards_priority[pattern_index]) === null) continue; + return awards_priority.length - pattern_index; + } + return 0; + } + +} \ No newline at end of file