Skip to content

Commit

Permalink
make archive method async
Browse files Browse the repository at this point in the history
  • Loading branch information
BodomBeach committed Jul 6, 2024
1 parent d71379f commit 3ea9c3d
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 52 deletions.
53 changes: 32 additions & 21 deletions src/jobs/channelCleanup.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,53 @@
const { archive } = require('../utils/archive.js');
const { archive } = require("../utils/archive.js");

function channelCleanup(client) {
async function channelCleanup(client) {
console.log("===== starting scheduled channel cleanup =====");


console.log('===== starting scheduled channel cleanup =====');
const allowedCategories = ['🪂 SORTIES', '🏃Sorties pas rapente'];
const allowedCategories = ["🪂 SORTIES", "🏃Sorties pas rapente"];
const guild = client.guilds.cache.get(process.env.GUILD_ID);
const categories = guild.channels.cache.filter(channel => allowedCategories.includes(channel.name))
const categories = guild.channels.cache.filter((channel) =>
allowedCategories.includes(channel.name)
);

const allowed = /(\d{1,2})-(\d{1,2})/
const allowed = /(\d{1,2})-(\d{1,2})/;

// do nothing for these format for now, too risky
const forbidden1 = /(\d{1,2})-(\d{1,2})-(\d{1,2})/ // 04-05-06
const forbidden2 = /-(janvier|fevrier|mars|avril|mai|juin|juillet|septembre|octobre|novembre|decembre)-/ // 04-05-juin-word
const forbidden1 = /(\d{1,2})-(\d{1,2})-(\d{1,2})/; // 04-05-06
const forbidden2 =
/-(janvier|fevrier|mars|avril|mai|juin|juillet|septembre|octobre|novembre|decembre)-/; // 04-05-juin

for (const category of categories.values()) {
const today = new Date();
const channels = category.children.cache;

categories.forEach(category => {
const today = new Date()
let channels = category.children.cache
channels.forEach(channel => {
for (const channel of channels.values()) {
const match = allowed.exec(channel.name);

let match = allowed.exec(channel.name)
// if month is found as a string (e.g. "juin"), do nothing for now, too risky
if (!allowed.exec(channel.name) || forbidden1.exec(channel.name) || forbidden2.exec(channel.name)) {
if (
!match ||
forbidden1.exec(channel.name) ||
forbidden2.exec(channel.name)
) {
console.log(`format invalide ${channel.name}`);
return
continue;
}

// archive channel 24 hours after actual expiry date. E.g. 18-05-plop will be archived on 20-05 at 1am
let channel_date = new Date(today.getFullYear(), (parseInt(match[2]) - 1), (parseInt(match[1]) + 2));
const channel_date = new Date(
today.getFullYear(),
parseInt(match[2]) - 1,
parseInt(match[1]) + 2
);

if (today > channel_date) {
console.log(`${channel.name} too old, archiving !`);
archive(channel)
console.log(`${channel.name} too old, archiving!`);
await archive(channel);
} else {
console.log(`${channel.name} OK`);
}
});
});
}
}
}

module.exports = { channelCleanup };
67 changes: 39 additions & 28 deletions src/jobs/serverStats.js
Original file line number Diff line number Diff line change
@@ -1,65 +1,76 @@
const { EmbedBuilder } = require('discord.js');
const { EmbedBuilder } = require("discord.js");

async function serverStats(client) {
console.log('===== starting scheduled server stats update =====');
console.log("===== starting scheduled server stats update =====");
const guild = client.guilds.cache.get(process.env.GUILD_ID);

// fetch members first, otherwise guild.roles.cache.get('1232623387965132820').members.size will be empty
let members = await guild.members.fetch();
const time = (new Date).toLocaleString(
'fr-FR',
{
weekday: 'long',
year: 'numeric',
month: 'long',
day: 'numeric',
hour: 'numeric',
minute: 'numeric',
second: 'numeric',
timeZone: 'Europe/Paris',
})
const time = new Date().toLocaleString("fr-FR", {
weekday: "long",
year: "numeric",
month: "long",
day: "numeric",
hour: "numeric",
minute: "numeric",
second: "numeric",
timeZone: "Europe/Paris",
});

const embed = new EmbedBuilder()
.setTitle('Statistiques du serveur')
.setTitle("Statistiques du serveur")
.setColor(0x0099ff)
.setThumbnail('https://cdn.discordapp.com/icons/943454897431523349/cadb5ff769c962489b5dd144acb1e2e3.webp?size=96')
.setThumbnail(
"https://cdn.discordapp.com/icons/943454897431523349/cadb5ff769c962489b5dd144acb1e2e3.webp?size=96"
)
.setFooter({ text: `Dernière mise à jour : ${time}` })
.addFields([
{
name: 'Membres total',
name: "Membres total",
value: guild.memberCount.toString(),
},
{
name: 'Admins',
value: guild.roles.cache.find(role => role.name === 'admins')?.members?.size?.toString() || '0',
name: "Admins",
value:
guild.roles.cache
.find((role) => role.name === "admins")
?.members?.size?.toString() || "0",
inline: true,
},
{
name: 'Staff',
value: guild.roles.cache.find(role => role.name === 'staff')?.members?.size?.toString() || '0',
name: "Staff",
value:
guild.roles.cache
.find((role) => role.name === "staff")
?.members?.size?.toString() || "0",
inline: true,
},
{
name: 'Biplaceurs',
value: guild.roles.cache.find(role => role.name === 'biplaceurs')?.members?.size?.toString() || '0',
name: "Biplaceurs",
value:
guild.roles.cache
.find((role) => role.name === "biplaceurs")
?.members?.size?.toString() || "0",
inline: true,
},
{
name: 'Nombre de salons',
name: "Nombre de salons",
value: guild.channels.cache.size.toString(),
},
]);

const statsChannel = client.channels.cache.find(channel => channel.name === 'stats');
const statsChannel = client.channels.cache.find(
(channel) => channel.name === "stats"
);
let messages = await statsChannel.messages.fetch({ limit: 1 });

// Create or edit existing embed
if (messages.size === 0) {
statsChannel.send({ embeds: [embed] });
} else {
messages.first().edit({ embeds: [embed] });
};
console.log('Server stats updated');
}
console.log("Server stats updated");
}

module.exports = { serverStats };
module.exports = { serverStats };
6 changes: 3 additions & 3 deletions src/utils/archive.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ async function archive(channel) {
channel.name.slice(0, 11).toLowerCase() === "📁archives_"
);
const latestCategory = archiveCategories
.filter((channel) => channel.children.cache.size < 3)
.filter((channel) => channel.children.cache.size < 50)
.sort(
(a, b) => parseInt(a.name.split("_")[1]) - parseInt(b.name.split("_")[1])
)
.last();

if (latestCategory) {
channel.setParent(latestCategory);
await channel.setParent(latestCategory);
} else {
// creating a new archive folder
const count = Math.max(
Expand All @@ -24,7 +24,7 @@ async function archive(channel) {
type: 4,
});
console.log(`Created new archive category ${newCategory.name}`);
channel.setParent(newCategory);
await channel.setParent(newCategory);
}
}

Expand Down

0 comments on commit 3ea9c3d

Please sign in to comment.