Skip to content

Commit

Permalink
Sorted Awards Command Interface
Browse files Browse the repository at this point in the history
  • Loading branch information
ichenglin committed Feb 25, 2023
1 parent f54cddb commit a66d4be
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 19 deletions.
2 changes: 2 additions & 0 deletions commands/button_verify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
13 changes: 7 additions & 6 deletions commands/command_awards.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -41,19 +42,19 @@ export default class AwardsCommand extends VerificationCommand {
await command_interaction.editReply({embeds: [invalid_embed]});
return;
}
const team_awards_sorted = new Map<string, string[]>();
const team_awards_classified = new Map<string, string[]>();
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]});
Expand Down
1 change: 1 addition & 0 deletions commands/command_nick.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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()});
Expand Down
34 changes: 22 additions & 12 deletions objects/robotevent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,27 @@ export default class RobotEvent {
}

public static async get_team_awards(team_id: number): Promise<TeamAward[]> {
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<any[]> {
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() {
Expand All @@ -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
}
}
12 changes: 12 additions & 0 deletions utilities/priority.ts
Original file line number Diff line number Diff line change
@@ -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;
}

}

0 comments on commit a66d4be

Please sign in to comment.