feat: split resolvePeriod

This commit is contained in:
TresAbhi committed 2023-06-27 02:02:00 +00:00
1 parent abcb75f06e
commit c221c06511
8 files changed
+73 -39

No files matched your search

+1
View File
@@ -31,6 +31,7 @@ export default {
return await evolution(commandGroup, period, player, tankId);
// TODO: implement this
// new ButtonBuilder()
// .setStyle(ButtonStyle.Link)
// .setLabel('View on BlitzStars')
+2 -1
View File
@@ -6,6 +6,7 @@ import addStatsSubCommandGroups from '../core/discord/addStatsSubCommandGroups.j
import interactionToURL from '../core/discord/interactionToURL.js';
import linkButton from '../core/discord/linkButton.js';
import primaryButton from '../core/discord/primaryButton.js';
import resolvePeriodFromButton from '../core/discord/resolvePeriodFromButton.js';
import resolvePeriodFromCommand from '../core/discord/resolvePeriodFromCommand.js';
import resolvePlayer from '../core/discord/resolvePlayer.js';
import tanksAutocomplete from '../core/discord/tanksAutocomplete.js';
@@ -57,7 +58,7 @@ export default {
const url = new URL(`${CYCLIC_API}/${interaction.customId}`);
const path = url.pathname.split('/').filter(Boolean);
const commandGroup = path[1] as StatType;
const period = resolvePeriodFromCommand(interaction);
const period = resolvePeriodFromButton(interaction);
const player = await resolvePlayer(interaction);
return await stats(
@@ -0,0 +1,9 @@
import { ButtonInteraction, CacheType } from 'discord.js';
import { CYCLIC_API } from '../../constants/cyclic.js';
import resolvePeriodFromURL from '../express/resolvePeriodFromURL.js';
export default function resolvePeriodFromButton(
interaction: ButtonInteraction<CacheType>,
) {
return resolvePeriodFromURL(`${CYCLIC_API}/${interaction.customId}`);
}
+5 -33
View File
@@ -1,10 +1,4 @@
import {
ButtonInteraction,
CacheType,
ChatInputCommandInteraction,
} from 'discord.js';
import { Request } from 'express';
import { CYCLIC_API } from '../../constants/cyclic.js';
import { CacheType, ChatInputCommandInteraction } from 'discord.js';
import getPeriodNow from '../blitzstars/getPeriodNow.js';
import getPeriodStart from '../blitzstars/getPeriodStart.js';
import getTimeDaysAgo from '../blitzstars/getTimeDaysAgo.js';
@@ -36,39 +30,17 @@ export interface ResolvedPeriod {
}
export default function resolvePeriodFromCommand(
input:
| ChatInputCommandInteraction<CacheType>
| ButtonInteraction<CacheType>
| Request,
interaction: ChatInputCommandInteraction<CacheType>,
) {
let statsName: string;
let evolutionName: string;
let start: number;
let end: number;
let url: URL;
let period: Period;
if (input instanceof ChatInputCommandInteraction) {
period = input.options.getSubcommand() as Period;
} else {
url = new URL(
`${CYCLIC_API}${
input instanceof ButtonInteraction ? `/${input.customId}` : input.path
}`,
);
const path = url.pathname.split('/').filter(Boolean);
period = path[path.length - 1] as Period;
}
const period = interaction.options.getSubcommand() as Period;
if (period === 'custom') {
const startRaw =
input instanceof ChatInputCommandInteraction
? input.options.getInteger('start')!
: parseInt(url!.searchParams.get('start')!);
const endRaw =
input instanceof ChatInputCommandInteraction
? input.options.getInteger('end')!
: parseInt(url!.searchParams.get('end')!);
const startRaw = interaction.options.getInteger('start')!;
const endRaw = interaction.options.getInteger('end')!;
const startMin = Math.min(startRaw, endRaw);
const endMax = Math.max(startRaw, endRaw);
@@ -0,0 +1,7 @@
import { Request } from 'express';
import { CYCLIC_API } from '../../constants/cyclic.js';
import resolvePeriodFromURL from './resolvePeriodFromURL.js';
export default function resolvePeriodFromRequest(request: Request) {
return resolvePeriodFromURL(`${CYCLIC_API}${request.path}`);
}
+43
View File
@@ -0,0 +1,43 @@
import getPeriodNow from '../blitzstars/getPeriodNow.js';
import getPeriodStart from '../blitzstars/getPeriodStart.js';
import getTimeDaysAgo from '../blitzstars/getTimeDaysAgo.js';
import { Period } from '../discord/addPeriodSubCommands.js';
import {
EVOLUTION_PERIOD_NAMES,
PERIOD_NAMES,
ResolvedPeriod,
} from '../discord/resolvePeriodFromCommand.js';
export default function resolvePeriodFromURL(urlString: string) {
let statsName: string;
let evolutionName: string;
let start: number;
let end: number;
const url = new URL(urlString);
const path = url.pathname.split('/').filter(Boolean);
const period = path[path.length - 1] as Period;
if (period === 'custom') {
const startRaw = parseInt(url!.searchParams.get('start')!);
const endRaw = parseInt(url!.searchParams.get('end')!);
const startMin = Math.min(startRaw, endRaw);
const endMax = Math.max(startRaw, endRaw);
statsName = `${startMin} to ${endMax} days' statistics`;
evolutionName = `${startMin} to ${endMax} days' evolution`;
start = getTimeDaysAgo(startMin);
end = getTimeDaysAgo(endMax);
} else {
statsName = PERIOD_NAMES[period];
evolutionName = EVOLUTION_PERIOD_NAMES[period];
start = getPeriodStart(period);
end = getPeriodNow();
}
return {
statsName,
evolutionName,
start,
end,
} satisfies ResolvedPeriod;
}
@@ -25,7 +25,7 @@ export default async function handleChatInputCommand(
if (registry.handlesInteraction) return;
const reply = await normalizeInteractionReturnable(returnable);
const reply = await normalizeInteractionReturnable(returnable!);
if (psa.data) {
if (!reply.embeds) reply.embeds = [];
+5 -4
View File
@@ -1,5 +1,5 @@
import { BlitzServer } from '../constants/servers.js';
import resolvePeriodFromCommand from '../core/discord/resolvePeriodFromCommand.js';
import resolvePeriodFromRequest from '../core/express/resolvePeriodFromRequest.js';
import stats, { StatType } from '../renderers/stats.js';
import { ResponderRegistry } from '../server.js';
@@ -9,11 +9,12 @@ export default {
inDevelopment: true,
async handler(req) {
console.log('yeah');
const type = req.params.type as StatType;
const period = resolvePeriodFromRequest(req);
return await stats(
req.params.type as StatType,
resolvePeriodFromCommand(req),
type,
period,
{
server: req.query.server as BlitzServer,
id: parseInt(req.query.id as string),