From 4cacc2cb069dbf4270efbc3ea572e0541ec0d04f Mon Sep 17 00:00:00 2001 From: ProgramPhoenix <93595010+ProgramPhoenix@users.noreply.github.com> Date: Tue, 28 Nov 2023 12:59:05 +0100 Subject: [PATCH 1/4] seperated different things into different files and made year automatically detected for the aoc command --- modules/aoc/aocCommand.ts | 6 ++-- modules/aoc/aocEmbedGenerator.ts | 32 +++++++++++++++++ modules/aoc/aocLeaderboardParser.ts | 56 ----------------------------- modules/aoc/aocTask.ts | 2 +- modules/aoc/aocTypes.ts | 28 +++++++++++++++ 5 files changed, 65 insertions(+), 59 deletions(-) create mode 100644 modules/aoc/aocEmbedGenerator.ts create mode 100644 modules/aoc/aocTypes.ts diff --git a/modules/aoc/aocCommand.ts b/modules/aoc/aocCommand.ts index 110833b..be6213a 100644 --- a/modules/aoc/aocCommand.ts +++ b/modules/aoc/aocCommand.ts @@ -1,7 +1,8 @@ import {ISlashCommand} from "../../types"; import {CommandInteraction} from "discord.js"; -import {embedLeaderboard} from "./aocLeaderboardParser"; +import {embedLeaderboard} from "./aocEmbedGenerator"; import {get} from "../../lib/configmanager"; +import {DateTime} from "luxon"; /** * Slash command definition for /aoc, a command that sends the current leaderboard @@ -12,7 +13,8 @@ export default { description: "Returns the Advent of Code Leaderboard" }, handler: async function(interaction: CommandInteraction) { - const embed = await embedLeaderboard(get("id", "aoc") as number, get("year", "aoc") as number); + const now = DateTime.now().setZone("Europe/Berlin"); + const embed = await embedLeaderboard(get("id", "aoc") as number, now.year); await interaction.reply({ embeds: [embed] }); } } as ISlashCommand; diff --git a/modules/aoc/aocEmbedGenerator.ts b/modules/aoc/aocEmbedGenerator.ts new file mode 100644 index 0000000..23131a5 --- /dev/null +++ b/modules/aoc/aocEmbedGenerator.ts @@ -0,0 +1,32 @@ +import {EmbedBuilder} from "discord.js"; +import {requestLeaderboard} from "./aocLeaderboardParser"; + + +/** + * Generates the embed for displaying a leaderboard + * + * @param id ID of the leaderboard + * @param year Year of this AoC competition + * @returns The Embed for showing the leaderboard + */ +export async function embedLeaderboard(id:number, year: number) : Promise { + const members = await requestLeaderboard(id, year); + const map = {} as {[key:string]: string}; + for(const m of members) { + map[m.name] = ""; + for (const k in m.completion_day_level) { + let emoji = ":eight_pointed_black_star:"; + if (m.completion_day_level[k]["1"].get_star_ts != 0) { + if (m.completion_day_level[k]["2"].get_star_ts != 0) { + emoji = ":star2:"; + } else { + emoji = ":star:"; + } + } + map[m.name] = map[m.name] + emoji; + } + } + return new EmbedBuilder().setURL("https://adventofcode.com").setTitle("Advent of Code Leaderboard") + .addFields({name:"Ranking", value:members.map(e => { return e.name + ": " + e.local_score + map[e.name]; }).join("\n")}); + +} diff --git a/modules/aoc/aocLeaderboardParser.ts b/modules/aoc/aocLeaderboardParser.ts index c29fbcb..c3e5bf5 100644 --- a/modules/aoc/aocLeaderboardParser.ts +++ b/modules/aoc/aocLeaderboardParser.ts @@ -1,36 +1,8 @@ import axios from "axios"; -import {EmbedBuilder} from "discord.js"; const url = "https://adventofcode.com/"; const path = "/leaderboard/private/view/"; -interface aoc_data { - event: string - owner_id: number - members: { - [id:number]:aoc_member - } -} -interface aoc_member { - last_start_ts: number - id: number - local_score: number - stars: number - name: string - global_score: number - completion_day_level: { - [day:number]: { - 1: { - get_star_ts: number - star_index: number - } - 2: { - get_star_ts: number - star_index: number - } - } - } -} /** * Gets the current leaderboard data from the AoC servers @@ -55,33 +27,5 @@ export async function requestLeaderboard(id: number, year: number) : Promise { - const members = await requestLeaderboard(id, year); - const map = {} as {[key:string]: string}; - for(const m of members) { - map[m.name] = ""; - for (const k in m.completion_day_level) { - let a = ":eight_pointed_black_star:"; - if (m.completion_day_level[k]["1"].get_star_ts != 0) { - if (m.completion_day_level[k]["2"].get_star_ts != 0) { - a = ":star2:"; - } else { - a = ":star:"; - } - } - map[m.name] = map[m.name] + a; - } - } - const embed = new EmbedBuilder().setURL("https://adventofcode.com").setTitle("Advent of Code Leaderboard") - .addFields({name:"Ranking", value:members.map(e => { return e.name + ": " + e.local_score + map[e.name]; }).join("\n")}); - return embed; -} diff --git a/modules/aoc/aocTask.ts b/modules/aoc/aocTask.ts index 77dbf01..c80846c 100644 --- a/modules/aoc/aocTask.ts +++ b/modules/aoc/aocTask.ts @@ -1,6 +1,6 @@ import {Client, TextChannel} from "discord.js"; import {TaskExecutor} from "../../types"; -import {embedLeaderboard} from "./aocLeaderboardParser"; +import {embedLeaderboard} from "./aocEmbedGenerator"; import {get} from "../../lib/configmanager"; import {DateTime} from "luxon"; diff --git a/modules/aoc/aocTypes.ts b/modules/aoc/aocTypes.ts new file mode 100644 index 0000000..14792e1 --- /dev/null +++ b/modules/aoc/aocTypes.ts @@ -0,0 +1,28 @@ +interface aoc_data { + event: string + owner_id: number + members: { + [id:number]:aoc_member + } +} + +interface aoc_member { + last_start_ts: number + id: number + local_score: number + stars: number + name: string + global_score: number + completion_day_level: { + [day:number]: { + 1: { + get_star_ts: number + star_index: number + } + 2: { + get_star_ts: number + star_index: number + } + } + } +} \ No newline at end of file From 3fc6511ffdf8cfd439f3405b1fd4faf872a0644c Mon Sep 17 00:00:00 2001 From: ProgramPhoenix <93595010+ProgramPhoenix@users.noreply.github.com> Date: Tue, 28 Nov 2023 13:07:33 +0100 Subject: [PATCH 2/4] seperated different things into different files and made year automatically detected for the aoc command --- modules/aoc/aocLeaderboardParser.ts | 4 ++-- modules/aoc/aocTypes.ts | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/modules/aoc/aocLeaderboardParser.ts b/modules/aoc/aocLeaderboardParser.ts index c3e5bf5..f2ef76c 100644 --- a/modules/aoc/aocLeaderboardParser.ts +++ b/modules/aoc/aocLeaderboardParser.ts @@ -11,10 +11,10 @@ const path = "/leaderboard/private/view/"; * @param year Year of this AoC competition * @returns Array of members of this leaderboard */ -export async function requestLeaderboard(id: number, year: number) : Promise { +export async function requestLeaderboard(id: number, year: number) : Promise { const concaturl = url + year + path + id + ".json"; const answer = await axios.get(concaturl, {headers: {'Content-Type' : 'application/json', 'cookie':'session=' + process.env.AOC_SESSION}}); - const data = answer.data as aoc_data; + const data = answer.data as aocData; const members = Object.values(data.members); members.sort((a, b)=> { if (a.local_score < b.local_score) { diff --git a/modules/aoc/aocTypes.ts b/modules/aoc/aocTypes.ts index 14792e1..b54c65b 100644 --- a/modules/aoc/aocTypes.ts +++ b/modules/aoc/aocTypes.ts @@ -1,12 +1,12 @@ -interface aoc_data { +interface aocData { event: string owner_id: number members: { - [id:number]:aoc_member + [id:number]:aocMember } } -interface aoc_member { +interface aocMember { last_start_ts: number id: number local_score: number From bb19ebaac5c83173add84a540eb8ae57b91c6ce2 Mon Sep 17 00:00:00 2001 From: ProgramPhoenix <93595010+ProgramPhoenix@users.noreply.github.com> Date: Tue, 28 Nov 2023 13:18:58 +0100 Subject: [PATCH 3/4] added year option to command --- modules/aoc/aocCommand.ts | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/modules/aoc/aocCommand.ts b/modules/aoc/aocCommand.ts index be6213a..586f3b2 100644 --- a/modules/aoc/aocCommand.ts +++ b/modules/aoc/aocCommand.ts @@ -1,5 +1,5 @@ import {ISlashCommand} from "../../types"; -import {CommandInteraction} from "discord.js"; +import {ApplicationCommandOptionType, CommandInteraction, CommandInteractionOptionResolver} from "discord.js"; import {embedLeaderboard} from "./aocEmbedGenerator"; import {get} from "../../lib/configmanager"; import {DateTime} from "luxon"; @@ -10,11 +10,23 @@ import {DateTime} from "luxon"; export default { command: { name: "aoc", - description: "Returns the Advent of Code Leaderboard" + description: "Returns the Advent of Code Leaderboard", + options:[ + { + type: ApplicationCommandOptionType.Integer, + name: "year", + description: "the year of the Advent of Code", + min_value: 2022, + max_value: DateTime.now().setZone("Europe/Berlin").year, + required: false + } + ] }, handler: async function(interaction: CommandInteraction) { - const now = DateTime.now().setZone("Europe/Berlin"); - const embed = await embedLeaderboard(get("id", "aoc") as number, now.year); + const options = interaction.options as CommandInteractionOptionResolver; + const requestedYear = options.getInteger("year", false); + const year = (requestedYear === undefined)? DateTime.now().setZone("Europe/Berlin").year : requestedYear; + const embed = await embedLeaderboard(get("id", "aoc") as number, year); await interaction.reply({ embeds: [embed] }); } } as ISlashCommand; From 980db062a0a28143c952baadd6383ae518c3e6f6 Mon Sep 17 00:00:00 2001 From: ProgramPhoenix <93595010+ProgramPhoenix@users.noreply.github.com> Date: Tue, 28 Nov 2023 13:23:38 +0100 Subject: [PATCH 4/4] fixed lint --- modules/aoc/aocLeaderboardParser.ts | 1 + modules/aoc/aocTypes.ts | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/modules/aoc/aocLeaderboardParser.ts b/modules/aoc/aocLeaderboardParser.ts index f2ef76c..ab62fb8 100644 --- a/modules/aoc/aocLeaderboardParser.ts +++ b/modules/aoc/aocLeaderboardParser.ts @@ -1,4 +1,5 @@ import axios from "axios"; +import {aocData, aocMember} from "./aocTypes"; const url = "https://adventofcode.com/"; const path = "/leaderboard/private/view/"; diff --git a/modules/aoc/aocTypes.ts b/modules/aoc/aocTypes.ts index b54c65b..5b8848e 100644 --- a/modules/aoc/aocTypes.ts +++ b/modules/aoc/aocTypes.ts @@ -1,4 +1,4 @@ -interface aocData { +export interface aocData { event: string owner_id: number members: { @@ -6,7 +6,7 @@ interface aocData { } } -interface aocMember { +export interface aocMember { last_start_ts: number id: number local_score: number