From 6b4a99372a64a76db57cf7cb751f286413139b1c Mon Sep 17 00:00:00 2001 From: Sam Huynh Date: Sat, 27 Apr 2024 07:55:30 +1000 Subject: [PATCH] refactor cleanup interface (#67) * chore: ignore all .env files from tracking * refactor: change all interface to types where applicable * chore: use is-production util in entrypoint script --- .gitignore | 3 ++- bin/index.ts | 3 ++- src/discord/deploy.ts | 33 ++++++++++++++++++--------------- src/slash-commands/builder.ts | 8 ++++---- 4 files changed, 26 insertions(+), 21 deletions(-) diff --git a/.gitignore b/.gitignore index ac908d4..0584ca2 100644 --- a/.gitignore +++ b/.gitignore @@ -10,4 +10,5 @@ coverage dist # Env -.env +.env* +!.env.sample diff --git a/bin/index.ts b/bin/index.ts index c2359a5..0ae9999 100644 --- a/bin/index.ts +++ b/bin/index.ts @@ -3,6 +3,7 @@ import { Result } from 'oxide.ts'; import { commands } from '../src/commands'; import { getClient as getDiscordClient } from '../src/discord/client'; import { deployGlobalCommands } from '../src/discord/deploy'; +import { isProduction } from '../src/utils/is-production'; import { loadEnv } from '../src/utils/load-env'; import { logger } from '../src/utils/logger'; @@ -15,7 +16,7 @@ async function main(): Promise { if (!client.user) throw new Error('Something went wrong!'); logger.info(`[main]: Logged in as ${client.user.tag}!`); - if (process.env.NODE_ENV === 'production') { + if (isProduction()) { // This should only be run once during the bot startup in production. // For development usage, please use `pnpm deploy:command` logger.info('[main]: Deploying global commands'); diff --git a/src/discord/deploy.ts b/src/discord/deploy.ts index 1d5c877..b8aa1a1 100644 --- a/src/discord/deploy.ts +++ b/src/discord/deploy.ts @@ -2,37 +2,40 @@ import { REST, type RequestData, type RouteLike } from '@discordjs/rest'; import { Routes } from 'discord-api-types/v10'; import type { SlashCommand } from '../slash-commands/builder'; -interface DiscordRequestConfig { - token: string; - clientId: string; - guildId: string; -} - -interface DiscordRequestPayload { +type DiscordRequestPayload = { request: RouteLike; - token: DiscordRequestConfig['token']; + token: string; body: RequestData['body']; -} +}; async function registerCommands({ request, token, body }: DiscordRequestPayload): Promise { const rest = new REST({ version: '10' }).setToken(token); return rest.put(request, { body }); } -export async function deployGuildCommands(commandList: SlashCommand[], config: DiscordRequestConfig): Promise { - const { token, clientId, guildId } = config; +type GlobalRequestConfig = { + token: string; + clientId: string; +}; + +export async function deployGlobalCommands(commandList: SlashCommand[], config: GlobalRequestConfig): Promise { + const { token, clientId } = config; const commands = commandList.map((cmd) => cmd.data.toJSON()); - const request = Routes.applicationGuildCommands(clientId, guildId); + const request = Routes.applicationCommands(clientId); return registerCommands({ request, token, body: commands }); } -export async function deployGlobalCommands(commandList: SlashCommand[], config: Omit): Promise { - const { token, clientId } = config; +type GuildRequestConfig = GlobalRequestConfig & { + guildId: string; +}; + +export async function deployGuildCommands(commandList: SlashCommand[], config: GuildRequestConfig): Promise { + const { token, clientId, guildId } = config; const commands = commandList.map((cmd) => cmd.data.toJSON()); - const request = Routes.applicationCommands(clientId); + const request = Routes.applicationGuildCommands(clientId, guildId); return registerCommands({ request, token, body: commands }); } diff --git a/src/slash-commands/builder.ts b/src/slash-commands/builder.ts index ac90c98..f07ed9a 100644 --- a/src/slash-commands/builder.ts +++ b/src/slash-commands/builder.ts @@ -3,13 +3,13 @@ import type { AutocompleteHandler } from '../autocompletes/builder'; export type SlashCommandHandler = (interaction: ChatInputCommandInteraction) => Promise; -export interface SlashCommand { +export type SlashCommand = { data: Omit | SlashCommandSubcommandsOnlyBuilder; execute: SlashCommandHandler; autocomplete?: AutocompleteHandler; -} +}; -export interface Subcommand { +export type Subcommand = { data: SlashCommandSubcommandBuilder | ((subcommandGroup: SlashCommandSubcommandBuilder) => SlashCommandSubcommandBuilder); execute: SlashCommandHandler; -} +};