feat: advanced PSAs

This commit is contained in:
TrèsAbhi committed 2023-11-14 13:42:15 -06:00
1 parent ef9f3bd361
commit 02a03b4d68
4 files changed
+87 -12

No files matched your search

+3 -1
View File
@@ -1,15 +1,17 @@
import { RatingsInfo } from '../../commands/ratings'; import { RatingsInfo } from '../../commands/ratings';
import { Region } from '../../constants/regions'; import { Region } from '../../constants/regions';
import { context } from '../blitzkrieg/context'; import { context } from '../blitzkrieg/context';
import { patientFetch } from '../blitzkrieg/patientFetch';
import regionToRegionSubdomain from './regionToRegionSubdomain'; import regionToRegionSubdomain from './regionToRegionSubdomain';
export default async function getRatingsInfo(region: Region) { export default async function getRatingsInfo(region: Region) {
const response = await fetch( const response = await patientFetch(
context === 'website' context === 'website'
? `/api/${region}/ratings/current/info` ? `/api/${region}/ratings/current/info`
: `https://${regionToRegionSubdomain( : `https://${regionToRegionSubdomain(
region, region,
)}.wotblitz.com/en/api/rating-leaderboards/season/`, )}.wotblitz.com/en/api/rating-leaderboards/season/`,
undefined,
{ cache: 'no-store' }, { cache: 'no-store' },
); );
+10 -3
View File
@@ -1,11 +1,18 @@
export async function patientFetch(url: string, timeout = 1000) { export async function patientFetch(
url: string,
timeout = 1000,
options?: Partial<{ cache: 'no-store' }>,
) {
let patientResponse: Response; let patientResponse: Response;
while (patientResponse! === undefined) { while (patientResponse! === undefined) {
try { try {
patientResponse = await fetch(url); patientResponse = await fetch(url, options);
} catch (error) { } catch (error) {
console.warn(`Failed to fetch ${url}; retrying in ${timeout}ms...`); console.warn(
`Failed to fetch ${url}; retrying in ${timeout}ms...`,
error,
);
await new Promise((resolve) => setTimeout(resolve, timeout)); await new Promise((resolve) => setTimeout(resolve, timeout));
} }
} }
+21 -5
View File
@@ -1,13 +1,29 @@
export interface PSAData { export type PSAData = {
title: string; secret?: boolean;
description: string; links?: {
} title: string;
url: string;
}[];
} & (
| {
type: 'image';
image: string;
title?: string;
}
| {
type: 'embed';
title: string;
description: string;
}
);
export interface PSA { export interface PSA {
data: PSAData | null; data: PSAData | null;
} }
export const psa: PSA = { data: null }; export const psa: PSA = {
data: null,
};
fetch( fetch(
'https://gist.githubusercontent.com/tresabhi/ed4b136f08856e615212e573aabe1968/raw', 'https://gist.githubusercontent.com/tresabhi/ed4b136f08856e615212e573aabe1968/raw',
@@ -1,13 +1,16 @@
import { import {
ActionRowBuilder, ActionRowBuilder,
AttachmentBuilder,
ButtonBuilder, ButtonBuilder,
ButtonStyle, ButtonStyle,
CacheType, CacheType,
ChatInputCommandInteraction, ChatInputCommandInteraction,
InteractionReplyOptions,
} from 'discord.js'; } from 'discord.js';
import { commands } from '..'; import { commands } from '..';
import discord from '../../../../discord.json' assert { type: 'json' }; import discord from '../../../../discord.json' assert { type: 'json' };
import { UserError } from '../../../core/blitzkrieg/userError'; import { UserError } from '../../../core/blitzkrieg/userError';
import buttonLink from '../../../core/discord/buttonLink';
import embedNegative from '../../../core/discord/embedNegative'; import embedNegative from '../../../core/discord/embedNegative';
import embedWarning from '../../../core/discord/embedWarning'; import embedWarning from '../../../core/discord/embedWarning';
import normalizeInteractionReturnable from '../../../core/discord/normalizeInteractionReturnable'; import normalizeInteractionReturnable from '../../../core/discord/normalizeInteractionReturnable';
@@ -40,12 +43,59 @@ export default async function handleChatInputCommand(
const reply = await normalizeInteractionReturnable(returnable!); const reply = await normalizeInteractionReturnable(returnable!);
if (psa.data) { if (psa.data && !psa.data.secret) {
if (!reply.embeds) reply.embeds = []; if (psa.data.type === 'embed') {
reply.embeds.push(embedWarning(psa.data.title, psa.data.description)); if (!reply.embeds) reply.embeds = [];
reply.embeds.push(embedWarning(psa.data.title, psa.data.description));
} else {
if (!reply.files) reply.files = [];
reply.files.push(new AttachmentBuilder(psa.data.image));
reply.content = reply.content ?? psa.data.title;
}
if (psa.data.links) {
if (!reply.components?.[0]) {
reply.components = [new ActionRowBuilder<ButtonBuilder>()];
}
(reply.components[0] as ActionRowBuilder<ButtonBuilder>).addComponents(
psa.data.links.map(({ title, url }) => buttonLink(url, title)),
);
}
} }
await interaction.editReply(reply); await interaction.editReply(reply);
if (psa.data && psa.data.secret) {
const followUp: InteractionReplyOptions = {
ephemeral: true,
};
if (psa.data.type === 'embed') {
if (!followUp.embeds) followUp.embeds = [];
followUp.embeds.push(
embedWarning(psa.data.title, psa.data.description),
);
} else {
if (!followUp.files) followUp.files = [];
followUp.files.push(new AttachmentBuilder(psa.data.image));
followUp.content = followUp.content ?? psa.data.title;
}
if (psa.data.links) {
if (!followUp.components?.[0]) {
followUp.components = [new ActionRowBuilder<ButtonBuilder>()];
}
(
followUp.components[0] as ActionRowBuilder<ButtonBuilder>
).addComponents(
psa.data.links.map(({ title, url }) => buttonLink(url, title)),
);
}
interaction.followUp(followUp);
}
} catch (error) { } catch (error) {
const components = [ const components = [
new ActionRowBuilder<ButtonBuilder>().addComponents( new ActionRowBuilder<ButtonBuilder>().addComponents(