From a4dfefd9f5b1d4940cd4728643b505fcddedc06d Mon Sep 17 00:00:00 2001 From: Starman <30315137+Starman3787@users.noreply.github.com> Date: Thu, 14 Nov 2024 17:31:55 +0000 Subject: [PATCH] fix interaction functions and expose token --- src/structures/Interaction.js | 69 ++++++++++++++++++++++------------ test/structures/Interaction.js | 43 +++++++++++++++++---- 2 files changed, 79 insertions(+), 33 deletions(-) diff --git a/src/structures/Interaction.js b/src/structures/Interaction.js index 9cd15fb6..b56d6059 100644 --- a/src/structures/Interaction.js +++ b/src/structures/Interaction.js @@ -102,6 +102,16 @@ class Interaction { return String(this.#_id); } + /** + * The token of the interaction. + * @type {String} + * @readonly + * @public + */ + get token() { + return this.#token; + } + /** * The type of interaction. * @type {Number} @@ -286,32 +296,20 @@ class Interaction { } /** - * Edits a response to an interaction. Works up to 15 minutes after the response was sent. - * @param {Object?} options The new interaction response options. - * @param {String?} options.content The new content of the interaction response. - * @param {Array?} options.files The new files to send with the interaction response. - * @param {Array?} options.embeds The new embeds to send with the interaction response. - * @param {Array?} options.components The new components to send with the interaction response. + * Silently acknowledges an interaction. * @returns {Promise} * @public * @async * @method - * @throws {Error | TypeError} */ - async edit({ content, files, embeds, components } = {}) { - Message.sendValidation({ content, embeds, components, files }); - + async acknowledge() { const body = {}; - if (content) body.content = content; - if (files) body.files = files; - if (embeds) body.embeds = embeds; - if (components) - body.components = Array.isArray(components) != true ? components : []; + body.type = 6; await this.#_client.request.makeRequest( - "patchOriginalInteractionResponse", - [this.#_client.user.id, this.#token], + "postInteractionResponse", + [this.id, this.#token], body, ); @@ -319,24 +317,45 @@ class Interaction { } /** - * Silently acknowledges an interaction. + * Edits a response to an interaction. Works up to 15 minutes after the response was sent. + * @param {Client} client The client instance. + * @param {String} interactionToken The interaction token. + * @param {Object?} options The new interaction response options. + * @param {String?} options.content The new content of the interaction response. + * @param {Array?} options.files The new files to send with the interaction response. + * @param {Array?} options.embeds The new embeds to send with the interaction response. + * @param {Array?} options.components The new components to send with the interaction response. * @returns {Promise} * @public * @async * @method + * @throws {Error | TypeError} */ - async acknowledge() { + static async edit( + client, + interactionToken, + { content, files, embeds, components }, + ) { + if (!(client instanceof Client)) + throw new TypeError("GLUON: Client must be an instance of Client"); + if (typeof interactionToken !== "string") + throw new TypeError("GLUON: Interaction token must be a string"); + + Message.sendValidation({ content, embeds, components, files }); + const body = {}; - body.type = 6; + if (content) body.content = content; + if (files) body.files = files; + if (embeds) body.embeds = embeds; + if (components) + body.components = Array.isArray(components) != true ? components : []; - await this.#_client.request.makeRequest( - "postInteractionResponse", - [this.id, this.#token], + return client.request.makeRequest( + "patchOriginalInteractionResponse", + [client.user.id, interactionToken], body, ); - - return this; } /** diff --git a/test/structures/Interaction.js b/test/structures/Interaction.js index f4e7c8e2..20fc55f8 100644 --- a/test/structures/Interaction.js +++ b/test/structures/Interaction.js @@ -29,10 +29,11 @@ describe("Interaction", function () { expect(interaction).to.have.property("options"); expect(interaction).to.have.property("guild"); expect(interaction).to.have.property("channel"); + expect(interaction).to.have.property("token"); expect(interaction).to.have.property("textPrompt"); expect(interaction).to.have.property("autocompleteResponse"); expect(interaction).to.have.property("reply"); - expect(interaction).to.have.property("edit"); + expect(Interaction).to.have.property("edit"); expect(interaction).to.have.property("acknowledge"); }); }); @@ -46,6 +47,15 @@ describe("Interaction", function () { }); }); + context("check token", function () { + it("should have the correct token", function () { + const client = TEST_CLIENTS.ALL_CACHES_ENABLED(); + TEST_GUILDS.ALL_CACHES_ENABLED(client); + const interaction = new Interaction(client, TEST_DATA.INTERACTION); + expect(interaction.token).to.equal(TEST_DATA.INTERACTION.token); + }); + }); + context("check type", function () { it("should have the correct type", function () { const client = TEST_CLIENTS.ALL_CACHES_ENABLED(); @@ -314,14 +324,30 @@ describe("Interaction", function () { it("should be a function", function () { const client = TEST_CLIENTS.ALL_CACHES_ENABLED(); TEST_GUILDS.ALL_CACHES_ENABLED(client); - const interaction = new Interaction(client, TEST_DATA.INTERACTION); - expect(interaction.edit).to.be.a("function"); + expect(Interaction.edit).to.be.a("function"); + }); + it("should throw an error if client is not an instance of Client", async function () { + const client = TEST_CLIENTS.ALL_CACHES_ENABLED(); + TEST_GUILDS.ALL_CACHES_ENABLED(client); + await expect(Interaction.edit({})).to.be.rejectedWith( + Error, + "GLUON: Client must be an instance of Client", + ); + }); + it("should throw an error if interactionToken is not a string", async function () { + const client = TEST_CLIENTS.ALL_CACHES_ENABLED(); + TEST_GUILDS.ALL_CACHES_ENABLED(client); + await expect(Interaction.edit(client, 123)).to.be.rejectedWith( + Error, + "GLUON: Interaction token must be a string", + ); }); it("should throw an error if no content, files, embeds, or components are provided", async function () { const client = TEST_CLIENTS.ALL_CACHES_ENABLED(); TEST_GUILDS.ALL_CACHES_ENABLED(client); - const interaction = new Interaction(client, TEST_DATA.INTERACTION); - await expect(interaction.edit()).to.be.rejectedWith( + await expect( + Interaction.edit(client, "vcsfdjhdfkjvhkdf"), + ).to.be.rejectedWith( Error, "GLUON: Must provide content, embeds, components or files", ); @@ -330,14 +356,15 @@ describe("Interaction", function () { const client = TEST_CLIENTS.ALL_CACHES_ENABLED(); TEST_GUILDS.ALL_CACHES_ENABLED(client); const interaction = new Interaction(client, TEST_DATA.INTERACTION); - await expect(interaction.edit({ content: "test" })).to.not.be.rejected; + await expect( + Interaction.edit(client, "vcsfdjhdfkjvhkdf", { content: "test" }), + ).to.not.be.rejected; }); it("should call makeRequest with the correct parameters", async function () { const client = TEST_CLIENTS.ALL_CACHES_ENABLED(); const request = spy(client.request, "makeRequest"); TEST_GUILDS.ALL_CACHES_ENABLED(client); - const interaction = new Interaction(client, TEST_DATA.INTERACTION); - await interaction.edit({ content: "test" }); + await Interaction.edit(client, "vcsfdjhdfkjvhkdf", { content: "test" }); expect(request).to.be.calledOnce; expect(request).to.be.calledOnceWith("patchOriginalInteractionResponse", [ TEST_DATA.CLIENT_USER.id,