mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-10-24 12:09:51 +02:00
fix: time zone compliant commands
This commit is contained in:
1 parent
dd0644e09f
commit
405f544774
11 files changed
+54
-30
No files matched your search
@@ -1,12 +1,16 @@
|
||||
import { BlitzServer } from '../../constants/servers.js';
|
||||
import { Period } from '../discord/addPeriodSubCommands.js';
|
||||
import getTimeDaysAgo from './getTimeDaysAgo.js';
|
||||
|
||||
export default function getPeriodStart(period: Exclude<Period, 'custom'>) {
|
||||
export default function getPeriodStart(
|
||||
server: BlitzServer,
|
||||
period: Exclude<Period, 'custom'>,
|
||||
) {
|
||||
if (period === 'career') {
|
||||
return -Infinity;
|
||||
} else if (period === 'today') {
|
||||
return getTimeDaysAgo(0);
|
||||
return getTimeDaysAgo(server, 1);
|
||||
} else {
|
||||
return getTimeDaysAgo(parseInt(period));
|
||||
return getTimeDaysAgo(server, parseInt(period));
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,20 @@
|
||||
export default function getTimeDaysAgo(daysAgo: number) {
|
||||
import { BlitzServer } from '../../constants/servers.js';
|
||||
|
||||
export const TIME_ZONE_MAPPINGS: Record<BlitzServer, number> = {
|
||||
com: -5, // Central North American Time
|
||||
eu: +1, // Central European Time
|
||||
asia: +7, // Central Asia Standard Time
|
||||
};
|
||||
|
||||
export default function getTimeDaysAgo(server: BlitzServer, daysAgo: number) {
|
||||
const now = new Date();
|
||||
now.setHours(5, 0, 0, 0);
|
||||
if (daysAgo === 0) now.getTime() / 1000;
|
||||
|
||||
// if it's after 5:00 AM today, subtract one day
|
||||
if (now.getTime() > Date.now()) now.setDate(now.getDate() - 1);
|
||||
const time = new Date();
|
||||
|
||||
// subtract the period number of days from now
|
||||
now.setDate(now.getDate() - daysAgo);
|
||||
time.setUTCHours(-TIME_ZONE_MAPPINGS[server], 0, 0, 0);
|
||||
if (time > now) time.setUTCDate(time.getUTCDate() - 1);
|
||||
time.setUTCDate(time.getUTCDate() - daysAgo + 1);
|
||||
|
||||
return now.getTime() / 1000;
|
||||
return time.getTime() / 1000;
|
||||
}
|
||||
@@ -1,9 +1,11 @@
|
||||
import { ButtonInteraction, CacheType } from 'discord.js';
|
||||
import { CYCLIC_API } from '../../constants/cyclic.js';
|
||||
import { BlitzServer } from '../../constants/servers.js';
|
||||
import resolvePeriodFromURL from '../express/resolvePeriodFromURL.js';
|
||||
|
||||
export default function resolvePeriodFromButton(
|
||||
server: BlitzServer,
|
||||
interaction: ButtonInteraction<CacheType>,
|
||||
) {
|
||||
return resolvePeriodFromURL(`${CYCLIC_API}/${interaction.customId}`);
|
||||
return resolvePeriodFromURL(server, `${CYCLIC_API}/${interaction.customId}`);
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
import { CacheType, ChatInputCommandInteraction } from 'discord.js';
|
||||
import { BlitzServer } from '../../constants/servers.js';
|
||||
import getPeriodNow from '../blitzstars/getPeriodNow.js';
|
||||
import getPeriodStart from '../blitzstars/getPeriodStart.js';
|
||||
import getTimeDaysAgo from '../blitzstars/getTimeDaysAgo.js';
|
||||
@@ -30,6 +31,7 @@ export interface ResolvedPeriod {
|
||||
}
|
||||
|
||||
export default function resolvePeriodFromCommand(
|
||||
server: BlitzServer,
|
||||
interaction: ChatInputCommandInteraction<CacheType>,
|
||||
) {
|
||||
let statsName: string;
|
||||
@@ -46,12 +48,12 @@ export default function resolvePeriodFromCommand(
|
||||
|
||||
statsName = `${startMin} to ${endMax} days' statistics`;
|
||||
evolutionName = `${startMin} to ${endMax} days' evolution`;
|
||||
start = getTimeDaysAgo(startMin);
|
||||
end = getTimeDaysAgo(endMax);
|
||||
start = getTimeDaysAgo(server, startMin);
|
||||
end = getTimeDaysAgo(server, endMax);
|
||||
} else {
|
||||
statsName = PERIOD_NAMES[period];
|
||||
evolutionName = EVOLUTION_PERIOD_NAMES[period];
|
||||
start = getPeriodStart(period);
|
||||
start = getPeriodStart(server, period);
|
||||
end = getPeriodNow();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
import { Request } from 'express';
|
||||
import { CYCLIC_API } from '../../constants/cyclic.js';
|
||||
import { BlitzServer } from '../../constants/servers.js';
|
||||
import resolvePeriodFromURL from './resolvePeriodFromURL.js';
|
||||
|
||||
export default function resolvePeriodFromRequest(request: Request) {
|
||||
return resolvePeriodFromURL(`${CYCLIC_API}${request.path}`);
|
||||
export default function resolvePeriodFromRequest(
|
||||
server: BlitzServer,
|
||||
request: Request,
|
||||
) {
|
||||
return resolvePeriodFromURL(server, `${CYCLIC_API}${request.path}`);
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
import { BlitzServer } from '../../constants/servers.js';
|
||||
import getPeriodNow from '../blitzstars/getPeriodNow.js';
|
||||
import getPeriodStart from '../blitzstars/getPeriodStart.js';
|
||||
import getTimeDaysAgo from '../blitzstars/getTimeDaysAgo.js';
|
||||
@@ -8,7 +9,10 @@ import {
|
||||
ResolvedPeriod,
|
||||
} from '../discord/resolvePeriodFromCommand.js';
|
||||
|
||||
export default function resolvePeriodFromURL(urlString: string) {
|
||||
export default function resolvePeriodFromURL(
|
||||
server: BlitzServer,
|
||||
urlString: string,
|
||||
) {
|
||||
let statsName: string;
|
||||
let evolutionName: string;
|
||||
let start: number;
|
||||
@@ -25,12 +29,12 @@ export default function resolvePeriodFromURL(urlString: string) {
|
||||
|
||||
statsName = `${startMin} to ${endMax} days' statistics`;
|
||||
evolutionName = `${startMin} to ${endMax} days' evolution`;
|
||||
start = getTimeDaysAgo(startMin);
|
||||
end = getTimeDaysAgo(endMax);
|
||||
start = getTimeDaysAgo(server, startMin);
|
||||
end = getTimeDaysAgo(server, endMax);
|
||||
} else {
|
||||
statsName = PERIOD_NAMES[period];
|
||||
evolutionName = EVOLUTION_PERIOD_NAMES[period];
|
||||
start = getPeriodStart(period);
|
||||
start = getPeriodStart(server, period);
|
||||
end = getPeriodNow();
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user