testing cmds now have module prop, not on filename

This commit is contained in:
jacoobes
2022-02-13 01:17:33 -06:00
parent fcaf992164
commit dfb34b99e3
3 changed files with 9 additions and 13 deletions

View File

@@ -64,7 +64,7 @@ export class Handler {
const cmdResult = (await this.commandResult(module, message, tryFmt.join(' ')))
if (cmdResult === undefined) return;
message.channel.send(cmdResult)
message.channel.send(cmdResult);
})
@@ -116,7 +116,7 @@ export class Handler {
if (module.mod.visibility === 'private') {
const checkIsTestServer = this.privateServers.find(({ id }) => id === message.guildId!)?.test;
if (checkIsTestServer === undefined) return 'This command has the private modifier but is not registered under Handler#privateServers';
if (checkIsTestServer !== module.testOnly) {
if (checkIsTestServer !== module.mod.test) {
const msg = `This command is only available on test servers.`; // TODO: Customizable private message
return msg;
@@ -144,13 +144,13 @@ export class Handler {
}[]
) {
for await (const { name, mod, absPath } of modArr) {
const { cmdName, testOnly } = Files.fmtFileName(name);
const cmdName = Files.fmtFileName(name);
switch (mod.type) {
case 1: Files.Commands.set(cmdName, { mod, options: [], testOnly }); break;
case 1: Files.Commands.set(cmdName, { mod, options: [] }); break;
case 2:
case (1 | 2): {
const options = ((await import(absPath)).options as ApplicationCommandOptionData[])
Files.Commands.set(cmdName, { mod, options: options ?? [], testOnly });
Files.Commands.set(cmdName, { mod, options: options ?? [] });
switch (mod.visibility) {
case 'private': {
// Loading guild slash commands only
@@ -172,7 +172,7 @@ export class Handler {
if (mod.alias.length > 0) {
for (const alias of mod.alias) {
Files.Alias.set(alias, { mod, options: [], testOnly })
Files.Alias.set(alias, { mod, options: [] })
}
}
}
@@ -270,6 +270,7 @@ export interface Module<T = string> {
desc: string,
visibility: Visibility,
type: CommandType,
test : boolean,
delegate: (eventParams: Context, args: Ok<T>) => Awaitable<Result<possibleOutput, string> | void>
parse?: (ctx: Context, args: Arg) => Utils.ArgType<T>
}

View File

@@ -15,7 +15,6 @@ import type * as Sern from '../sern';
export type CommandVal = {
mod: Sern.Module<unknown>,
options: ApplicationCommandOptionData[],
testOnly: boolean
}
export const Commands = new Map<string, CommandVal>();
export const Alias = new Map<string, CommandVal>();
@@ -37,12 +36,7 @@ async function readPath(dir: string, arrayOfFiles: string[] = []): Promise<strin
return arrayOfFiles;
}
export const fmtFileName = (n: string) => {
const endsW = n.toLowerCase().endsWith('-test.js') || n.toLowerCase().endsWith('-test.ts');
return endsW
? { cmdName: n.substring(0, n.length - 8), testOnly: true }
: { cmdName: n.substring(0, n.length - 3), testOnly: false };
};
export const fmtFileName = (n: string) => n.substring(0, n.length - 3);
/**
* @param {Sern.Handler} handler an instance of Sern.Handler

View File

@@ -27,6 +27,7 @@ export type Context = {
message: Option<Message>,
interaction: Option<CommandInteraction>
}
// `string` | `SlashOptions`, narrow down your type by checking `text` | `slash`
export type Arg = ParseType<{text : string, slash : SlashOptions}>
// TypeAlias for interaction.options