Skip to content

Commit

Permalink
Merge branch 'fixLocalization' into addMixinOfCommands
Browse files Browse the repository at this point in the history
  • Loading branch information
carafelix committed May 11, 2024
2 parents 0ea44ba + 761470e commit 680016e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
11 changes: 7 additions & 4 deletions src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class Command<C extends Context = Context> implements MiddlewareObj<C> {
private _scopes: BotCommandScope[] = [];
private _languages: Map<
string,
{ name: string; description: string }
{ name: string | RegExp; description: string }
> = new Map();
private _composer: Composer<C> = new Composer<C>();
private _options: CommandOptions = {
Expand All @@ -56,7 +56,7 @@ export class Command<C extends Context = Context> implements MiddlewareObj<C> {
* @access package
*/
constructor(
name: string,
name: string | RegExp,
description: string,
options: Partial<CommandOptions> = {},
) {
Expand Down Expand Up @@ -264,7 +264,7 @@ export class Command<C extends Context = Context> implements MiddlewareObj<C> {
*/
public localize(
languageCode: string,
name: string,
name: string | RegExp,
description: string,
) {
this._languages.set(languageCode, {
Expand Down Expand Up @@ -301,8 +301,11 @@ export class Command<C extends Context = Context> implements MiddlewareObj<C> {
* @returns Object representation of this command
*/
public toObject(languageCode = "default"): BotCommand {
const localizedName = this.getLocalizedName(languageCode);
return {
command: this.getLocalizedName(languageCode),
command: localizedName instanceof RegExp
? localizedName.source
: localizedName,
description: this.getLocalizedDescription(languageCode),
};
}
Expand Down
13 changes: 7 additions & 6 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
Middleware,
} from "./deps.deno.ts";
import { CommandOptions } from "./types.ts";
import { fuzzyMatch } from "./jaro-winkler.ts";

export type SetMyCommandsParams = {
/**
Expand Down Expand Up @@ -114,7 +115,7 @@ export class Commands<C extends Context> {
options?: Partial<CommandOptions>,
): Command<C>;
public command(
name: string,
name: string | RegExp,
description: string,
handlerOrOptions?:
| MaybeArray<Middleware<CommandContext<C>>>
Expand All @@ -135,11 +136,11 @@ export class Commands<C extends Context> {
this._commands.push(command);
return command;
}

/**
* Serializes the commands into multiple objects that can each be passed to a `setMyCommands` call.
*
* @returns One item for each combination of command + scope + language
*
* Note: also used in {@link fuzzyMatch} behind the {@link toJSON} alias
*/
public toArgs() {
this._populateMetadata();
Expand All @@ -152,9 +153,8 @@ export class Commands<C extends Context> {
language_code: language === "default"
? undefined
: language,
commands: commands.map((command) =>
command.toObject(language)
)
commands: commands
.map((command) => command.toObject(language))
.filter((args) => args.command.length > 0),
});
}
Expand All @@ -178,6 +178,7 @@ export class Commands<C extends Context> {
language_code: language === "default" ? undefined : language,
commands: this._commands
.filter((command) => command.scopes.length)
.filter((command) => typeof command.name === "string")
.map((command) => command.toObject(language)),
});
}
Expand Down

0 comments on commit 680016e

Please sign in to comment.