Simplified choices

This commit is contained in:
mmacierzanka committed 2021-08-25 17:58:29 +02:00
1 parent 3946e7f1ab
commit 06bd5a7316
3 files changed
+11 -17

No files matched your search

+5 -2
View File
@@ -143,7 +143,10 @@ export class Command {
description: p.description || "No description",
required: !p.optional,
type: type,
choices: p.choices || [],
choices:
p.choices?.map((c) => {
return { name: c, value: c };
}) || [],
};
});
}
@@ -185,7 +188,7 @@ export class Command {
let usageTemplate: string = "";
this.parameters &&
this.parameters.map((e) => {
usageTemplate += `[${e.name} (${e.choices ? e.choices.map((c) => c.name).join(" / ") : e.type}${e.optional ? ", optional" : ""})] `;
usageTemplate += `[${e.name} (${e.choices ? e.choices.join(" / ") : e.type}${e.optional ? ", optional" : ""})] `;
});
return usageTemplate;
}
+6 -11
View File
@@ -1,18 +1,18 @@
import { CategoryChannel, Guild, GuildMember, NewsChannel, Role, StageChannel, StoreChannel, TextChannel, VoiceChannel } from "discord.js";
import { ParameterType, ParameterResolvable, Choice } from "./types.js";
import { ParameterType, ParameterResolvable } from "./types.js";
export interface ParameterSchema {
name: string;
description?: string;
optional: boolean;
type: ParameterType;
choices?: Choice[];
choices?: string[];
}
export class Parameter {
name: string;
description: string;
optional: boolean;
type: ParameterType;
choices?: Choice[];
choices?: string[];
constructor(options: ParameterSchema) {
this.name = options.name;
@@ -53,16 +53,11 @@ export class StringParameter extends InputParameter {
throw new Error(`Parameter type mismatch`);
}
if (parameter.choices) {
const match = parameter.choices.filter((c) => c.name.toLowerCase() === value?.toString().toLowerCase() || c.value.toLowerCase() === value?.toString().toLowerCase());
const match = parameter.choices.filter((c) => c.toLowerCase() === value?.toString().toLowerCase());
if (match.length === 0) {
throw new Error(
`Parameter "${parameter.name}" has incorrect value. Please enter one of the following values: ${parameter.choices
.map((c) => [c.name, c.value])
.flat(1)
.join(", ")}`
);
throw new Error(`Parameter "${parameter.name}" has incorrect value. Please enter one of the following values: ${parameter.choices.join(", ")}`);
} else {
super(parameter, match[0].value);
super(parameter, match[0]);
return;
}
}
-4
View File
@@ -48,7 +48,3 @@ export interface HelpMessageParams {
}
export type ParameterType = "string" | "boolean" | "number" | "user" | "role" | "channel" | "mentionable";
export type ParameterResolvable = string | boolean | number | ObjectID | undefined;
export interface Choice {
name: string;
value: string;
}