Skip to content

Commit

Permalink
Merge pull request #6 from ichenglin/development-event-range
Browse files Browse the repository at this point in the history
Added Season Filter to Awards Command
  • Loading branch information
ichenglin authored Jan 28, 2024
2 parents 9e886d5 + 40f17b0 commit 1cc7f0a
Show file tree
Hide file tree
Showing 12 changed files with 316 additions and 71 deletions.
4 changes: 2 additions & 2 deletions commands/command_assign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ export default class AssignCommand extends VerificationCommand {
.addFields([(updated_team) && {
name: `🏦 Team Number 🏦`,
value: [
(team_data !== undefined) ? `${VerificationDisplay.LIST_MARKER} Country: ${CountryFlag.get_flag(team_data.team_country)} \`${team_data.team_country}\`` : undefined,
(team_data !== undefined) ? `${VerificationDisplay.LIST_MARKER} Organization: \`${team_data.team_organization}\`` : undefined,
(team_data !== undefined) ? `${VerificationDisplay.EMOJI.LIST_MARKER} Country: ${CountryFlag.get_flag(team_data.team_country)} \`${team_data.team_country}\`` : undefined,
(team_data !== undefined) ? `${VerificationDisplay.EMOJI.LIST_MARKER} Organization: \`${team_data.team_organization}\`` : undefined,
`\`\`\`diff\n- ${user_data.user_team_number}\n+ ${assign_team}\`\`\``
].join("\n")
}, (updated_name) && {
Expand Down
98 changes: 86 additions & 12 deletions commands/command_awards.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { ChatInputCommandInteraction, EmbedBuilder, SlashCommandBuilder } from "discord.js";
import { ActionRowBuilder, ChatInputCommandInteraction, ComponentType, EmbedBuilder, Message, SlashCommandBuilder, StringSelectMenuBuilder, StringSelectMenuOptionBuilder } from "discord.js";
import VerificationPriority from "../utilities/priority";
import RobotEvent from "../objects/robotevent";
import RobotEvent, { EventDataSimplified, SeasonData, TeamData } from "../objects/robotevent";
import VerificationCommand from "../templates/template_command";
import VerificationDisplay from "../utilities/display";
import PreProcess from "../objects/preprocess";

export default class AwardsCommand extends VerificationCommand {

Expand Down Expand Up @@ -43,21 +44,50 @@ export default class AwardsCommand extends VerificationCommand {
await command_interaction.editReply({embeds: [invalid_embed]});
return;
}
const team_awards_classified = new Map<string, string[]>();
const team_awards_classified = new Map<string, EventDataSimplified[]>();
for (let award_index = 0; award_index < team_awards.length; award_index++) {
const award_data = team_awards[award_index];
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);
team_awards_classified.get(award_data.award_name)?.push(award_data.award_event);
}
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 team_seasons: SeasonData[] = [];
const team_awards_sorted = Array.from(team_awards_classified.entries()).map(award_data => ({
award_name: award_data[0],
award_events: award_data[1].map(award_event => {
const event_season = PreProcess.get_event_season(award_event.event_id, team_data.team_program.program_code) as SeasonData;
if (team_seasons.find(team_season => team_season.season_id === event_season.season_id) === undefined) team_seasons.push(event_season);
return {
event_data: award_event,
event_season: event_season
};
})})
).sort((award_a, award_b) => VerificationPriority.priority_awards(award_b.award_name) - VerificationPriority.priority_awards(award_a.award_name));
// send embed
await this.awards_response(command_interaction, [], team_data, team_awards_sorted, team_seasons, []);
}

private async awards_response(command_interaction: ChatInputCommandInteraction, command_messages_old: Message<boolean>[], team_data: TeamData, team_awards: TeamAward[], team_seasons: SeasonData[], seasons_filter: number[]): Promise<void> {
const filter_active = (seasons_filter.length > 0 && seasons_filter.length < team_seasons.length);
const filter_seasons = seasons_filter.map(filter_season_id => team_seasons.find(team_season_data => team_season_data.season_id === filter_season_id) as SeasonData);
const team_awards_filtered = (!filter_active) ? team_awards : team_awards.map(award_data => ({
award_name: award_data.award_name,
award_events: award_data.award_events.filter(event_data => seasons_filter.includes(event_data.event_season.season_id))
} as TeamAward)).filter(award_data => award_data.award_events.length > 0);
// generate embed
const awards_amount_all = team_awards.map( award_data => award_data.award_events.length).reduce((total_events, award_events) => (total_events + award_events), 0);
const awards_amount_filtered = team_awards_filtered.map(award_data => award_data.award_events.length).reduce((total_events, award_events) => (total_events + award_events), 0);
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`)
.setDescription([
`**${team_data.team_name} (${team_data.team_number})** had won a total of **${awards_amount_all} awards**, below are the details of the awards and their events.`,
(filter_active ? `\n\n✨ **Applied Filter(s):** ${VerificationDisplay.string_list(filter_seasons.map(season_data => `\`${season_data.season_name}\``))} (${awards_amount_all - awards_amount_filtered} awards hidden)` : ""),
`\n\u200B`
].join(""))
.addFields(
...team_awards_sorted.map((award_data) => {
...team_awards_filtered.map((award_data) => {
const message_limit = "(and more events...)\n";
let award_events_display = "";
let award_events = award_data.award_events.map((event_data, event_index) => `${VerificationDisplay.LIST_MARKER} \`${event_data}\`\n`);
let award_events = award_data.award_events.map((event_data, event_index) => `${VerificationDisplay.EMOJI.LIST_MARKER} __**\`(${event_data.event_season.season_name})\`**__ \`${event_data.event_data.event_name}\`\n`);
for (let event_index = 0; event_index < award_events.length; event_index++) {
const event_string = award_events[event_index];
const display_length_new = (award_events_display.length + event_string.length + message_limit.length);
Expand All @@ -72,10 +102,54 @@ export default class AwardsCommand extends VerificationCommand {
.setTimestamp()
.setFooter({text: `requested by ${command_interaction.user.tag}`, iconURL: command_interaction.client.user.displayAvatarURL()})
.setColor("#84cc16");
// send embed
const embed_safe = VerificationDisplay.embed_safe(awards_embed);
await command_interaction.editReply(embed_safe[0]);
for (const embed_children of embed_safe.slice(1)) await command_interaction.channel?.send(embed_children);
// generate dropdown
const awards_filter = new StringSelectMenuBuilder()
.setCustomId("awards-season")
.setPlaceholder("Filter by Season(s)")
.setMinValues(0)
.setMaxValues(team_seasons.length)
.addOptions(team_seasons.sort((season_a, season_b) => season_b.season_id - season_a.season_id).map(season_data => {
const season_name_matcher = season_data.season_name.match(/^\s*(\w+) (\d{4}-\d{4}): ([\w ]*\w)\s*$/) as RegExpMatchArray;
return new StringSelectMenuOptionBuilder()
.setLabel(`${season_name_matcher[1]} ${season_name_matcher[2]}`)
.setDescription(season_name_matcher[3])
.setEmoji(VerificationDisplay.EMOJI.VRC_LOGO)
.setValue(season_data.season_id.toString());
}));
const awards_actionrow = new ActionRowBuilder().addComponents(awards_filter);
// send embed and dropdown
const embed_messages = await VerificationDisplay.embed_editreply(command_interaction, VerificationDisplay.embed_safe(awards_embed, undefined, [awards_actionrow]), command_messages_old);
// reply to dropdown
const embed_collector = embed_messages[embed_messages.length - 1].createMessageComponentCollector({
componentType: ComponentType.StringSelect,
filter: (component_interaction) => component_interaction.user.id === command_interaction.user.id,
time: (120 * 1E3)
});
embed_collector.on("collect", async (component_interaction) => {
await component_interaction.deferUpdate();
embed_collector.removeAllListeners();
await this.awards_response(command_interaction, embed_messages, team_data, team_awards, team_seasons, component_interaction.values.map(collector_value => parseInt(collector_value)));
});
embed_collector.on("ignore", async (component_interaction) => {
const prohibited_embed = new EmbedBuilder()
.setTitle("⛔ No Permission ⛔")
.setDescription(`This embed belongs to <@${command_interaction.user.id}>, you are not allowed to use this!`)
.setColor("#ef4444");
await component_interaction.reply({embeds: [prohibited_embed], ephemeral: true});
});
embed_collector.on("end", async () => {
awards_filter.setDisabled(true);
awards_filter.setPlaceholder("(Filter Expired After 2 Minutes of Inactivity)")
await embed_messages[embed_messages.length - 1].edit({components: [awards_actionrow as any]});
});
}

}

interface TeamAward {
award_name: string;
award_events: {
event_data: EventDataSimplified;
event_season: SeasonData;
}[];
}
10 changes: 5 additions & 5 deletions commands/command_help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,24 @@ export default class HelpCommand extends VerificationCommand {
name: `🏷️ ${command_full}`,
value: [
`\`${command_signature.description}\``,
`${VerificationDisplay.LIST_MARKER} Prerequisites: \`${(command_signature.default_member_permissions !== undefined ? `⚠️ ${command_permissions} Permission` : "✅ None")}\``,
`${VerificationDisplay.LIST_MARKER} Compatibility: \`${(command_signature.dm_permission === false) ? "⚠️ Guild Only" : "✅ All"}\``
`${VerificationDisplay.EMOJI.LIST_MARKER} Prerequisites: \`${(command_signature.default_member_permissions !== undefined ? `⚠️ ${command_permissions} Permission` : "✅ None")}\``,
`${VerificationDisplay.EMOJI.LIST_MARKER} Compatibility: \`${(command_signature.dm_permission === false) ? "⚠️ Guild Only" : "✅ All"}\``
].join("\n")
};
}))
.addFields({
name: "🔔 Ping Team (#[team])",
value: [
"\`Pings all users from a team in text messages.\`",
`${VerificationDisplay.LIST_MARKER} Prerequisites: \`✅ None\``,
`${VerificationDisplay.LIST_MARKER} Compatibility: \`⚠️ Guild Only\``
`${VerificationDisplay.EMOJI.LIST_MARKER} Prerequisites: \`✅ None\``,
`${VerificationDisplay.EMOJI.LIST_MARKER} Compatibility: \`⚠️ Guild Only\``
].join("\n")
})
.setTimestamp()
.setFooter({text: `requested by ${command_interaction.user.tag}`, iconURL: command_interaction.client.user.displayAvatarURL()})
.setColor("#84cc16");
// send embed
const embed_safe = VerificationDisplay.embed_safe(help_embed);
const embed_safe = VerificationDisplay.embed_safe(help_embed, undefined, undefined);
await command_interaction.editReply(embed_safe[0]);
for (const embed_children of embed_safe.slice(1)) await command_interaction.channel?.send(embed_children);
}
Expand Down
6 changes: 3 additions & 3 deletions commands/command_roster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ export default class RosterCommand extends VerificationCommand {
name: `${loop_team.team_number}`,
value: [
`\`${loop_team_data.team_name}\``,
`${VerificationDisplay.LIST_MARKER} Country: ${CountryFlag.get_flag(loop_team_data.team_country)}`,
`${VerificationDisplay.LIST_MARKER} Grade: \`${loop_team_data.team_grade}\``,
`${VerificationDisplay.EMOJI.LIST_MARKER} Country: ${CountryFlag.get_flag(loop_team_data.team_country)}`,
`${VerificationDisplay.EMOJI.LIST_MARKER} Grade: \`${loop_team_data.team_grade}\``,
...loop_team.team_users.map(loop_user => `<@${loop_user.user_id}>`)
].join("\n"),
inline: true
Expand All @@ -50,7 +50,7 @@ export default class RosterCommand extends VerificationCommand {
.setFooter({text: `requested by ${command_interaction.user.tag}`, iconURL: command_interaction.client.user.displayAvatarURL()})
.setColor("#84cc16");
// send embed
const embed_safe = VerificationDisplay.embed_safe(roster_embed);
const embed_safe = VerificationDisplay.embed_safe(roster_embed, undefined, undefined);
await command_interaction.editReply(embed_safe[0]);
for (const embed_children of embed_safe.slice(1)) await command_interaction.channel?.send(embed_children);
}
Expand Down
12 changes: 6 additions & 6 deletions commands/command_skills.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AttachmentBuilder, ChatInputCommandInteraction, EmbedBuilder, InteractionEditReplyOptions, MessagePayload, SlashCommandBuilder } from "discord.js";
import * as NodeChartJS from "chartjs-node-canvas";
import RobotEvent, { SeasonData } from "../objects/robotevent";
import RobotEvent, { SeasonDataSimplified } from "../objects/robotevent";
import VerificationCommand from "../templates/template_command";
import VerificationDisplay from "../utilities/display";

