From f5bab8a698b20b33dd3d40c45e970886ae7f1af0 Mon Sep 17 00:00:00 2001 From: tresabhi Date: Thu, 7 Sep 2023 01:24:12 +0000 Subject: [PATCH] faet: clan-event --- docs/changelogs/1.12.4.md | 7 ++ src/commands/clanEvent.ts | 107 ++++++++++++++++++++++++++ src/events/interactionCreate/index.ts | 2 + tsconfig.json | 2 +- 4 files changed, 117 insertions(+), 1 deletion(-) create mode 100644 docs/changelogs/1.12.4.md create mode 100644 src/commands/clanEvent.ts diff --git a/docs/changelogs/1.12.4.md b/docs/changelogs/1.12.4.md new file mode 100644 index 000000000..4714d1774 --- /dev/null +++ b/docs/changelogs/1.12.4.md @@ -0,0 +1,7 @@ +# Blitzkrieg 1.12.4 + +Added very badly-coded temporary `/clan-event` command (without linting) + +## Changes + +- Added `/clan-event` command diff --git a/src/commands/clanEvent.ts b/src/commands/clanEvent.ts new file mode 100644 index 000000000..fe015dbe2 --- /dev/null +++ b/src/commands/clanEvent.ts @@ -0,0 +1,107 @@ +import { SlashCommandBuilder } from 'discord.js'; +import markdownEscape from 'markdown-escape'; +import autocompleteClan from '../core/discord/autocompleteClan'; +import embedInfo from '../core/discord/embedInfo'; +import { secrets } from '../core/node/secrets'; +import { CommandRegistry } from '../events/interactionCreate'; + +const DEFAULT_THRESHOLD = 7; + +export const clanEventCommand: CommandRegistry = { + inProduction: true, + inDevelopment: true, + inPublic: true, + + command: new SlashCommandBuilder() + .setName('clan-event') + .setDescription('Lists all inactive players') + .addStringOption((option) => + option + .setName('clan') + .setDescription('The clan name or tag you are checking') + .setRequired(true), + ), + + async handler(interaction) { + const REGION = 'com'; + const CLAN_SEARCH = interaction.options.getString('clan', true); + + const { + clan_id, + name: clan_name, + tag: clan_tag, + } = await fetch( + `https://api.wotblitz.${REGION}/wotb/clans/list/?application_id=${secrets.WARGAMING_APPLICATION_ID}&search=${CLAN_SEARCH}`, + ) + .then((response) => response.json()) + .then(({ data }) => data[0]); + const memebers = await fetch( + `https://api.wotblitz.${REGION}/wotb/clans/info/?application_id=${secrets.WARGAMING_APPLICATION_ID}&clan_id=${clan_id}`, + ) + .then((response) => response.json()) + .then(({ data }) => data[clan_id].members_ids); + const stats = await Promise.all( + memebers.map(async (id, index) => { + await new Promise((resolve) => setTimeout(resolve, index * 200)); + + const [yesterdayPlatoonWins, name, todayPlatoonWins] = + await Promise.all([ + fetch(`https://www.blitzstars.com/api/playerstats/${id}`) + .then((response) => response.json()) + .then((data) => data[0]?.achievements.jointVictoryCount), + fetch( + `https://api.wotblitz.${REGION}/wotb/account/info/?application_id=${secrets.WARGAMING_APPLICATION_ID}&account_id=${id}`, + ) + .then((response) => response.json()) + .then(({ data }) => data[id].nickname), + fetch( + `https://api.wotblitz.${REGION}/wotb/account/achievements/?application_id=${secrets.WARGAMING_APPLICATION_ID}&account_id=${id}`, + ) + .then((response) => response.json()) + .then(({ data }) => data[id].max_series.jointVictory), + ]); + + return { + id, + name, + yesterdayPlatoonWins, + todayPlatoonWins, + }; + }), + ); + + return embedInfo( + `Estimation of ${clan_name} [${clan_tag}]'s total platoon wins today: ${( + stats.reduce( + (accumulator, player) => + accumulator + + (player.yesterdayPlatoonWins === undefined + ? 0 + : player.todayPlatoonWins - player.yesterdayPlatoonWins), + 0, + ) / 2 + ).toFixed(0)}`, + stats + .sort( + (playerA, playerB) => + (playerB.yesterdayPlatoonWins === undefined + ? 0 + : playerB.todayPlatoonWins - playerB.yesterdayPlatoonWins) - + (playerA.yesterdayPlatoonWins === undefined + ? 0 + : playerA.todayPlatoonWins - playerA.yesterdayPlatoonWins), + ) + .map( + (player) => + `${markdownEscape(player.name)}: ${ + player.yesterdayPlatoonWins === undefined + ? 0 + : player.todayPlatoonWins - player.yesterdayPlatoonWins + }`, + ) + .join('\n'), + ); + }, + + autocomplete: autocompleteClan, +}; diff --git a/src/events/interactionCreate/index.ts b/src/events/interactionCreate/index.ts index 6664952cc..00aea17cf 100644 --- a/src/events/interactionCreate/index.ts +++ b/src/events/interactionCreate/index.ts @@ -13,6 +13,7 @@ import { SlashCommandSubcommandsOnlyBuilder, } from 'discord.js'; import discord from '../../../discord.json' assert { type: 'json' }; +import { clanEventCommand } from '../../commands/clanEvent'; import { debugCommand } from '../../commands/debug'; import { eligibleCommand } from '../../commands/eligible'; import { evolutionCommand } from '../../commands/evolution'; @@ -99,6 +100,7 @@ export const COMMANDS_RAW: CommandRegistry[] = [ evolutionCommand, statsCommand, ratingsCommand, + clanEventCommand, ]; export const commands = Promise.all(COMMANDS_RAW).then((rawCommands) => diff --git a/tsconfig.json b/tsconfig.json index 65c75994f..7b1732b23 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -11,5 +11,5 @@ "resolveJsonModule": true, "jsx": "react-jsx" }, - "include": ["src/**/*.ts", "src/components/Tanks/components/Row.tsx"] + "include": ["src/**/*.ts", "src/components/Tanks/components/Row.tsx", "src/commands/clanEvent.ts"] }