Skip to content

Commit

Permalink
feat: typing indicator
Browse files Browse the repository at this point in the history
Closes #23.
  • Loading branch information
Mesteery committed May 8, 2021
1 parent 56e453f commit 89603e9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
26 changes: 21 additions & 5 deletions src/helpers/__tests__/sendBinEmbed.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class MockMessage {

public readonly channel = {
send: jest.fn(async () => this),
startTyping: asyncFn(),
stopTyping: jest.fn(() => {}),
};

public readonly reactions = {
Expand Down Expand Up @@ -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)
Expand All @@ -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");
Expand All @@ -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);
});
});
10 changes: 3 additions & 7 deletions src/helpers/sendBinEmbed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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("🗑️");
Expand Down

0 comments on commit 89603e9

Please sign in to comment.