Skip to content

Commit

Permalink
Fix /lc sync subcommand (#46)
Browse files Browse the repository at this point in the history
* wip

* Update api.ts
  • Loading branch information
EthanThatOneKid committed Oct 17, 2023
1 parent 3ac6b70 commit 041692a
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 48 deletions.
7 changes: 0 additions & 7 deletions api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,3 @@ export function makeOnListen(
function makeInviteURL(applicationID: string) {
return `https://discord.com/api/oauth2/authorize?client_id=${applicationID}&scope=applications.commands`;
}

fetch(
"http://127.0.0.1:8080/webhook?season_id=01H8T4MM00BQHHK7VTTEJE1WAS&webhook_url=https://discord.com/api/webhooks/1128189891847667712/jUq5uK9dzM4V99fsj8Y6b8tBiZx_idMB5DAMKAPJQ4rTbEiqoy7ah2qUpORWyfaHGl4l",
{
method: "POST",
},
).then((res) => res.json()).then(console.log).catch(console.error);
6 changes: 5 additions & 1 deletion api/dailies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,11 @@ export function makeDailyWebhookEmbeds(
if (options.season) {
embed.fields?.push({
name: `Leaderboard for week of ${options.season.start_date}`,
value: leaderboard.formatScores(options.season),
value: [
"```",
leaderboard.formatScores(options.season),
"```",
].join("\n"),
});
}

Expand Down
10 changes: 6 additions & 4 deletions api/discord_app/sub/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,14 @@ export function makeSyncInteractionResponse(
return {
type: InteractionResponseType.ChannelMessageWithSource,
data: {
content: "",
embeds: [
{
title: `Season \`${r.season.id}\` synced ${
toDiscordTimestamp(new Date(r.season.synced_at!))
}`,
description: formatScores(r.season),
title:
`Leaderboard \`${r.season.id}\` for week of ${r.season.start_date} synced ${
toDiscordTimestamp(new Date(r.season.synced_at!))
}`,
description: ["```", formatScores(r.season), "```"].join("\n"),
},
],
},
Expand Down
21 changes: 9 additions & 12 deletions lib/leaderboard/scores.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,18 +144,15 @@ export function defaultModifyScore(score: number): number {
* formatScores formats the scores of all players in a season.
*/
export function formatScores(season: api.Season): string {
return [
"```",
...Object.entries(season.scores)
.sort(({ 1: scoreA }, { 1: scoreB }) => scoreB - scoreA)
.map(([playerID, score], i) => {
const player = season.players[playerID];
const formattedScore = String(score).padStart(3, " ");
const formattedRank = formatRank(i + 1);
return `${formattedScore} ${player.lc_username} (${formattedRank})`;
}),
"```",
].join("\n");
return Object.entries(season.scores)
.sort(({ 1: scoreA }, { 1: scoreB }) => scoreB - scoreA)
.map(([playerID, score], i) => {
const player = season.players[playerID];
const formattedScore = String(score).padStart(3, " ");
const formattedRank = formatRank(i + 1);
return `${formattedScore} ${player.lc_username} (${formattedRank})`;
})
.join("\n");
}

/**
Expand Down
64 changes: 40 additions & 24 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,45 @@ async function main() {
kv,
lcClient,
);
const r = api.makeAPIRouter(
DISCORD_APPLICATION_ID,
DISCORD_PUBLIC_KEY,
DISCORD_CHANNEL_ID,
DISCORD_WEBHOOK_URL,
WEBHOOK_TOKEN,
lcClient,
leaderboardClient,
);
try {
const r = api.makeAPIRouter(
DISCORD_APPLICATION_ID,
DISCORD_PUBLIC_KEY,
DISCORD_CHANNEL_ID,
DISCORD_WEBHOOK_URL,
WEBHOOK_TOKEN,
lcClient,
leaderboardClient,
);

await Router.serve(
{
port: PORT,
onListen: api.makeOnListen(
PORT,
DISCORD_APPLICATION_ID,
DISCORD_TOKEN,
),
},
r,
)
.finished
.finally(() => {
kv.close();
});
await Router.serve(
{
port: PORT,
onListen: api.makeOnListen(
PORT,
DISCORD_APPLICATION_ID,
DISCORD_TOKEN,
),
},
r,
)
.finished
.finally(() => {
kv.close();
});
} catch (error) {
// TODO: Resolve this error.
//
// Task start deno run -A --unstable main.ts
// - Discord application information: https://discord.com/developers/applications/1130004756153253960/
// - Interaction endpoint: http://127.0.0.1:8080/
// - Invite LC-Dailies to your server: http://127.0.0.1:8080/invite
// - Latest season: http://127.0.0.1:8080/seasons/latest
// SyntaxError: Unexpected token 'O', "OK" is not valid JSON
// at parse (<anonymous>)
// at packageData (ext:deno_fetch/22_body.js:369:14)
// at consumeBody (ext:deno_fetch/22_body.js:246:12)
// at eventLoopTick (ext:core/01_core.js:183:11)
console.log(error);
}
}

0 comments on commit 041692a

Please sign in to comment.