From 9f0470b7b1ea64bff9b86338c4219198c112b969 Mon Sep 17 00:00:00 2001 From: versx Date: Fri, 7 May 2021 19:10:57 -0700 Subject: [PATCH] Fix delete expired event channels --- package.json | 2 +- src/index.ts | 77 +++++++++++++++++++++++++++++++--------------------- 2 files changed, 47 insertions(+), 32 deletions(-) diff --git a/package.json b/package.json index 72a0c19..426ce4d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "eventwatcher", - "version": "2.0.2", + "version": "2.0.4", "description": "", "main": "dist/index.js", "scripts": { diff --git a/src/index.ts b/src/index.ts index c5dcca0..65191e5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,7 +2,7 @@ import { readdirSync } from 'fs'; import { resolve } from 'path'; -import { Client, Collection, Guild, GuildChannel, OverwriteResolvable, TextChannel } from 'discord.js'; +import { CategoryChannel, Client, Collection, Guild, GuildChannel, OverwriteResolvable, TextChannel } from 'discord.js'; import { render } from 'mustache'; const config = require('../src/config.json'); @@ -13,13 +13,13 @@ import { PokemonEvents } from './models/events'; import { UrlWatcher } from './services/url-watcher'; import { post, getWebhookData, } from './services/utils'; import { Dictionary } from './types/dictionary'; +import { ActiveEvent } from './types/events'; const client = new Client(); //const urlToWatch = 'https://raw.githubusercontent.com/ccev/pogoinfo/info/events/active.json'; const urlToWatch = 'https://raw.githubusercontent.com/ccev/pogoinfo/v2/active/events.json'; const intervalM = 1 * 60 * 1000; const NotAvailable = 'N/A'; -const existingEventChannels: Dictionary = {}; let started = false; // TODO: Show time when event expires that day @@ -38,7 +38,13 @@ if (config.token) { client.on('ready', async () => { console.log(`Logged in as ${client.user?.tag}!`); // Once the bot is ready we can start updating the voice channels - await startActiveEventsUpdater(); + // Prevent multiple + if (started) return; + + setInterval(async () => { + started = true; + await createChannels(); + }, intervalM); // Create channels as soon as Discord guild is available await createChannels(); }); @@ -55,16 +61,6 @@ const createChannels = async (): Promise => { } }; -const startActiveEventsUpdater = async (): Promise => { - // Prevent multiple - if (started) return; - - setInterval(async () => { - started = true; - await createChannels(); - }, intervalM); -}; - const createVoiceChannels = async (guildInfo: any, activeEvents: any): Promise => { // Check if event category id set for guild if (!guildInfo.eventsCategoryId) { @@ -96,30 +92,17 @@ const createVoiceChannels = async (guildInfo: any, activeEvents: any): Promise => console.error(`Failed to find expired event channel ${channelId} to delete.`); return; } - await channel.delete(); + try { + await channel.delete(); + } catch (e) { + console.error(`Failed to delete channel ${channel.id}: ${e}`); + } +}; + +const deleteExpiredEvents = (guildChannel: GuildChannel, activeEvents: any): void => { + // Check if channels in category exists in active events, if so, keep it, otherwise delete it. + const channels = (((guildChannel)).children).array(); + const activeEventNames = activeEvents.map((x: ActiveEvent) => formatEventName(x)); + for (const channel of channels) { + if (!channel) { + continue; + } + // Check if channel does not exist in formatted active event names + if (!activeEventNames.includes(channel?.name)) { + // Delete channel if it's not an active event + deleteChannel(guildChannel.guild, channel?.id); + } + } +}; + +const formatEventName = (event: ActiveEvent): string => { + // Format event ends date + const eventEndDate = event.end ? new Date(event.end) : NotAvailable; + // Get channel name from event name and ends date + const channelName = render(config.channelNameFormat, { + month: eventEndDate !== NotAvailable ? eventEndDate.getMonth() + 1 : NotAvailable, + day: eventEndDate !== NotAvailable ? eventEndDate.getDate() : '', + name: event.name, + }); + return channelName; }; UrlWatcher(urlToWatch, intervalM, async (): Promise => {