Skip to content

Commit

Permalink
fix(purge-until): Fix message handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Marco (Valandur) committed Nov 27, 2019
1 parent d89baa2 commit 5649c0c
Showing 1 changed file with 20 additions and 22 deletions.
42 changes: 20 additions & 22 deletions src/moderation/commands/purge/purge-until.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,37 +26,35 @@ export default class extends Command {

public async action(message: Message, [untilMessageID]: [string], flags: {}, { guild, t }: Context): Promise<any> {
const embed = this.createEmbed({
title: t('cmd.purgeUntil.title')
title: t('cmd.purgeUntil.title'),
description: t('cmd.purgeUntil.inProgress')
});

const messages: Message[] = await message.channel.getMessages(1000, undefined, untilMessageID).catch(() => []);
const response = await this.sendReply(message, embed);

let amount = 0;
let messages: Message[];
while (!messages || messages.length > 1) {
messages = await message.channel.getMessages(1000, undefined, untilMessageID);

if (messages.length === 0) {
embed.description = t('cmd.purgeUntil.none');
return this.sendReply(message, embed);
} else if (!messages.some(m => m.id === untilMessageID)) {
embed.description = t('cmd.purgeUntil.msgNotFound');
return this.sendReply(message, embed);
} else {
try {
await this.client.deleteMessages(
message.channel.id,
messages.map(m => m.id)
messages.filter(m => m.id !== response.id).map(m => m.id)
);

embed.description = t('cmd.purgeUntil.text', {
amount: `**${messages.length}**`
});
} catch (error) {
embed.title = t('cmd.purgeUntil.error');
embed.description = JSON.stringify(error);
}

const response = await this.sendReply(message, embed);
if (response) {
const func = () => response.delete().catch(() => undefined);
setTimeout(func, 5000);
amount += messages.length;
} catch {
break;
}
}

embed.description = t('cmd.purgeUntil.text', {
amount: `**${amount}**`
});
await response.edit({ embed });

const func = () => response.delete().catch(() => undefined);
setTimeout(func, 5000);
}
}

0 comments on commit 5649c0c

Please sign in to comment.