Skip to content

Commit

Permalink
fix interaction functions and expose token
Browse files Browse the repository at this point in the history
  • Loading branch information
Starman3787 committed Nov 14, 2024
1 parent e2c941a commit a4dfefd
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 33 deletions.
69 changes: 44 additions & 25 deletions src/structures/Interaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -286,57 +296,66 @@ 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<FileUpload>?} options.files The new files to send with the interaction response.
* @param {Array<Embed>?} options.embeds The new embeds to send with the interaction response.
* @param {Array<ActionRow>?} options.components The new components to send with the interaction response.
* Silently acknowledges an interaction.
* @returns {Promise<Interaction>}
* @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,
);

return this;
}

/**
* 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<FileUpload>?} options.files The new files to send with the interaction response.
* @param {Array<Embed>?} options.embeds The new embeds to send with the interaction response.
* @param {Array<ActionRow>?} options.components The new components to send with the interaction response.
* @returns {Promise<Interaction>}
* @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;
}

/**
Expand Down
43 changes: 35 additions & 8 deletions test/structures/Interaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});
});
Expand All @@ -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();
Expand Down Expand Up @@ -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",
);
Expand All @@ -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,
Expand Down

0 comments on commit a4dfefd

Please sign in to comment.