From 152e3619aac0088f0a7e6f87d344e66b7daea4ad Mon Sep 17 00:00:00 2001 From: Starman <30315137+Starman3787@users.noreply.github.com> Date: Mon, 18 Nov 2024 21:11:17 +0000 Subject: [PATCH] add emoji registration methods --- src/Client.js | 34 +++++++++++++++++++++++ src/rest/endpoints.js | 20 ++++++++++++++ test/Client.js | 63 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 117 insertions(+) diff --git a/src/Client.js b/src/Client.js index c1cc82ba..2598f1db 100644 --- a/src/Client.js +++ b/src/Client.js @@ -560,6 +560,40 @@ class Client extends EventsEmitter { ); } + /** + * Fetches the emojis for this client. + * @returns {Array} + * @public + * @method + * @async + */ + async fetchEmojis() { + return this.request.makeRequest("getClientEmojis", [this.user.id]); + } + + /** + * Creates an emoji for this client. + * @param {Object} emoji The emoji to create. + * @param {String} emoji.name The name of the emoji. + * @param {String} emoji.image The image of the emoji (base64, should start with "data:image/png;base64,"). + * @returns {Promise} + * @public + * @method + * @async + * @throws {TypeError} + */ + async createEmoji({ name, image }) { + if (typeof name !== "string") + throw new TypeError(`GLUON: Name is not a string. Got ${typeof name}`); + if (typeof image !== "string") + throw new TypeError(`GLUON: Image is not a string. Got ${typeof image}`); + + return this.request.makeRequest("createClientEmoji", [this.user.id], { + name, + image, + }); + } + /** * Sets the bot's status across all shards. * @param {Object} status Status options. diff --git a/src/rest/endpoints.js b/src/rest/endpoints.js index e5401e69..5bf99cf2 100644 --- a/src/rest/endpoints.js +++ b/src/rest/endpoints.js @@ -404,4 +404,24 @@ export default { return HttpResponse.json(TEST_DATA.TEXT_CHANNEL); }, }, + getClientEmojis: { + path: (client_id) => { + return `/applications/${client_id}/emojis`; + }, + method: "GET", + majorParams: [0], + mockResponse: ({ params } = {}) => { + return HttpResponse.json([TEST_DATA.EMOJI]); + }, + }, + postAddClientEmoji: { + path: (client_id) => { + return `/applications/${client_id}/emojis`; + }, + method: "POST", + majorParams: [0], + mockResponse: ({ params } = {}) => { + return HttpResponse.json(TEST_DATA.EMOJI); + }, + }, }; diff --git a/test/Client.js b/test/Client.js index 4e8f3fd4..6904ca5b 100644 --- a/test/Client.js +++ b/test/Client.js @@ -321,6 +321,69 @@ describe("Client", function () { ); }); }); + context("check fetchEmojis", function () { + it("should be a function", function () { + const client = new Client({ + intents: INTENTS.GUILDS, + }); + expect(client.fetchEmojis).to.be.a("function"); + }); + it("should call getGuildEmojis with the correct arguments", async function () { + const client = new Client({ + intents: INTENTS.GUILDS, + }); + client.request = { makeRequest: () => {} }; + client.user = new User(client, TEST_DATA.CLIENT_USER); + const request = spy(client.request, "makeRequest"); + await client.fetchEmojis(); + expect(request).to.be.calledOnce; + expect(request).to.be.calledOnceWith("getClientEmojis", [ + TEST_DATA.CLIENT_USER.id, + ]); + }); + }); + context("check createEmoji", function () { + it("should be a function", function () { + const client = new Client({ + intents: INTENTS.GUILDS, + }); + expect(client.createEmoji).to.be.a("function"); + }); + it("should throw an error if name is not a string", async function () { + const client = new Client({ + intents: INTENTS.GUILDS, + }); + await expect(client.createEmoji({ name: 1 })).to.be.rejectedWith( + "GLUON: Name is not a string.", + ); + }); + it("should throw an error if image is not a string", async function () { + const client = new Client({ + intents: INTENTS.GUILDS, + }); + await expect( + client.createEmoji({ name: "test", image: 1 }), + ).to.be.rejectedWith("GLUON: Image is not a string."); + }); + it("should call createGuildEmoji with the correct arguments", async function () { + const client = new Client({ + intents: INTENTS.GUILDS, + }); + client.request = { makeRequest: () => {} }; + client.user = new User(client, TEST_DATA.CLIENT_USER); + const request = spy(client.request, "makeRequest"); + await client.createEmoji({ name: "test", image: "testImage" }); + expect(request).to.be.calledOnce; + expect(request).to.be.calledOnceWith( + "createClientEmoji", + [TEST_DATA.CLIENT_USER.id], + { + name: "test", + image: "testImage", + }, + ); + }); + }); context("check setStatus", function () { it("should be a function", function () { const client = new Client({