From 85807f34f0da0bc76a69203f59880b8908c8f5b5 Mon Sep 17 00:00:00 2001 From: Rodry <38259440+ImRodry@users.noreply.github.com> Date: Thu, 7 Oct 2021 23:00:13 +0100 Subject: [PATCH 1/2] feat(CommandInteraction): add toString method --- src/structures/CommandInteraction.js | 11 +++++++++++ typings/index.d.ts | 1 + 2 files changed, 12 insertions(+) diff --git a/src/structures/CommandInteraction.js b/src/structures/CommandInteraction.js index 4c8bd5d7535a..da2a79b030a5 100644 --- a/src/structures/CommandInteraction.js +++ b/src/structures/CommandInteraction.js @@ -21,6 +21,17 @@ class CommandInteraction extends BaseCommandInteraction { this.transformResolved(data.data.resolved ?? {}), ); } + + /** + * Returns a string representation of the command interaction. + * This can then be copied by a user and executed again in a new command while keeping the option order. + * @returns {string} + */ + toString() { + return `/${this.commandName} ${this.options._group ? `${this.options._group} ` : ''}${ + this.options._subcommand ? `${this.options._subcommand} ` : '' + }${this.options._hoistedOptions.map(o => `${o.name}:${o.value}`).join(' ')}`; + } } module.exports = CommandInteraction; diff --git a/typings/index.d.ts b/typings/index.d.ts index 3bd0b3dfcf15..b73f3ba27016 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -603,6 +603,7 @@ export type GuildCommandInteraction { public options: CommandInteractionOptionResolver; + public toString(): string; } export class CommandInteractionOptionResolver { From e66c84aa264f34b138b857eed9087ffc114361de Mon Sep 17 00:00:00 2001 From: Rodry <38259440+ImRodry@users.noreply.github.com> Date: Fri, 8 Oct 2021 15:29:25 +0100 Subject: [PATCH 2/2] refactor: make method more verbose and remove extra space --- src/structures/CommandInteraction.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/structures/CommandInteraction.js b/src/structures/CommandInteraction.js index da2a79b030a5..c5dcfa2a3fd7 100644 --- a/src/structures/CommandInteraction.js +++ b/src/structures/CommandInteraction.js @@ -28,9 +28,13 @@ class CommandInteraction extends BaseCommandInteraction { * @returns {string} */ toString() { - return `/${this.commandName} ${this.options._group ? `${this.options._group} ` : ''}${ - this.options._subcommand ? `${this.options._subcommand} ` : '' - }${this.options._hoistedOptions.map(o => `${o.name}:${o.value}`).join(' ')}`; + const properties = [ + this.commandName, + this.options._group, + this.options._subcommand, + ...this.options._hoistedOptions.map(o => `${o.name}:${o.value}`), + ]; + return `/${properties.filter(Boolean).join(' ')}`; } }