Skip to content

Commit

Permalink
Merge AoC config into main config
Browse files Browse the repository at this point in the history
  • Loading branch information
holzmaster committed Jul 23, 2024
1 parent dc47cfc commit 8a43175
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 34 deletions.
6 changes: 0 additions & 6 deletions .github/aoc.config.json

This file was deleted.

7 changes: 7 additions & 0 deletions .github/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@
"blacklistedChannelIds": [],
"defaultTargetChannelId": "893179191556706386",
"targetChannelOverrides": []
},
"aoc": {
"enabled": false,
"targetChannelId": "",
"sessionToken": "",
"leaderBoardJsonUrl": "",
"userMap": {}
}
},

Expand Down
3 changes: 0 additions & 3 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@ jobs:
sed -i 's/<BOT_TOKEN>/${{ secrets.CI_BOT_TOKEN }}/g' config.json
sed -i 's/<CLIENT_ID>/${{ secrets.CI_CLIENT_ID }}/g' config.json
cp .github/aoc.config.json aoc.config.json
- name: Check if bot starts correctly
if: ${{ github.event_name == 'push' }}
run: |
Expand All @@ -71,6 +69,5 @@ jobs:
-e NODE_ENV=development \
-e DATABASE_PATH=:memory: \
-v "$PWD/config.json:/app/config.json" \
-v "$PWD/aoc.config.json:/app/aoc.config.json" \
csz-bot:latest --dry-run
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,3 @@ dist
*.db
.idea/
built/
aoc.config.json
1 change: 0 additions & 1 deletion .infra/compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ services:
- .env
volumes:
- ./config.json:/app/config.json:ro
- ./aoc.config.json:/app/aoc.config.json:ro
- ./sounds:/app/sounds:ro
- ./banners:/app/banners:ro
- /etc/timezone:/etc/timezone:ro
Expand Down
50 changes: 27 additions & 23 deletions src/commands/aoc.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import * as fs from "node:fs/promises";
import * as path from "node:path";

