Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: typing indicator #45

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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