feat: refresh button

This commit is contained in:
TresAbhi committed 2023-06-25 02:32:56 +00:00
1 parent ae2d12a629
commit 58f5b894f9
30 files changed
+479 -299

No files matched your search

+20
View File
@@ -0,0 +1,20 @@
import { CacheType, ChatInputCommandInteraction } from 'discord.js';
export default function interactionToURL(
interaction: ChatInputCommandInteraction<CacheType>,
) {
const pathname = [
interaction.commandName,
interaction.options.getSubcommandGroup(),
interaction.options.getSubcommand(),
]
.filter(Boolean)
.join('/');
const urlSearchParams = new URLSearchParams();
interaction.options.data.forEach((option) => {
if (option.value) urlSearchParams.set(option.name, `${option.value}`);
});
console.log(`${urlSearchParams}`);
}
+21
View File
@@ -0,0 +1,21 @@
import {
ButtonBuilder,
ButtonStyle,
ComponentEmojiResolvable,
} from 'discord.js';
import { CYCLIC_API } from '../../constants/cyclic.js';
export default function linkButton(
id: string,
label: string,
emoji?: ComponentEmojiResolvable,
) {
const button = new ButtonBuilder()
.setStyle(ButtonStyle.Link)
.setURL(`${CYCLIC_API}/${id}`)
.setLabel(label);
if (emoji) button.setEmoji(emoji);
return button;
}
@@ -0,0 +1,40 @@
import {
ActionRowBuilder,
ButtonBuilder,
EmbedBuilder,
InteractionEditReplyOptions,
} from 'discord.js';
import { InteractionReturnable } from '../../events/interactionCreate/index.js';
import render from '../ui/render.js';
export default async function normalizeInteractionReturnable(
returnable: InteractionReturnable,
) {
const reply: InteractionEditReplyOptions = {};
const awaitedReturnable = await returnable;
const normalizedReturnable = Array.isArray(awaitedReturnable)
? awaitedReturnable
: [awaitedReturnable];
await Promise.all(
normalizedReturnable.map(async (item) => {
if (item instanceof EmbedBuilder) {
if (!reply.embeds) reply.embeds = [];
reply.embeds.push(item);
} else if (item instanceof ButtonBuilder) {
if (!reply.components)
reply.components = [new ActionRowBuilder<ButtonBuilder>()];
(reply.components[0] as ActionRowBuilder<ButtonBuilder>).addComponents(
item,
);
} else {
const image = await render(item);
if (!reply.files) reply.files = [];
reply.files.push(image);
}
}),
);
return reply;
}
+20
View File
@@ -0,0 +1,20 @@
import {
ButtonBuilder,
ButtonStyle,
ComponentEmojiResolvable,
} from 'discord.js';
export default function primaryButton(
id: string,
label: string,
emoji?: ComponentEmojiResolvable,
) {
const button = new ButtonBuilder()
.setCustomId(id)
.setStyle(ButtonStyle.Primary)
.setLabel(label);
if (emoji) button.setEmoji(emoji);
return button;
}
+32 -7
View File
@@ -1,4 +1,9 @@
import { CacheType, ChatInputCommandInteraction } from 'discord.js';
import {
ButtonInteraction,
CacheType,
ChatInputCommandInteraction,
} from 'discord.js';
import { CYCLIC_API } from '../../constants/cyclic.js';
import getPeriodNow from '../blitzstars/getPeriodNow.js';
import getPeriodStart from '../blitzstars/getPeriodStart.js';
import getTimeDaysAgo from '../blitzstars/getTimeDaysAgo.js';
@@ -31,19 +36,39 @@ export interface ResolvedPeriod {
}
export default function resolvePeriod(
interaction: ChatInputCommandInteraction<CacheType>,
interaction:
| ChatInputCommandInteraction<CacheType>
| ButtonInteraction<CacheType>,
) {
const period = interaction.options.getSubcommand(true) as Period;
let statsName: string;
let evolutionName: string;
let start: number;
let end: number;
const relativePeriodName =
RELATIVE_PERIOD_NAMES[interaction.options.getSubcommand() as Period];
let subcommand: Period;
let url: typeof interaction extends ChatInputCommandInteraction
? undefined
: URL;
if (interaction instanceof ChatInputCommandInteraction) {
subcommand = interaction.options.getSubcommand() as Period;
} else {
url = new URL(`${CYCLIC_API}/${interaction.customId}`);
const path = url.pathname.split('/').filter(Boolean);
subcommand = path[path.length - 1] as Period;
}
const period = subcommand;
const relativePeriodName = RELATIVE_PERIOD_NAMES[subcommand];
if (period === 'custom') {
const startRaw = interaction.options.getInteger('start')!;
const endRaw = interaction.options.getInteger('end')!;
const startRaw =
interaction instanceof ChatInputCommandInteraction
? interaction.options.getInteger('start')!
: parseInt(url!.searchParams.get('start')!);
const endRaw =
interaction instanceof ChatInputCommandInteraction
? interaction.options.getInteger('end')!
: parseInt(url!.searchParams.get('end')!);
const startMin = Math.min(startRaw, endRaw);
const endMax = Math.max(startRaw, endRaw);
+46 -31
View File
@@ -1,4 +1,9 @@
import { CacheType, ChatInputCommandInteraction } from 'discord.js';
import {
ButtonInteraction,
CacheType,
ChatInputCommandInteraction,
} from 'discord.js';
import { CYCLIC_API } from '../../constants/cyclic.js';
import { BlitzServer } from '../../constants/servers.js';
import listAccountsPanServer, {
usernamePatternWithoutPosition,
@@ -13,40 +18,50 @@ export interface ResolvedPlayer {
}
export default async function resolvePlayer(
interaction: ChatInputCommandInteraction<CacheType>,
interaction:
| ChatInputCommandInteraction<CacheType>
| ButtonInteraction<CacheType>,
) {
const commandUsername = interaction.options.getString('username');
const displayName = interaction.guild?.members.cache
.get(interaction.user.id)
?.displayName.match(usernamePatternWithoutPosition)?.[0];
const username = commandUsername ?? displayName;
if (interaction instanceof ChatInputCommandInteraction) {
const commandUsername = interaction.options.getString('username');
const displayName = interaction.guild?.members.cache
.get(interaction.user.id)
?.displayName.match(usernamePatternWithoutPosition)?.[0];
const username = commandUsername ?? displayName;
if (username === undefined) {
throw errorWithCause(
'Username could not be inferred',
"You didn't provide me a username in the command nor was I able to infer your Blitz username from your Discord nickname. Try providing a username manually in the command or using the `/verify` command to set your Discord nickname to your Blitz username.",
);
}
if (serverAndIdPattern.test(username)) {
const [server, accountId] = username.split('/');
return {
server: server as BlitzServer,
id: Number(accountId),
} satisfies ResolvedPlayer;
} else {
const accounts = await listAccountsPanServer(username);
if (accounts[0]) {
return {
server: accounts[0].server,
id: accounts[0].account_id,
} satisfies ResolvedPlayer;
} else {
if (username === undefined) {
throw errorWithCause(
'Could not find user',
`I couldn't find user \`${username}\`. Try providing a valid username manually or using the \`/verify\` command to set your Discord nickname to your Blitz username.`,
'Username could not be inferred',
"You didn't provide me a username in the command nor was I able to infer your Blitz username from your Discord nickname. Try providing a username manually in the command or using the `/verify` command to set your Discord nickname to your Blitz username.",
);
}
if (serverAndIdPattern.test(username)) {
const [server, accountId] = username.split('/');
return {
server: server as BlitzServer,
id: Number(accountId),
} satisfies ResolvedPlayer;
} else {
const accounts = await listAccountsPanServer(username);
if (accounts[0]) {
return {
server: accounts[0].server,
id: accounts[0].account_id,
} satisfies ResolvedPlayer;
} else {
throw errorWithCause(
'Could not find user',
`I couldn't find user \`${username}\`. Try providing a valid username manually or using the \`/verify\` command to set your Discord nickname to your Blitz username.`,
);
}
}
} else {
const url = new URL(`${CYCLIC_API}/${interaction.customId}`);
return {
id: parseInt(url.searchParams.get('id')!),
server: url.searchParams.get('server') as BlitzServer,
} satisfies ResolvedPlayer;
}
}
+2 -2
View File
@@ -4,10 +4,10 @@ export interface PSAData {
}
export interface PSA {
data?: PSAData;
data: PSAData | null;
}
export const psa: PSA = {};
export const psa: PSA = { data: null };
console.log('Caching PSA...');
fetch(