From 7ac92eaeaa9d4e7c09d7945dc21f0c64abeecc8d Mon Sep 17 00:00:00 2001 From: Marco Moretti Date: Fri, 2 Oct 2020 01:36:33 +0200 Subject: [PATCH] feat: add the new private-chat command (#14) * feat: add the new private-chat command the new command is available using ?private-chat * fix: should show both warnings * fix: review * fix: remove Set because can cause problems with server restart * Update src/commands/command-fns/private-chat.js Co-authored-by: Kent C. Dodds --- src/features/private-chat/cleanup.js | 110 +++++++++++++++++++++++++++ src/features/private-chat/index.js | 13 ++++ 2 files changed, 123 insertions(+) create mode 100644 src/features/private-chat/cleanup.js create mode 100644 src/features/private-chat/index.js diff --git a/src/features/private-chat/cleanup.js b/src/features/private-chat/cleanup.js new file mode 100644 index 00000000..7572f0d1 --- /dev/null +++ b/src/features/private-chat/cleanup.js @@ -0,0 +1,110 @@ +const {getSend, sleep} = require('../utils') + +async function cleanup(guild) { + const categoryPrivateChat = guild.channels.cache.find( + ({name, type}) => + type === 'category' && name.toLowerCase().includes('private chat'), + ) + + const warningStep = 1000 * 60 * 5 + const maxExistingTime = 1000 * 60 * 60 + const maxInactiveTime = 1000 * 60 * 10 + const forceDelayTime = 1000 * 60 * 2 + const eolReason = 'deleted for end of life πŸ‘»' + const inactivityReason = 'deleted for inactivity πŸšΆβ€β™€οΈ' + + const allActivePrivateChannels = guild.channels.cache.filter( + channel => + channel.type === 'text' && + channel.parentID === categoryPrivateChat.id && + channel.name.includes('-private-') && + !channel.deleted, + ) + + allActivePrivateChannels.forEach(async channel => { + const channelCreateDate = channel.createdAt + const lastMessageDate = channel.lastMessage?.createdAt ?? channelCreateDate + const timeSinceChannelCreation = new Date() - channelCreateDate + const timeSinceLastMessage = new Date() - lastMessageDate + const messages = Array.from((await channel.messages.fetch()).values()) + const hasInactiveWarned = messages.some(message => { + return ( + message.content.includes('5 minutes') && + message.content.includes(inactivityReason) + ) + }) + const hasEOLWarned = messages.some( + message => + message.content.includes('5 minutes') && + message.content.includes(eolReason), + ) + const isGettingDeleted = messages.some(message => + message.content.includes( + 'This channel is getting deleted for the following reason', + ), + ) + const send = getSend(channel) + if ( + timeSinceChannelCreation > maxExistingTime || + timeSinceLastMessage > maxInactiveTime + ) { + let reason + if (timeSinceChannelCreation > maxExistingTime) { + reason = eolReason + } else { + reason = inactivityReason + } + if (!isGettingDeleted) { + await send( + ` + This channel is getting deleted for the following reason: ${reason} + + Goodbye πŸ‘‹ + `.trim(), + ) + // Give just a while for the users to understand that the channel will be deleted soon + await sleep(10000) + await channel.delete(reason) + } + // After two minute from deletion we try to delate the channel again + // Maybe the server was stopped and the previous sleep was not finished + if ( + timeSinceChannelCreation - maxExistingTime > forceDelayTime || + timeSinceLastMessage - maxInactiveTime > forceDelayTime + ) { + await channel.delete(reason) + } + + return + } + + if ( + (timeSinceChannelCreation > maxExistingTime - warningStep || + timeSinceLastMessage > maxInactiveTime - warningStep) && + !hasInactiveWarned && + !hasEOLWarned + ) { + let reason + if ( + timeSinceChannelCreation > maxExistingTime - warningStep && + !hasEOLWarned + ) { + reason = eolReason + } else if ( + timeSinceLastMessage > maxInactiveTime - warningStep && + !hasInactiveWarned + ) { + reason = inactivityReason + } else { + return + } + await send( + ` +This channel will be deleted in 5 minutes for the following reason: ${reason} + `.trim(), + ) + } + }) +} + +module.exports = {cleanup} diff --git a/src/features/private-chat/index.js b/src/features/private-chat/index.js new file mode 100644 index 00000000..210970c3 --- /dev/null +++ b/src/features/private-chat/index.js @@ -0,0 +1,13 @@ +const privateChat = { + ...require('./cleanup'), +} + +function setup(client) { + setInterval(() => { + client.guilds.cache.forEach(guild => { + privateChat.cleanup(guild) + }) + }, 5000) +} + +module.exports = {...privateChat, setup}