-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
minor improvemts for the aoc command (#119)
* seperated different things into different files and made year automatically detected for the aoc command * added year option to command * fixed lint
- Loading branch information
1 parent
7c94dd0
commit 21c7c22
Showing
5 changed files
with
82 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,32 @@ | ||
import {ISlashCommand} from "../../types"; | ||
import {CommandInteraction} from "discord.js"; | ||
import {embedLeaderboard} from "./aocLeaderboardParser"; | ||
import {ApplicationCommandOptionType, CommandInteraction, CommandInteractionOptionResolver} from "discord.js"; | ||
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 | ||
*/ | ||
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 embed = await embedLeaderboard(get("id", "aoc") as number, get("year", "aoc") as number); | ||
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<EmbedBuilder> { | ||
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")}); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
export interface aocData { | ||
event: string | ||
owner_id: number | ||
members: { | ||
[id:number]:aocMember | ||
} | ||
} | ||
|
||
export interface aocMember { | ||
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 | ||
} | ||
} | ||
} | ||
} |