Skip to content

Commit

Permalink
feat: add CommandCustomIdCodec#getNameTreeFromId() and deserializeToN…
Browse files Browse the repository at this point in the history
…ameTree()

This should help hide the specific implementation of CommandCustomIdCodec
  • Loading branch information
Amgelo563 committed Jul 10, 2024
1 parent fa55bb3 commit 4828ee5
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,15 @@ export interface CommandCustomIdCodec
*/
createIteratorFromCustomId(commandCustomId: string): StringIterator | null;

/** Returns the separator used to serialize names in command customIds. */
getNamesSeparator(): string;
/** Returns a name tree given the passed command id, extracted with {@link deserializeToObjectId}. */
getNameTreeFromId(id: string): [string, ...string[]];

/** Returns a name tree from a command customId.
* Alias of:
* ```ts
* const id = codec.deserializeToObjectId(interaction.customId);
* const nameTree = codec.getNameTreeFromId(id);
* ```
*/
deserializeToNameTree(customId: string): [string, ...string[]] | null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -304,12 +304,9 @@ export class DefaultCommandManager implements CommandManager {
} else {
const { customId } = interaction;

const commandName = this.customIdCodec.deserializeToObjectId(customId);
if (!commandName) return false;
const names = this.customIdCodec.deserializeToNameTree(customId);
if (!names) return false;

const names = commandName.split(
this.customIdCodec.getNamesSeparator(),
) as [string, ...string[]];
const found = this.repository.locateByNameTree(...names);
if (!found || found.isParent() || found.isSubCommandGroup()) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,15 @@ export class DefaultCommandCustomIdCodec
return new DefaultCommandCustomIdCodec();
}

public getNamesSeparator(): string {
return this.namesSeparator;
public getNameTreeFromId(id: string): [string, ...string[]] {
return id.split(this.namesSeparator) as [string, ...string[]];
}

public deserializeToNameTree(customId: string): [string, ...string[]] | null {
const id = this.deserializeToObjectId(customId);
if (!id) return null;

return this.getNameTreeFromId(id);
}

/** @inheritDoc */
Expand Down

0 comments on commit 4828ee5

Please sign in to comment.