MAJOR CHANGE: swapped calling function arguments + replaced parameters list with function that fetches parameter (earlier getParam)

This commit is contained in:
mmacierzanka committed 2021-08-08 04:03:00 +02:00
1 parent 207b358535
commit 4173c96a55
3 files changed
+46 -32

No files matched your search

+35 -26
View File
@@ -28,8 +28,11 @@ export class Command {
guilds?: string[]; guilds?: string[];
visible: boolean; visible: boolean;
private function: ( private function: (
interaction?: Message | CommandInteraction, params: (
cmdParams?: InputParameter[] query: string,
returnType?: "value" | "object"
) => ParameterResolvable | InputParameter | null,
interaction?: Message | CommandInteraction
) => ) =>
| void | void
| string | string
@@ -103,15 +106,21 @@ export class Command {
) { ) {
const fnResult = await this.function.call( const fnResult = await this.function.call(
this, this,
interaction, function (query: string, returnType?: "value" | "object") {
cmdParams return returnType === "object"
? cmdParams?.find((p) => p.name === query) || null
: cmdParams?.find((p) => p.name === query)?.value ||
null;
},
interaction
); );
if ( if (
"content" in (fnResult as ReplyMessageOptions) || fnResult instanceof Object &&
"embeds" in (fnResult as ReplyMessageOptions) || ("content" in (fnResult as any) ||
"files" in (fnResult as ReplyMessageOptions) || "embeds" in (fnResult as any) ||
"components" in (fnResult as ReplyMessageOptions) || "files" in (fnResult as any) ||
"sticker" in (fnResult as ReplyMessageOptions) "components" in (fnResult as any) ||
"sticker" in (fnResult as any))
) { ) {
await interaction.reply(fnResult as ReplyMessageOptions); await interaction.reply(fnResult as ReplyMessageOptions);
} else if (typeof fnResult == "string") { } else if (typeof fnResult == "string") {
@@ -144,16 +153,26 @@ export class Command {
}, 2000); }, 2000);
const fnResult = await this.function.call( const fnResult = await this.function.call(
this, this,
interaction, function (
cmdParams query: string,
returnType?: "value" | "object"
) {
return returnType === "object"
? cmdParams?.find((p) => p.name === query) ||
null
: cmdParams?.find((p) => p.name === query)
?.value || null;
},
interaction
); );
try { try {
if ( if (
"content" in (fnResult as ReplyMessageOptions) || fnResult instanceof Object &&
"embeds" in (fnResult as ReplyMessageOptions) || ("content" in (fnResult as any) ||
"files" in (fnResult as ReplyMessageOptions) || "embeds" in (fnResult as any) ||
"components" in (fnResult as ReplyMessageOptions) || "files" in (fnResult as any) ||
"sticker" in (fnResult as ReplyMessageOptions) "components" in (fnResult as any) ||
"sticker" in (fnResult as any))
) { ) {
interaction.replied interaction.replied
? await interaction.editReply( ? await interaction.editReply(
@@ -251,16 +270,6 @@ export class Command {
}; };
} }
getParam(
query: string,
list: InputParameter[],
returnType?: "value" | "object"
): ParameterResolvable | InputParameter | null {
return returnType === "object"
? list.find((p) => p.name === query) || null
: list.find((p) => p.name === query)?.value || null;
}
private static processPhrase( private static processPhrase(
phrase?: string | string[] phrase?: string | string[]
): string[] | undefined { ): string[] | undefined {
+6 -4
View File
@@ -23,15 +23,15 @@ export class HelpMessage extends Command {
type: "string", type: "string",
}, },
], ],
function: (_, a) => { function: (param, _) => {
const helpMsg = new MessageEmbed(); const helpMsg = new MessageEmbed();
helpMsg.setColor(params.color); helpMsg.setColor(params.color);
helpMsg.setTimestamp(); helpMsg.setTimestamp();
helpMsg.setFooter(botName || ""); helpMsg.setFooter(botName || "");
if (helpMsg != null) { if (helpMsg != null) {
if (a && a[0]) { if (param("command_name")) {
const cmd: Command | null = cmdManager.get( const cmd: Command | null = cmdManager.get(
a[0].value?.toString() || "", param("command_name")?.toString() || "",
"ALL" "ALL"
); );
if (cmd) { if (cmd) {
@@ -75,7 +75,9 @@ export class HelpMessage extends Command {
} }
} else { } else {
throw new ReferenceError( throw new ReferenceError(
`Command "${a[0].value}" does not exist` `Command "${param(
"command_name"
)}" does not exist`
); );
} }
} else { } else {
+5 -2
View File
@@ -30,8 +30,11 @@ export interface CommandBuilder {
guilds?: string[]; guilds?: string[];
visible?: boolean; visible?: boolean;
function: ( function: (
interaction?: Message | CommandInteraction, params: (
cmdParams?: InputParameter[] query: string,
returnType?: "value" | "object"
) => ParameterResolvable | InputParameter | null,
interaction?: Message | CommandInteraction
) => ) =>
| void | void
| string | string