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

@@ -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;
}