Skip to content

Commit

Permalink
refactor cleanup interface (#67)
Browse files Browse the repository at this point in the history
* chore: ignore all .env files from tracking

* refactor: change all interface to types where applicable

* chore: use is-production util in entrypoint script
  • Loading branch information
samhwang authored Apr 26, 2024
1 parent c9bf205 commit 6b4a993
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 21 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ coverage
dist

# Env
.env
.env*
!.env.sample
3 changes: 2 additions & 1 deletion bin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -15,7 +16,7 @@ async function main(): Promise<void> {
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');
Expand Down
33 changes: 18 additions & 15 deletions src/discord/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<unknown> {
const rest = new REST({ version: '10' }).setToken(token);
return rest.put(request, { body });
}

export async function deployGuildCommands(commandList: SlashCommand[], config: DiscordRequestConfig): Promise<unknown> {
const { token, clientId, guildId } = config;
type GlobalRequestConfig = {
token: string;
clientId: string;
};

export async function deployGlobalCommands(commandList: SlashCommand[], config: GlobalRequestConfig): Promise<unknown> {
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<DiscordRequestConfig, 'guildId'>): Promise<unknown> {
const { token, clientId } = config;
type GuildRequestConfig = GlobalRequestConfig & {
guildId: string;
};

export async function deployGuildCommands(commandList: SlashCommand[], config: GuildRequestConfig): Promise<unknown> {
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 });
}
8 changes: 4 additions & 4 deletions src/slash-commands/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import type { AutocompleteHandler } from '../autocompletes/builder';

export type SlashCommandHandler = (interaction: ChatInputCommandInteraction) => Promise<void>;

export interface SlashCommand {
export type SlashCommand = {
data: Omit<SlashCommandBuilder, 'addSubcommandGroup' | 'addSubcommand'> | SlashCommandSubcommandsOnlyBuilder;
execute: SlashCommandHandler;
autocomplete?: AutocompleteHandler;
}
};

export interface Subcommand {
export type Subcommand = {
data: SlashCommandSubcommandBuilder | ((subcommandGroup: SlashCommandSubcommandBuilder) => SlashCommandSubcommandBuilder);
execute: SlashCommandHandler;
}
};

0 comments on commit 6b4a993

Please sign in to comment.