import {
type CacheType,
type CommandInteraction,
Expand All @@ -14,22 +11,13 @@ import type { ApplicationCommand } from "./command.js";

import log from "@log";

const aocConfigPath = path.resolve("aoc.config.json");
type CompletionInfo = Record<1 | 2, { get_start_ts: number }>;

type UserMapEntry = {
export type UserMapEntry = {
displayName: string;
language: string;
};

type AoCConfig = {
targetChannelId: string;
sessionToken: string;
leaderBoardJsonUrl: string;
userMap: Record<string, UserMapEntry>;
};

type CompletionInfo = Record<1 | 2, { get_start_ts: number }>;

type AoCMember = {
id: string;
name: string;
Expand All @@ -46,7 +34,6 @@ type LeaderBoard = {
members: Record<number, AoCMember>;
};

const aocConfig = JSON.parse(await fs.readFile(aocConfigPath, "utf8")) as AoCConfig;
const medals = ["🥇", "🥈", "🥉", "🪙", "🏵️", "🌹"];

const getLanguage = (member: AoCMember, userMap: Record<string, UserMapEntry>): string => {
Expand Down Expand Up @@ -120,10 +107,13 @@ const createEmbedFromLeaderBoard = (
};
};

const getLeaderBoard = async (): Promise<LeaderBoard> => {
const leaderBoard = (await fetch(aocConfig.leaderBoardJsonUrl, {
const getLeaderBoard = async (
leaderBoardJsonUrl: string,
sessionToken: string,
): Promise<LeaderBoard> => {
const leaderBoard = (await fetch(leaderBoardJsonUrl, {
headers: {
Cookie: `session=${aocConfig.sessionToken}`,
Cookie: `session=${encodeURIComponent(sessionToken)}`,
},
}).then(r => r.json())) as LeaderBoard;
return leaderBoard;
Expand All @@ -132,14 +122,16 @@ const getLeaderBoard = async (): Promise<LeaderBoard> => {
export async function publishAocLeaderBoard(context: BotContext) {
log.debug("Entered `AoCHandler#publishLeaderBoard`");

const aocConfig = context.commandConfig.aoc;

const targetChannel = context.guild.channels.cache.get(aocConfig.targetChannelId);
if (!targetChannel) {
log.error(`Target channel ${aocConfig.targetChannelId} not found`);
return;
}

const channel = targetChannel as discord.ThreadChannel;
const leaderBoard = await getLeaderBoard();
const leaderBoard = await getLeaderBoard(aocConfig.leaderBoardJsonUrl, aocConfig.sessionToken);
const embed = createEmbedFromLeaderBoard(aocConfig.userMap, leaderBoard, "local_score");
return channel.send({ embeds: [embed] });
}
Expand Down Expand Up @@ -171,29 +163,41 @@ export default class AoCCommand implements ApplicationCommand {
.setRequired(false),
);

async handleInteraction(command: CommandInteraction<CacheType>) {
async handleInteraction(command: CommandInteraction<CacheType>, context: BotContext) {
if (!command.isChatInputCommand()) {
// TODO: Solve this on a type level
return;
}

const { channel } = command;
if (!channel?.isTextBased()) {
const aocConfig = context.commandConfig.aoc;

if (!command.channel?.isTextBased()) {
await command.reply({
content: "Mach mal nicht hier",
ephemeral: true,
});
return;
}

if (!aocConfig.enabled) {
await command.reply({
content: "AoC ist gerade nicht",
ephemeral: true,
});
return;
}

const order = command.options.getString("order", false) ?? "local_score";

if (order !== "stars" && order !== "local_score" && order !== "global_score") {
// Shouldn't happen unless we change the command
throw new Error(`Invalid order ${order}`);
}

const leaderBoard = await getLeaderBoard();
const leaderBoard = await getLeaderBoard(
aocConfig.leaderBoardJsonUrl,
aocConfig.sessionToken,
);
const embed = createEmbedFromLeaderBoard(aocConfig.userMap, leaderBoard, order);
await command.reply({ embeds: [embed] });
}
Expand Down
16 changes: 16 additions & 0 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import type {
import { ChannelType } from "discord.js";
import { Temporal } from "@js-temporal/polyfill";

import type { UserMapEntry } from "./commands/aoc.js";
import { readConfig } from "./service/configService.js";

/**
Expand Down Expand Up @@ -53,6 +54,14 @@ export interface BotContext {
instagram: {
rapidApiInstagramApiKey?: string;
};
aoc: {
enabled: boolean;

targetChannelId: Snowflake;
sessionToken: string;
leaderBoardJsonUrl: string;
userMap: Record<Snowflake, UserMapEntry>;
};
};

roles: {
Expand Down Expand Up @@ -221,6 +230,13 @@ export async function createBotContext(client: Client<true>): Promise<BotContext
rapidApiInstagramApiKey:
config.command.instagram.rapidApiInstagramApiKey?.trim() ?? undefined,
},
aoc: {
enabled: config.command.aoc.enabled,
targetChannelId: config.command.aoc.targetChannelId,
sessionToken: config.command.aoc.sessionToken,
leaderBoardJsonUrl: config.command.aoc.leaderBoardJsonUrl,
userMap: config.command.aoc.userMap,
},
},

deleteThreadMessagesInChannelIds: new Set(config.deleteThreadMessagesInChannelIds),
Expand Down
14 changes: 14 additions & 0 deletions src/service/configService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,20 @@ export interface Config {
targetChannelOverrides: Record<string, string>;
defaultTargetChannelId: Snowflake;
};
aoc: {
enabled: boolean;

targetChannelId: Snowflake;
sessionToken: string;
leaderBoardJsonUrl: string;
userMap: Record<
Snowflake,
{
displayName: string;
language: string;
}
>;
};
};

deleteThreadMessagesInChannelIds: readonly Snowflake[];
Expand Down

0 comments on commit 8a43175

Please sign in to comment.