From abcaad29b50139110bfa4aa607f2690604d124b2 Mon Sep 17 00:00:00 2001 From: bodombeach Date: Thu, 16 May 2024 17:33:54 +0200 Subject: [PATCH] improve archive and channel cleanup function --- .github/workflows/deploy.yml | 12 +++--- src/events/commands/archive.js | 5 +-- src/jobs/channelCleanup.js | 67 ++++++++++++++++------------------ src/utils/archive.js | 4 +- 4 files changed, 41 insertions(+), 47 deletions(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index ce28a4f..41d1bab 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -1,15 +1,17 @@ name: Deploy to Production - on: push: branches: - master + pull_request: + branches: + - master jobs: deploy: name: Deploy runs-on: ubuntu-latest steps: - - name: executing remote ssh commands using ssh key + - name: deploy uses: appleboy/ssh-action@v1.0.3 with: host: ${{ secrets.HOST }} @@ -18,11 +20,7 @@ jobs: port: ${{ secrets.PORT }} script: | cd ~/bot-discord-duck - echo "Pulling code" git pull - echo "Pruning docker" docker system prune -f - echo "Restarting docker" docker compose down - docker compose -f compose.yaml -f compose.prod.yaml up --build - + docker compose -f compose.yaml -f compose.prod.yaml up --build -d diff --git a/src/events/commands/archive.js b/src/events/commands/archive.js index bb78309..8d9c1e8 100644 --- a/src/events/commands/archive.js +++ b/src/events/commands/archive.js @@ -1,7 +1,7 @@ // TODO -> Archive by transforming channel into a thread in the 'archives' channel import { SlashCommandBuilder } from 'discord.js'; -import archive from '../../utils/archive.js'; +import {archive, allowedCategories}from '../../utils/archive.js'; // Creates an Object in JSON with the data required by Discord's API to create a SlashCommand const create = () => { @@ -15,8 +15,6 @@ const create = () => { const invoke = async (interaction) => { console.log(`${interaction.user.username} used /archive`); - const allowedCategories = ['🪂 SORTIES', '🏃Sorties pas rapente', '🏆 Compétitions']; - if (!allowedCategories.includes(interaction.channel.parent.name)) { return interaction.reply({ content: 'Seuls les salons sorties, évenements et compétitions peuvent être archivés !', @@ -24,6 +22,7 @@ const invoke = async (interaction) => { }); } else { archive(interaction.channel); + await interaction.reply({ content: 'Salon archivé !', ephemeral: true }); }; }; diff --git a/src/jobs/channelCleanup.js b/src/jobs/channelCleanup.js index f01215c..7c4cbf1 100644 --- a/src/jobs/channelCleanup.js +++ b/src/jobs/channelCleanup.js @@ -1,38 +1,35 @@ -import archive from '../utils/archive.js'; +import {archive, allowedCategories} from '../utils/archive.js'; export function channelCleanup(client) { - const guild = client.guilds.cache.get(process.env.GUILD_ID); - const category = guild.channels.cache.find(channel => channel.type === 4 && channel.name === '🪂 SORTIES'); - - if (category) { - - 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 - - const today = new Date() - let channels = category.children.cache - channels.forEach(channel => { - - 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)) { - console.log(`format invalide ${channel.name}`); - return - } - - let channel_date = new Date(today.getFullYear(), (parseInt(match[2]) - 1), (parseInt(match[1]) + 1)); - - if (today > channel_date) { - console.log(`${channel.name} too old, archiving !`); - archive(channel) - } else { - console.log(`${channel.name} OK`); - } - }); - } else { - console.log('category not found'); - } + const guild = client.guilds.cache.get(process.env.GUILD_ID); + const categories = guild.channels.cache.filter(channel => allowedCategories.includes(channel.name)) + + 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 + + categories.forEach(category => { + const today = new Date() + let channels = category.children.cache + channels.forEach(channel => { + + 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)) { + console.log(`format invalide ${channel.name}`); + return + } + + let channel_date = new Date(today.getFullYear(), (parseInt(match[2]) - 1), (parseInt(match[1]) + 1)); + + if (today > channel_date) { + console.log(`${channel.name} too old, archiving !`); + archive(channel) + } else { + console.log(`${channel.name} OK`); + } + }); + }); } \ No newline at end of file diff --git a/src/utils/archive.js b/src/utils/archive.js index 554ee31..8fa4d97 100644 --- a/src/utils/archive.js +++ b/src/utils/archive.js @@ -1,8 +1,8 @@ // TODO -> Archive by transforming channel into a thread in the 'archives' channel +export const allowedCategories = ['🪂 SORTIES', '🏃Sorties pas rapente', '🏆 Compétitions']; -export default async function archive(channel) { +export async function archive(channel) { const archiveCategories = channel.guild.channels.cache.filter(channel => channel.type === 4 && channel.name.slice(0,11).toLowerCase() === '📁archives_'); - let available_archive = archiveCategories.find(cat => cat.children.cache.size < 3) if (available_archive) {