Skip to content

Commit

Permalink
improve archive and channel cleanup function
Browse files Browse the repository at this point in the history
  • Loading branch information
BodomBeach committed May 16, 2024
1 parent 461e235 commit abcaad2
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 47 deletions.
12 changes: 5 additions & 7 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -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/[email protected]
with:
host: ${{ secrets.HOST }}
Expand All @@ -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
5 changes: 2 additions & 3 deletions src/events/commands/archive.js
Original file line number Diff line number Diff line change
@@ -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 = () => {
Expand All @@ -15,15 +15,14 @@ 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 !',
ephemeral: true,
});
} else {
archive(interaction.channel);
await interaction.reply({ content: 'Salon archivé !', ephemeral: true });
};
};

Expand Down
67 changes: 32 additions & 35 deletions src/jobs/channelCleanup.js
Original file line number Diff line number Diff line change
@@ -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`);
}
});
});
}
4 changes: 2 additions & 2 deletions src/utils/archive.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down

0 comments on commit abcaad2

Please sign in to comment.