feat: code split a lot

This commit is contained in:
TrèsAbhi committed 2023-10-11 18:42:36 -05:00
1 parent 78e4c82f20
commit d2668974f7
18 files changed
+155 -133

No files matched your search

+14
View File
@@ -0,0 +1,14 @@
import { ChatInputCommandInteraction } from 'discord.js';
import { PeriodType } from './addPeriodSubCommands';
export function getCustomPeriodParams(
interaction: ChatInputCommandInteraction,
) {
if ((interaction.options.getSubcommand() as PeriodType) === 'custom')
return {
start: interaction.options.getInteger('start', true),
end: interaction.options.getInteger('end', true),
};
return undefined;
}
+17
View File
@@ -0,0 +1,17 @@
import { ButtonInteraction } from 'discord.js';
import { TreeTypeString } from '../../components/Tanks';
import { StatFilters } from '../blitz/filterStats';
export function getFiltersFromButton(interaction: ButtonInteraction) {
const url = new URL(`https://example.com/${interaction.customId}`);
return {
nation: url.searchParams.get('nation') ?? undefined,
tank: parseInt(url.searchParams.get('tank') ?? '0') || undefined,
tankType: url.searchParams.get('tank-type') ?? undefined,
treeType:
(url.searchParams.get('tree-type') as TreeTypeString | undefined) ??
undefined,
tier: parseInt(url.searchParams.get('tier') ?? '0') || undefined,
} satisfies StatFilters;
}
+18
View File
@@ -0,0 +1,18 @@
import { ChatInputCommandInteraction } from 'discord.js';
import { TreeTypeString } from '../../components/Tanks';
import { StatFilters } from '../blitz/filterStats';
export function getFiltersFromCommand(
interaction: ChatInputCommandInteraction,
) {
return {
nation: interaction.options.getString('nation') ?? undefined,
tier: parseInt(interaction.options.getString('tier') ?? '0') || undefined,
tankType: interaction.options.getString('tank-type') ?? undefined,
treeType:
(interaction.options.getString('tree-type') as
| TreeTypeString
| undefined) ?? undefined,
tank: parseInt(interaction.options.getString('tank') ?? '0') || undefined,
} satisfies StatFilters;
}
+38 -8
View File
@@ -1,13 +1,43 @@
import { ButtonInteraction, CacheType } from 'discord.js';
import { ButtonInteraction } from 'discord.js';
import { Region } from '../../constants/regions';
import resolvePeriodFromURL from './resolvePeriodFromURL';
import getPeriodNow from '../blitzstars/getPeriodNow';
import getPeriodStart from '../blitzstars/getPeriodStart';
import getTimeDaysAgo from '../blitzstars/getTimeDaysAgo';
import { PeriodType } from './addPeriodSubCommands';
import {
ResolvedPeriod,
getPeriodOptionName,
} from './resolvePeriodFromCommand';
export default function resolvePeriodFromButton(
server: Region,
interaction: ButtonInteraction<CacheType>,
region: Region,
interaction: ButtonInteraction,
) {
return resolvePeriodFromURL(
server,
`https://example.com/${interaction.customId}`,
);
let name: string;
let start: number;
let end: number;
const url = new URL(`https://example.com/${interaction.customId}`);
const path = url.pathname.split('/');
const periodSubcommand = path[2] as PeriodType;
if (periodSubcommand === 'custom') {
const startRaw = parseInt(url!.searchParams.get('start')!);
const endRaw = parseInt(url!.searchParams.get('end')!);
const startDaysAgoMin = Math.min(startRaw, endRaw);
const endDaysAgoMax = Math.max(startRaw, endRaw);
name = `${startDaysAgoMin} - ${endDaysAgoMax} days`;
start = getTimeDaysAgo(region, endDaysAgoMax);
end = getTimeDaysAgo(region, startDaysAgoMin);
} else {
name = getPeriodOptionName(periodSubcommand);
start = getPeriodStart(region, periodSubcommand);
end = getPeriodNow();
}
return {
name,
start,
end,
} satisfies ResolvedPeriod;
}
-42
View File
@@ -1,42 +0,0 @@
import { Region } from '../../constants/regions';
import getPeriodNow from '../blitzstars/getPeriodNow';
import getPeriodStart from '../blitzstars/getPeriodStart';
import getTimeDaysAgo from '../blitzstars/getTimeDaysAgo';
import { PeriodType } from './addPeriodSubCommands';
import {
ResolvedPeriod,
getPeriodOptionName,
} from './resolvePeriodFromCommand';
export default function resolvePeriodFromURL(
region: Region,
urlString: string,
) {
let name: string;
let start: number;
let end: number;
const url = new URL(urlString);
const path = url.pathname.split('/');
const periodSubcommand = path[2] as PeriodType;
if (periodSubcommand === 'custom') {
const startRaw = parseInt(url!.searchParams.get('start')!);
const endRaw = parseInt(url!.searchParams.get('end')!);
const startDaysAgoMin = Math.min(startRaw, endRaw);
const endDaysAgoMax = Math.max(startRaw, endRaw);
name = `${startDaysAgoMin} - ${endDaysAgoMax} days`;
start = getTimeDaysAgo(region, endDaysAgoMax);
end = getTimeDaysAgo(region, startDaysAgoMin);
} else {
name = getPeriodOptionName(periodSubcommand);
start = getPeriodStart(region, periodSubcommand);
end = getPeriodNow();
}
return {
name,
start,
end,
} satisfies ResolvedPeriod;
}
+10 -4
View File
@@ -1,8 +1,14 @@
import { ButtonInteraction, CacheType } from 'discord.js';
import resolvePlayerFromURL from './resolvePlayerFromURL';
import { ButtonInteraction } from 'discord.js';
import { Region } from '../../constants/regions';
import { ResolvedPlayer } from './resolvePlayerFromCommand';
export default async function resolvePlayerFromButton(
interaction: ButtonInteraction<CacheType>,
interaction: ButtonInteraction,
) {
return resolvePlayerFromURL(`https://exmaple.com/${interaction.customId}`);
const url = new URL(`https://exmaple.com/${interaction.customId}`);
return {
id: parseInt(url.searchParams.get('id')!),
region: url.searchParams.get('region') as Region,
} satisfies ResolvedPlayer;
}
-11
View File
@@ -1,11 +0,0 @@
import { Region } from '../../constants/regions';
import { ResolvedPlayer } from '../discord/resolvePlayerFromCommand';
export default function resolvePlayerFromURL(urlString: string) {
const url = new URL(urlString);
return {
id: parseInt(url.searchParams.get('id')!),
region: url.searchParams.get('region') as Region,
} satisfies ResolvedPlayer;
}