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

-241
View File
@@ -1,241 +0,0 @@
import {
ActionRowBuilder,
AutocompleteInteraction,
ButtonBuilder,
ButtonInteraction,
ButtonStyle,
CacheType,
ChatInputCommandInteraction,
EmbedBuilder,
Interaction,
InteractionEditReplyOptions,
REST,
RESTPostAPIChatInputApplicationCommandsJSONBody,
Routes,
SlashCommandBuilder,
SlashCommandSubcommandsOnlyBuilder,
} from 'discord.js';
import discord from '../../discord.json' assert { type: 'json' };
import debug from '../commands/debug.js';
import eligible from '../commands/eligible.js';
import evolution from '../commands/evolution.js';
import help from '../commands/help.js';
import inactive from '../commands/inactive.js';
import ownedtanks from '../commands/ownedtanks.js';
import ping from '../commands/ping.js';
import playerachievements from '../commands/playerachievements.js';
import playerinfo from '../commands/playerinfo.js';
import searchclans from '../commands/searchclans.js';
import searchplayers from '../commands/searchplayers.js';
import searchtanks from '../commands/searchtanks.js';
import stats from '../commands/stats.js';
import today from '../commands/today.js';
import verify from '../commands/verify.js';
import negativeEmbed from '../core/interaction/negativeEmbed.js';
import warningEmbed from '../core/interaction/warningEmbed.js';
import { DISCORD_TOKEN } from '../core/process/args.js';
import getClientId from '../core/process/getClientId.js';
import isDev from '../core/process/isDev.js';
import { psa } from '../core/process/psa.js';
import render from '../core/ui/render.js';
import { handleError } from './error.js';
export type CommandReturnable = EmbedBuilder | ButtonBuilder | JSX.Element;
export type RichCommandReturnable = CommandReturnable | CommandReturnable[];
export type CommandRegistry = {
inDevelopment: boolean;
inProduction: boolean;
inPublic: boolean;
command:
| SlashCommandBuilder
| SlashCommandSubcommandsOnlyBuilder
| Omit<SlashCommandBuilder, 'addSubcommand' | 'addSubcommandGroup'>;
autocomplete?: (interaction: AutocompleteInteraction<CacheType>) => void;
} & (
| {
handlesInteraction?: false;
execute: (
interaction: ChatInputCommandInteraction<CacheType>,
) => RichCommandReturnable | Promise<RichCommandReturnable>;
}
| {
handlesInteraction: true;
execute: (interaction: ChatInputCommandInteraction<CacheType>) => void;
}
);
const rest = new REST().setToken(DISCORD_TOKEN);
const commands = (
[
debug,
eligible,
help,
inactive,
ownedtanks,
playerachievements,
playerinfo,
searchclans,
searchplayers,
searchtanks,
stats,
today,
verify,
ping,
evolution,
] as CommandRegistry[]
).reduce<Record<string, CommandRegistry>>((accumulator, registry) => {
registry.command.setName(
isDev() ? `${registry.command.name}dev` : registry.command.name,
);
return { ...accumulator, [registry.command.name]: registry };
}, {});
export const guildCommands: RESTPostAPIChatInputApplicationCommandsJSONBody[] =
[];
export const publicCommands: RESTPostAPIChatInputApplicationCommandsJSONBody[] =
[];
Object.entries(commands).forEach(([, registry]) => {
if (isDev() ? registry.inDevelopment : registry.inProduction) {
const json = registry.command.toJSON();
if (registry.inPublic) {
publicCommands.push(json);
} else {
guildCommands.push(json);
}
}
});
try {
console.log(`Refreshing ${publicCommands.length} public command(s).`);
rest
.put(Routes.applicationCommands(getClientId()), { body: publicCommands })
.then((publicData) => {
console.log(
`Successfully refreshed ${
(publicData as RESTPostAPIChatInputApplicationCommandsJSONBody[])
.length
} public command(s).`,
);
});
console.log(`Refreshing ${guildCommands.length} guild command(s).`);
rest
.put(
Routes.applicationGuildCommands(getClientId(), discord.tres_guild_id),
{ body: guildCommands },
)
.then((guildData) => {
console.log(
`Successfully refreshed ${
(guildData as unknown[]).length
} guild command(s).`,
);
});
} catch (error) {
console.error(error);
}
//#region Event type handlers
function handleAutocomplete(interaction: AutocompleteInteraction<CacheType>) {
const command = commands[interaction.commandName];
if (!command) {
console.error(`No command matching ${interaction.commandName} was found.`);
return;
}
if (command.autocomplete) command.autocomplete(interaction);
}
async function handleChatInputCommand(
interaction: ChatInputCommandInteraction<CacheType>,
) {
const command = commands[interaction.commandName];
if (!command) {
console.error(`No command matching ${interaction.commandName} was found.`);
return;
}
console.log(interaction.toString());
await interaction.deferReply();
try {
const result = await command.execute(interaction);
if (command.handlesInteraction) return;
const normalizedResult = Array.isArray(result) ? result : [result];
const reply: InteractionEditReplyOptions = {};
if (psa.data) {
normalizedResult.push(warningEmbed(psa.data.title, psa.data.description));
}
await Promise.all(
normalizedResult.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);
}
}),
);
await interaction.editReply(reply);
} catch (error) {
const actionRow = new ActionRowBuilder<ButtonBuilder>().addComponents(
new ButtonBuilder()
.setLabel('Get Help on Discord Server')
.setURL('https://discord.gg/nDt7AjGJQH')
.setStyle(ButtonStyle.Link),
);
await interaction.editReply({
embeds: [
negativeEmbed(
(error as Error).message,
`${(error as Error).cause ?? 'No further information is available.'}`,
),
],
components: [actionRow],
});
handleError(error as Error, interaction.commandName);
}
}
function handleButton(interaction: ButtonInteraction<CacheType>) {
console.log(interaction.customId);
interaction.message.edit({
files: [],
});
}
//#endregion
export default async function interactionCreate(
interaction: Interaction<CacheType>,
) {
if (interaction.isAutocomplete()) {
handleAutocomplete(interaction);
} else if (interaction.isChatInputCommand()) {
handleChatInputCommand(interaction);
} else if (interaction.isButton()) {
handleButton(interaction);
}
}
@@ -0,0 +1,15 @@
import { AutocompleteInteraction, CacheType } from 'discord.js';
import { commands } from '../index.js';
export default function handleAutocomplete(
interaction: AutocompleteInteraction<CacheType>,
) {
const command = commands[interaction.commandName];
if (!command) {
console.error(`No command matching ${interaction.commandName} was found.`);
return;
}
if (command.autocomplete) command.autocomplete(interaction);
}
@@ -0,0 +1,20 @@
import { ButtonInteraction, CacheType } from 'discord.js';
import normalizeInteractionReturnable from '../../../core/interaction/normalizeInteractionReturnable.js';
import { commands } from '../index.js';
export default async function handleButton(
interaction: ButtonInteraction<CacheType>,
) {
const commandName = interaction.customId.split('/')[0];
const button = commands[commandName]?.button;
if (!button) {
console.error(`Button handler not found for "${commandName}"`);
return;
}
await interaction.deferUpdate();
await interaction.message.edit(
await normalizeInteractionReturnable(button(interaction)),
);
}
@@ -0,0 +1,56 @@
import {
ActionRowBuilder,
ButtonBuilder,
ButtonStyle,
CacheType,
ChatInputCommandInteraction,
} from 'discord.js';
import negativeEmbed from '../../../core/interaction/negativeEmbed.js';
import normalizeInteractionReturnable from '../../../core/interaction/normalizeInteractionReturnable.js';
import warningEmbed from '../../../core/interaction/warningEmbed.js';
import { psa } from '../../../core/process/psa.js';
import { handleError } from '../../error.js';
import { commands } from '../index.js';
export default async function handleChatInputCommand(
interaction: ChatInputCommandInteraction<CacheType>,
) {
const command = commands[interaction.commandName];
console.log(interaction.toString());
await interaction.deferReply();
try {
const returnable = await command.execute(interaction);
if (command.handlesInteraction) return;
const reply = await normalizeInteractionReturnable(returnable!);
if (psa.data) {
if (!reply.embeds) reply.embeds = [];
reply.embeds.push(warningEmbed(psa.data.title, psa.data.description));
}
await interaction.editReply(reply);
} catch (error) {
const actionRow = new ActionRowBuilder<ButtonBuilder>().addComponents(
new ButtonBuilder()
.setLabel('Get Help on Discord Server')
.setURL('https://discord.gg/nDt7AjGJQH')
.setStyle(ButtonStyle.Link),
);
await interaction.editReply({
embeds: [
negativeEmbed(
(error as Error).message,
`${(error as Error).cause ?? 'No further information is available.'}`,
),
],
components: [actionRow],
});
handleError(error as Error, interaction.commandName);
}
}
+156
View File
@@ -0,0 +1,156 @@
import {
AutocompleteInteraction,
ButtonBuilder,
ButtonInteraction,
CacheType,
ChatInputCommandInteraction,
EmbedBuilder,
Interaction,
REST,
RESTPostAPIChatInputApplicationCommandsJSONBody,
Routes,
SlashCommandBuilder,
SlashCommandSubcommandsOnlyBuilder,
} from 'discord.js';
import discord from '../../../discord.json' assert { type: 'json' };
import debug from '../../commands/debug.js';
import eligible from '../../commands/eligible.js';
import evolution from '../../commands/evolution.js';
import help from '../../commands/help.js';
import inactive from '../../commands/inactive.js';
import ownedtanks from '../../commands/ownedtanks.js';
import ping from '../../commands/ping.js';
import playerachievements from '../../commands/playerachievements.js';
import playerinfo from '../../commands/playerinfo.js';
import searchclans from '../../commands/searchclans.js';
import searchplayers from '../../commands/searchplayers.js';
import searchtanks from '../../commands/searchtanks.js';
import stats from '../../commands/stats.js';
import today from '../../commands/today.js';
import verify from '../../commands/verify.js';
import { DISCORD_TOKEN } from '../../core/process/args.js';
import getClientId from '../../core/process/getClientId.js';
import isDev from '../../core/process/isDev.js';
import handleAutocomplete from './handlers/autocomplete.js';
import handleButton from './handlers/button.js';
import handleChatInputCommand from './handlers/chatInputCommand.js';
export type InteractionRawReturnable =
| EmbedBuilder
| ButtonBuilder
| JSX.Element;
export type InteractionIterableReturnable =
| InteractionRawReturnable
| InteractionRawReturnable[];
export type InteractionReturnable =
| InteractionIterableReturnable
| Promise<InteractionIterableReturnable>;
export type CommandRegistry = {
inDevelopment: boolean;
inProduction: boolean;
inPublic: boolean;
command:
| SlashCommandBuilder
| SlashCommandSubcommandsOnlyBuilder
| Omit<SlashCommandBuilder, 'addSubcommand' | 'addSubcommandGroup'>;
autocomplete?: (interaction: AutocompleteInteraction<CacheType>) => void;
button?: (interaction: ButtonInteraction<CacheType>) => InteractionReturnable;
} & (
| {
handlesInteraction?: false;
execute: (
interaction: ChatInputCommandInteraction<CacheType>,
) => InteractionReturnable;
}
| {
handlesInteraction: true;
execute: (interaction: ChatInputCommandInteraction<CacheType>) => void;
}
);
const rest = new REST().setToken(DISCORD_TOKEN);
export const commands: Record<string, CommandRegistry> = (
[
debug,
eligible,
help,
inactive,
ownedtanks,
playerachievements,
playerinfo,
searchclans,
searchplayers,
searchtanks,
stats,
today,
verify,
ping,
evolution,
] as CommandRegistry[]
).reduce((accumulator, registry) => {
// if (isDev()) registry.command.setName(`${registry.command.name}dev`);
return { ...accumulator, [registry.command.name]: registry };
}, {});
export const guildCommands: RESTPostAPIChatInputApplicationCommandsJSONBody[] =
[];
export const publicCommands: RESTPostAPIChatInputApplicationCommandsJSONBody[] =
[];
Object.entries(commands).forEach(([, registry]) => {
if (isDev() ? registry.inDevelopment : registry.inProduction) {
const json = registry.command.toJSON();
if (registry.inPublic) {
publicCommands.push(json);
} else {
guildCommands.push(json);
}
}
});
try {
console.log(`Refreshing ${publicCommands.length} public command(s).`);
rest
.put(Routes.applicationCommands(getClientId()), { body: publicCommands })
.then((publicData) => {
console.log(
`Successfully refreshed ${
(publicData as RESTPostAPIChatInputApplicationCommandsJSONBody[])
.length
} public command(s).`,
);
});
console.log(`Refreshing ${guildCommands.length} guild command(s).`);
rest
.put(
Routes.applicationGuildCommands(getClientId(), discord.tres_guild_id),
{ body: guildCommands },
)
.then((guildData) => {
console.log(
`Successfully refreshed ${
(guildData as unknown[]).length
} guild command(s).`,
);
});
} catch (error) {
console.error(error);
}
export default async function interactionCreate(
interaction: Interaction<CacheType>,
) {
if (interaction.isAutocomplete()) {
handleAutocomplete(interaction);
} else if (interaction.isChatInputCommand()) {
handleChatInputCommand(interaction);
} else if (interaction.isButton()) {
handleButton(interaction);
}
}