From 5fd2a7b4c4bc92467fbaa26005d261d4ed8b2a13 Mon Sep 17 00:00:00 2001 From: Evo <85353424+EvolutionX-10@users.noreply.github.com> Date: Fri, 1 Sep 2023 01:37:16 +0530 Subject: [PATCH] feat: list subcommand (#113) * chore: progress * chore: blaming regex. permalink: http://whatthecommit.com/7eaa73b94ca6e8f964d99b6f2db6e9e4 * chore: permalink: * chore: lol * chore: accidental commit permalink: http://whatthecommit.com/7c6c9323d8c243d10cd93c8bbbc55d09 * fix syntx * fix list not showing up * prety * chore: refresh lockfile * chore: progress * fix list not showing up * refactor: cleanup some mess * Update preprocessor.ts --------- Co-authored-by: Jacob Nguyen <76754747+jacoobes@users.noreply.github.com> --- src/commands/extra.ts | 1 + src/commands/list.ts | 67 ++++++++++++++++++++++++++++++++++++++++ src/index.ts | 5 +++ src/utilities/getSern.ts | 17 ++++++++++ src/utilities/types.ts | 34 ++++++++++++++++++++ 5 files changed, 124 insertions(+) create mode 100644 src/commands/list.ts create mode 100644 src/utilities/getSern.ts diff --git a/src/commands/extra.ts b/src/commands/extra.ts index 974450f..b810da2 100644 --- a/src/commands/extra.ts +++ b/src/commands/extra.ts @@ -7,5 +7,6 @@ export async function extra() { if (Object.keys(extra).length < 1) process.exit(1); const lang = extra.extra.includes('typescript') ? 'TS' : 'JS'; + await create(extra.extra.split('-')[0], lang, process.cwd(), true); } diff --git a/src/commands/list.ts b/src/commands/list.ts new file mode 100644 index 0000000..66fa974 --- /dev/null +++ b/src/commands/list.ts @@ -0,0 +1,67 @@ +import { blueBright, bold, cyanBright, greenBright, italic, magentaBright, underline } from 'colorette'; +import { getSern } from '../utilities/getSern'; +import { readFileSync } from 'node:fs'; +import type { CommandData, GuildId } from '../utilities/types'; + +export function list() { + const files = getSern(); + if (!files.includes('command-data-remote.json')) { + console.error(`No commands found\nPlease run ${cyanBright('sern commands publish')} to publish your commands`); + process.exit(1); + } + + const commands: Record = JSON.parse(readFileSync('.sern/command-data-remote.json', 'utf-8')); + const globalCommands = commands.global; + + delete commands.global; + console.log(bold('Global Commands')); + for (const command of globalCommands) log(command); + + console.log('\t'); + + for (const guildId in commands) { + const guildCommands = commands[guildId]; + console.log(`${bold('Guild Commands')} [${underline(cyanBright(guildId))}]`); + for (const command of guildCommands) log(command); + } +} + +const AppCommandsType: Record = { + 1: magentaBright('Slash'), + 2: magentaBright('User'), + 3: magentaBright('Message'), +}; + +const AppCommandOptionType: Record = { + 1: magentaBright('SubCommand'), + 2: magentaBright('SubCommand Group'), + 3: magentaBright('String'), + 4: magentaBright('Integer'), + 5: magentaBright('Boolean'), + 6: magentaBright('User'), + 7: magentaBright('Channel'), + 8: magentaBright('Role'), + 9: magentaBright('Mentionable'), + 10: magentaBright('Number'), + 11: magentaBright('Attachment'), +}; + +function log(command: CommandData) { + console.log(clean(`\t${cyanBright(command.name)} ${italic(command.description)} (${greenBright(command.id)})`)); + console.log(`\t Type: ${AppCommandsType[command.type]}`); + + if (command.options) { + console.log(`\t Options:`); + for (const option of command.options) { + console.log(`\t ${blueBright(option.name)}: ${AppCommandOptionType[option.type]}`); + if (option.options) { + console.log(`\t Options:`); + for (const subOption of option.options) { + console.log(`\t ${cyanBright(subOption.name)}: ${AppCommandOptionType[subOption.type]}`); + } + } + } + } +} + +const clean = (str: string) => str.split(' ').filter(Boolean).join(' '); diff --git a/src/index.ts b/src/index.ts index 1f6ce31..e6ea376 100644 --- a/src/index.ts +++ b/src/index.ts @@ -43,6 +43,11 @@ program // .option('--appId [applicationId]') .argument('[path]', 'path with respect to current working directory that will locate all published files') .action(async (...args) => importDynamic('publish.js').then((m) => m.publish(...args))) + ) + .addCommand( + new Command('list') // + .description('List all slash commands') + .action(async (...args) => importDynamic('list.js').then((m) => m.list(...args))) ); program diff --git a/src/utilities/getSern.ts b/src/utilities/getSern.ts new file mode 100644 index 0000000..d7e2cf8 --- /dev/null +++ b/src/utilities/getSern.ts @@ -0,0 +1,17 @@ +import { readdirSync } from 'node:fs'; +import { fromCwd } from './fromCwd'; +import { redBright, cyanBright } from 'colorette'; + +export function getSern() { + let files: string[] = []; + + try { + const sern = fromCwd('.sern'); + files = readdirSync(sern); + } catch (error) { + console.error(`${redBright('Error:')} Could not locate ${cyanBright('.sern')} directory`); + process.exit(1); + } finally { + return files; + } +} diff --git a/src/utilities/types.ts b/src/utilities/types.ts index cf7dad6..7aa886f 100644 --- a/src/utilities/types.ts +++ b/src/utilities/types.ts @@ -1 +1,35 @@ export type PackageManagerChoice = 'skip' | 'npm' | 'yarn'; + +export type GuildId = string; + +export interface CommandData { + id: string; + application_id: string; + version: string; + default_member_permissions?: string; + type: number; + name: string; + name_localizations?: Record; + description: string; + description_localizations?: Record; + dm_permission: boolean; + guild_id: string; + nsfw: boolean; + options?: OptionData[]; +} + +interface OptionData { + type: number; + name: string; + name_localizations?: Record; + description: string; + description_localizations?: Record; + required?: boolean; + choices?: ChoiceData[]; + options?: OptionData[]; +} + +interface ChoiceData { + name: string; + value: string | number; +}