Skip to content

Commit

Permalink
fix(purge): Fixed bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
dev-737 committed Mar 21, 2023
1 parent bd1b0d4 commit e64bd8a
Showing 1 changed file with 13 additions and 17 deletions.
30 changes: 13 additions & 17 deletions src/Commands/Staff/purge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export default {
),
async execute(interaction: ChatInputCommandInteraction) {
const subcommand = interaction.options.getSubcommand();
const limit = interaction.options.getInteger('limit') || undefined;
const limit = interaction.options.getInteger('limit') || 100;
const emoji = interaction.client.emoji;
const { messageData } = getDb();

Expand Down Expand Up @@ -126,7 +126,6 @@ export default {
gt: fetchedMsg.timestamp,
},
},
orderBy: { id: 'asc' },
take: limit,
});
}
Expand Down Expand Up @@ -155,16 +154,14 @@ export default {
ephemeral: true,
});
}
await interaction.reply({
content: `${emoji.normal.loading} Purging...\n**Tip:** The \`Delete Message\` context menu is faster for smaller deletes.`,
ephemeral: true,
});
const initialReply = await interaction.reply(`${emoji.normal.loading} Purging...\n**Tip:** The \`Delete Message\` context menu is faster for smaller deletes.`);

let erroredMessageCount = 0;
const deletedMessagesArr: string[][] = [];
let deletedMessageCount = 0;
const deletedMessagesArr: string[] = [];

const startTime = performance.now();
const allNetworks = await getAllNetworks();

for (const network of allNetworks) {
try {
const channel = await interaction.client.channels.fetch(network.channelId);
Expand All @@ -179,7 +176,8 @@ export default {
if (messageIds.length < 1) continue;

await channel.bulkDelete(messageIds);
deletedMessagesArr.push(messageIds);
deletedMessagesArr.push(messageIds[0]);
deletedMessageCount += messageIds.length;
}
}
catch (e) {
Expand All @@ -189,26 +187,24 @@ export default {
}
}

const deletedMessages = deletedMessagesArr.flatMap((e) => e);
const resultEmbed = new EmbedBuilder()
.setTitle(`${emoji.icons.delete} Purge Results`)
.setDescription(`Finished purging from **${allNetworks.length}** networks.`)
.addFields([
{ name: 'Total Purged', value: `\`\`\`js\n${deletedMessages.length}\`\`\``, inline: true },
{ name: 'Total Purged', value: `\`\`\`js\n${deletedMessageCount}\`\`\``, inline: true },
{ name: 'Errored Purges', value: `\`\`\`js\n${erroredMessageCount}\`\`\``, inline: true },
{ name: 'Avg. purged/network', value: `\`\`\`js\n${deletedMessagesArr[0].length}\`\`\``, inline: true },
{ name: 'Purge Limit', value: `\`\`\`js\n${limit}\`\`\``, inline: true },
{ name: 'Purge Limit', value: `\`\`\`js\n${limit || 'None'}\`\`\``, inline: true },
{ name: 'Time Took', value: `\`\`\`elm\n${toHuman(performance.now() - startTime)}\`\`\``, inline: true },
])
.setFooter({ text: `Purged By: ${interaction.user.tag}`, iconURL: interaction.user.avatarURL() || undefined })
.setTimestamp()
.setColor('Orange');

interaction.followUp({ embeds: [resultEmbed] }).catch(captureException);
initialReply.edit({ content: '', embeds: [resultEmbed] }).catch(captureException);

// Remove the deleted messages from the database
await messageData.deleteMany({
where: { channelAndMessageIds: { some: { messageId: { in: deletedMessages } } } },
});
where: { channelAndMessageIds: { some: { messageId: { in: deletedMessagesArr } } } },
}).catch(captureException);
},
};
};

0 comments on commit e64bd8a

Please sign in to comment.