From 89603e9b1ce902c911b9bcfacc8725f4b95a9b3d Mon Sep 17 00:00:00 2001 From: Mestery Date: Sat, 8 May 2021 18:00:26 +0200 Subject: [PATCH 1/3] feat: typing indicator Closes https://github.com/readthedocs-fr/bin-client-discord/issues/23. --- src/helpers/__tests__/sendBinEmbed.test.ts | 26 +++++++++++++++++----- src/helpers/sendBinEmbed.ts | 10 +++------ 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/src/helpers/__tests__/sendBinEmbed.test.ts b/src/helpers/__tests__/sendBinEmbed.test.ts index 412f030..6ec34d5 100644 --- a/src/helpers/__tests__/sendBinEmbed.test.ts +++ b/src/helpers/__tests__/sendBinEmbed.test.ts @@ -19,6 +19,8 @@ class MockMessage { public readonly channel = { send: jest.fn(async () => this), + startTyping: asyncFn(), + stopTyping: jest.fn(() => {}), }; public readonly reactions = { @@ -53,9 +55,10 @@ describe(sendBinEmbed, () => { attachments.clone().set("3", new MessageAttachment(`${cdnLink}4.jpg`, "4.jpg", { size: 1e6 })), ); - expect(message.channel.send).toBeCalledTimes(2); - expect(message.channel.send).toBeCalledWith("Transformation du message en cours..."); - expect(message.channel.send).toHaveBeenLastCalledWith({ + expect(message.channel.startTyping).toBeCalledTimes(1); + expect(message.channel.stopTyping).not.toBeCalled(); + expect(message.channel.send).toBeCalledTimes(1); + expect(message.channel.send).toBeCalledWith({ embed: new MessageEmbed({ description: "hey" }) .setAuthor(message.member!.displayName, message.author.displayAvatarURL()) .setTimestamp(message.createdAt) @@ -64,6 +67,19 @@ describe(sendBinEmbed, () => { }); }); + it("should start and stop typing correctly", async () => { + const message = new MockMessage(); + message.channel.send = jest.fn(async () => { + // eslint-disable-next-line @typescript-eslint/no-throw-literal + throw "Error"; + }); + + await sendBinEmbed((message as unknown) as Message, "hey"); + + expect(message.channel.startTyping).toBeCalledTimes(1); + expect(message.channel.stopTyping).toBeCalledTimes(1); + }); + it("should react with 🗑️", async () => { const message = new MockMessage(); await sendBinEmbed((message as unknown) as Message, "hey"); @@ -80,10 +96,10 @@ describe(sendBinEmbed, () => { expect(message.reactions.removeAll).not.toBeCalled(); }); - it("should delete the right number of messages", async () => { + it("should delete the right messages", async () => { const message = new MockMessage(); await sendBinEmbed((message as unknown) as Message, "hey"); - expect(message.delete).toBeCalledTimes(3); + expect(message.delete).toBeCalledTimes(2); }); }); diff --git a/src/helpers/sendBinEmbed.ts b/src/helpers/sendBinEmbed.ts index 65345d1..c3f0070 100644 --- a/src/helpers/sendBinEmbed.ts +++ b/src/helpers/sendBinEmbed.ts @@ -18,7 +18,7 @@ export async function sendBinEmbed( extender(embed); } - const waitMessage = await message.channel.send("Transformation du message en cours...").catch(noop); + message.channel.startTyping(); const files: MessageAttachment[] = []; if (attachments) { @@ -33,18 +33,14 @@ export async function sendBinEmbed( } } - const botMessage = await message.channel.send({ embed, files }).catch(noop); - - if (waitMessage?.deletable) { - await waitMessage.delete().catch(noop); - } + const botMessage = await message.channel.send({ embed, files }).catch(() => message.channel.stopTyping()); if (!botMessage) { return; } if (message.deletable) { - await message.delete().catch(noop); + message.delete().catch(noop); } await botMessage.react("🗑️"); From 605be0876c14bb9d7792e48ba97d096b65871783 Mon Sep 17 00:00:00 2001 From: Mestery Date: Sat, 8 May 2021 19:42:47 +0200 Subject: [PATCH 2/3] feat: ping embed style --- src/events/message/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/events/message/index.ts b/src/events/message/index.ts index e0de820..7d2c71b 100644 --- a/src/events/message/index.ts +++ b/src/events/message/index.ts @@ -42,8 +42,8 @@ export default class MessageEvent extends Event { const embed = new MessageEmbed() .setColor(binHealth ? 0x2ab533 : 0xf33030) .addField("État du bin", binHealth ? "En ligne" : "Hors ligne", true) - .addField("Latence du bot", `${pingMessage.createdTimestamp - message.createdTimestamp}ms`, true) - .addField("Latence du WebSocket", `${Math.round(this.client.ws.ping)}ms`, true); + .addField("Latence du bot", `${pingMessage.createdTimestamp - message.createdTimestamp} ms`, true) + .addField("Latence du WebSocket", `${Math.round(this.client.ws.ping)} ms`, true); pingMessage.edit("", embed).catch(noop); From 49690873705411c22df82f1efcf9288f8da15555 Mon Sep 17 00:00:00 2001 From: Mestery Date: Wed, 12 May 2021 21:53:26 +0200 Subject: [PATCH 3/3] revert: feat: ping embed style This reverts commit 605be0876c14bb9d7792e48ba97d096b65871783. --- src/events/message/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/events/message/index.ts b/src/events/message/index.ts index 7d2c71b..e0de820 100644 --- a/src/events/message/index.ts +++ b/src/events/message/index.ts @@ -42,8 +42,8 @@ export default class MessageEvent extends Event { const embed = new MessageEmbed() .setColor(binHealth ? 0x2ab533 : 0xf33030) .addField("État du bin", binHealth ? "En ligne" : "Hors ligne", true) - .addField("Latence du bot", `${pingMessage.createdTimestamp - message.createdTimestamp} ms`, true) - .addField("Latence du WebSocket", `${Math.round(this.client.ws.ping)} ms`, true); + .addField("Latence du bot", `${pingMessage.createdTimestamp - message.createdTimestamp}ms`, true) + .addField("Latence du WebSocket", `${Math.round(this.client.ws.ping)}ms`, true); pingMessage.edit("", embed).catch(noop);