feat: add information about the channel (total deleted messages, total messages, pourcent and total channel remaining)
This commit is contained in:
parent
c918acb25f
commit
dfb74c400c
5
.prettierrc
Normal file
5
.prettierrc
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"tabWidth": 4,
|
||||||
|
"useTabs": true,
|
||||||
|
"printWidth": 300
|
||||||
|
}
|
63
src/index.ts
63
src/index.ts
@ -39,9 +39,7 @@ const axiosInstance = axios.create({
|
|||||||
});
|
});
|
||||||
|
|
||||||
const deleteMessage = async (channelId: string, messageId: bigint) => {
|
const deleteMessage = async (channelId: string, messageId: bigint) => {
|
||||||
return await axiosInstance.delete(
|
return await axiosInstance.delete(`/channels/${channelId}/messages/${messageId.toString()}`);
|
||||||
`/channels/${channelId}/messages/${messageId.toString()}`
|
|
||||||
);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
@ -52,9 +50,7 @@ async function main() {
|
|||||||
|
|
||||||
// Loop through all the folders in the messages directory to calculate the total number of messages
|
// Loop through all the folders in the messages directory to calculate the total number of messages
|
||||||
for (const channelFolder of channelsFolder) {
|
for (const channelFolder of channelsFolder) {
|
||||||
const channelDataFile = await fs
|
const channelDataFile = await fs.readFile(`./messages/${channelFolder}/channel.json`).catch(() => null);
|
||||||
.readFile(`./messages/${channelFolder}/channel.json`)
|
|
||||||
.catch(() => null);
|
|
||||||
if (!channelDataFile) continue;
|
if (!channelDataFile) continue;
|
||||||
|
|
||||||
// check if it's a DM
|
// check if it's a DM
|
||||||
@ -66,9 +62,7 @@ async function main() {
|
|||||||
if (destinataireId == "Deleted User") continue;
|
if (destinataireId == "Deleted User") continue;
|
||||||
|
|
||||||
// get messages.json
|
// get messages.json
|
||||||
const messagesData = await fs
|
const messagesData = await fs.readFile(`./messages/${channelFolder}/messages.json`).catch(() => null);
|
||||||
.readFile(`./messages/${channelFolder}/messages.json`)
|
|
||||||
.catch(() => null);
|
|
||||||
if (!messagesData) continue;
|
if (!messagesData) continue;
|
||||||
|
|
||||||
// get messagesDeleted.json and filter to get only messages not deleted
|
// get messagesDeleted.json and filter to get only messages not deleted
|
||||||
@ -78,9 +72,7 @@ async function main() {
|
|||||||
CHANNELS_DATA.push(channelData);
|
CHANNELS_DATA.push(channelData);
|
||||||
MESSAGES_DATA[channelData.id] = JSON.parse(messagesData.toString());
|
MESSAGES_DATA[channelData.id] = JSON.parse(messagesData.toString());
|
||||||
|
|
||||||
totalDeleted += messages.filter(
|
totalDeleted += messages.filter((message) => message?.Deleted == true).length;
|
||||||
(message) => message?.Deleted == true
|
|
||||||
).length;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Loop through all the folders in the messages directory to delete the messages
|
// Loop through all the folders in the messages directory to delete the messages
|
||||||
@ -92,25 +84,24 @@ async function main() {
|
|||||||
if (destinataireId == "Deleted User") continue;
|
if (destinataireId == "Deleted User") continue;
|
||||||
|
|
||||||
// get messagesDeleted.json and filter to get only messages not deleted
|
// get messagesDeleted.json and filter to get only messages not deleted
|
||||||
const messages: MessageData[] = MESSAGES_DATA[channelData.id],
|
const channelMessages: MessageData[] = MESSAGES_DATA[channelData.id],
|
||||||
messagesNotDeleted = messages.filter((message) => {
|
messagesNotDeleted = channelMessages.filter((message) => {
|
||||||
return !message.Deleted;
|
return !message.Deleted;
|
||||||
});
|
});
|
||||||
|
|
||||||
for (const message of messagesNotDeleted) {
|
for (const message of messagesNotDeleted) {
|
||||||
const totalPourcentage = (
|
const totalChannelDeletedMessages: number = channelMessages.filter((message) => message.Deleted).length;
|
||||||
(totalDeleted / totalMessages) *
|
const totalChannelMessages: number = channelMessages.length;
|
||||||
100
|
const channelTotalPourcentage: string = ((totalChannelDeletedMessages / totalChannelMessages) * 100).toFixed(2);
|
||||||
).toFixed(2);
|
const channelTotalTimeReamining: number = (channelMessages.length - channelMessages.filter((message) => message.Deleted).length) * secondsDelay;
|
||||||
|
|
||||||
const timeReamining = (totalMessages - totalDeleted) * secondsDelay;
|
const totalPourcentage: string = ((totalDeleted / totalMessages) * 100).toFixed(2);
|
||||||
|
const totalTimeReamining: number = (totalMessages - totalDeleted) * secondsDelay;
|
||||||
|
|
||||||
console.log(
|
console.log(
|
||||||
`Deleting message ${message.ID} in channel ${
|
`Deleting message ${message.ID} in channel ${channelData.id} with user ${destinataireId}
|
||||||
channelData.id
|
- Channel: ${channelMessages.filter((message) => message.Deleted).length}/${channelMessages.length} - ${channelTotalPourcentage}% - ${moment.duration(channelTotalTimeReamining, "seconds").humanize()} remaining
|
||||||
} with ${destinataireId} (${totalDeleted}/${totalMessages} - ${totalPourcentage}%) - ${moment
|
- Total: ${totalDeleted}/${totalMessages} - ${totalPourcentage}% - ${moment.duration(totalTimeReamining, "seconds").humanize()} remaining`
|
||||||
.duration(timeReamining, "seconds")
|
|
||||||
.humanize()} remaining`
|
|
||||||
);
|
);
|
||||||
|
|
||||||
// Delete the message
|
// Delete the message
|
||||||
@ -118,29 +109,17 @@ async function main() {
|
|||||||
.then(async () => {
|
.then(async () => {
|
||||||
// Update the message data
|
// Update the message data
|
||||||
message.Deleted = true;
|
message.Deleted = true;
|
||||||
await fs.writeFile(
|
await fs.writeFile(`./messages/c${channelData.id}/messages.json`, JSON.stringify(channelMessages, null, 2));
|
||||||
`./messages/c${channelData.id}/messages.json`,
|
|
||||||
JSON.stringify(messages, null, 2)
|
|
||||||
);
|
|
||||||
})
|
})
|
||||||
.catch(async (error) => {
|
.catch(async (error) => {
|
||||||
const errorMessage = (error as any)?.response?.data
|
const errorMessage = (error as any)?.response?.data?.message as string;
|
||||||
?.message as string;
|
|
||||||
|
|
||||||
console.error(errorMessage);
|
console.error(errorMessage);
|
||||||
|
|
||||||
if (
|
if (errorMessage && ["Message inconnu", "message système"].some((msg) => errorMessage?.includes(msg))) {
|
||||||
errorMessage &&
|
|
||||||
["Message inconnu", "message système"].some((msg) =>
|
|
||||||
errorMessage?.includes(msg)
|
|
||||||
)
|
|
||||||
) {
|
|
||||||
// Update the message data
|
// Update the message data
|
||||||
message.Deleted = true;
|
message.Deleted = true;
|
||||||
await fs.writeFile(
|
await fs.writeFile(`./messages/c${channelData.id}/messages.json`, JSON.stringify(channelMessages, null, 1));
|
||||||
`./messages/c${channelData.id}/messages.json`,
|
|
||||||
JSON.stringify(messages, null, 1)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -150,9 +129,7 @@ async function main() {
|
|||||||
await wait(secondsDelay * 1000);
|
await wait(secondsDelay * 1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(
|
console.log(`Finished deleting messages in channel ${channelData.id} with ${destinataireId}`);
|
||||||
`Finished deleting messages in channel ${channelData.id} with ${destinataireId}`
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Log the totals
|
// Log the totals
|
||||||
|
Loading…
Reference in New Issue
Block a user