Skip to content

Commit

Permalink
feat: list subcommand (#113)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
EvolutionX-10 and jacoobes authored Aug 31, 2023
1 parent 42bd528 commit 5fd2a7b
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/commands/extra.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
67 changes: 67 additions & 0 deletions src/commands/list.ts
Original file line number Diff line number Diff line change
@@ -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<GuildId, CommandData[]> = 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<number, string> = {
1: magentaBright('Slash'),
2: magentaBright('User'),
3: magentaBright('Message'),
};

const AppCommandOptionType: Record<number, string> = {
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(' ');
5 changes: 5 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions src/utilities/getSern.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}
34 changes: 34 additions & 0 deletions src/utilities/types.ts
Original file line number Diff line number Diff line change
@@ -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<string, string>;
description: string;
description_localizations?: Record<string, string>;
dm_permission: boolean;
guild_id: string;
nsfw: boolean;
options?: OptionData[];
}

interface OptionData {
type: number;
name: string;
name_localizations?: Record<string, string>;
description: string;
description_localizations?: Record<string, string>;
required?: boolean;
choices?: ChoiceData[];
options?: OptionData[];
}

interface ChoiceData {
name: string;
value: string | number;
}

0 comments on commit 5fd2a7b

Please sign in to comment.