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("🗑️");