Expand Down Expand Up @@ -43,7 +43,7 @@ export default class SkillsCommand extends VerificationCommand {
await command_interaction.editReply({embeds: [invalid_embed]});
return;
}
const team_season_data: SeasonData[] = [];
const team_season_data: SeasonDataSimplified[] = [];
for (let skill_index = 0; skill_index < team_skills.length; skill_index++) {
const skill_data = team_skills[skill_index];
// if driver score and programming score is both 0, won't exist in season skill
Expand All @@ -67,9 +67,9 @@ export default class SkillsCommand extends VerificationCommand {
.addFields(
...team_season_skills_sorted.map((skill_data) => (
{name: `🏁 ${skill_data.season_data.season_name} 🏁`, value: [
`${VerificationDisplay.LIST_MARKER} Rank: \`#${skill_data.season_skills.skills_rank}\` out of ${skill_data.season_skills.skills_entries} entries (\`Top ${Math.ceil(skill_data.season_skills.skills_rank / skill_data.season_skills.skills_entries * 100)}%\`)`,
`${VerificationDisplay.LIST_MARKER} Driver: \`${skill_data.season_skills.skills_score.driver_score}\``,
`${VerificationDisplay.LIST_MARKER} Programming: \`${skill_data.season_skills.skills_score.programming_score}\``
`${VerificationDisplay.EMOJI.LIST_MARKER} Rank: \`#${skill_data.season_skills.skills_rank}\` out of ${skill_data.season_skills.skills_entries} entries (\`Top ${Math.ceil(skill_data.season_skills.skills_rank / skill_data.season_skills.skills_entries * 100)}%\`)`,
`${VerificationDisplay.EMOJI.LIST_MARKER} Driver: \`${skill_data.season_skills.skills_score.driver_score}\``,
`${VerificationDisplay.EMOJI.LIST_MARKER} Programming: \`${skill_data.season_skills.skills_score.programming_score}\``
].join("\n")}
)))
.setImage("attachment://skills_graph.png")
Expand Down Expand Up @@ -116,7 +116,7 @@ export default class SkillsCommand extends VerificationCommand {
}}});
const skills_image = new AttachmentBuilder(skills_buffer, {name: "skills_graph.png"});
// send embed
const embed_safe = VerificationDisplay.embed_safe(skills_embed, [skills_image]);
const embed_safe = VerificationDisplay.embed_safe(skills_embed, [skills_image], undefined);
await command_interaction.editReply(embed_safe[0]);
for (const embed_children of embed_safe.slice(1)) await command_interaction.channel?.send(embed_children);
}
Expand Down
10 changes: 5 additions & 5 deletions commands/command_upcoming.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ export default class UpcomingCommand extends VerificationCommand {
return {
name: `📌 ${event_data.event_name} 📌`,
value: [
`${VerificationDisplay.LIST_MARKER} Location: ${CountryFlag.get_flag(event_data.event_location.address_country)} \`${event_region}\``,
`${VerificationDisplay.LIST_MARKER} Date: <t:${Math.floor(event_data.event_date.date_begin / 1000)}:R>`,
`${VerificationDisplay.LIST_MARKER} Teams: \`${event_teams_list}\` and \`${event_teams_excluded}\` more team(s)...`,
`${VerificationDisplay.LIST_MARKER} Links: ${event_links.map(link_data => `[**\`${link_data.link_name}\`**](${link_data.link_url})`).join(", ")}`,
`${VerificationDisplay.EMOJI.LIST_MARKER} Location: ${CountryFlag.get_flag(event_data.event_location.address_country)} \`${event_region}\``,
`${VerificationDisplay.EMOJI.LIST_MARKER} Date: <t:${Math.floor(event_data.event_date.date_begin / 1000)}:R>`,
`${VerificationDisplay.EMOJI.LIST_MARKER} Teams: \`${event_teams_list}\` and \`${event_teams_excluded}\` more team(s)...`,
`${VerificationDisplay.EMOJI.LIST_MARKER} Links: ${event_links.map(link_data => `[**\`${link_data.link_name}\`**](${link_data.link_url})`).join(", ")}`,
`\u200B`
].join("\n")
}
Expand All @@ -78,7 +78,7 @@ export default class UpcomingCommand extends VerificationCommand {
.setFooter({text: `requested by ${command_interaction.user.tag}`, iconURL: command_interaction.client.user.displayAvatarURL()})
.setColor("#84cc16");
// send embed
const embed_safe = VerificationDisplay.embed_safe(events_embed);
const embed_safe = VerificationDisplay.embed_safe(events_embed, undefined, undefined);
await command_interaction.editReply(embed_safe[0]);
for (const embed_children of embed_safe.slice(1)) await command_interaction.channel?.send(embed_children);
}
Expand Down
Loading

0 comments on commit 1cc7f0a

Please sign in to comment.