Skip to content

Commit

Permalink
feat: add the new private-chat command (#14)
Browse files Browse the repository at this point in the history
* 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 <me+github@kentcdodds.com>
  • Loading branch information
2 people authored and vcarl committed Dec 28, 2020
1 parent a5c2e29 commit 7ac92ea
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 0 deletions.
110 changes: 110 additions & 0 deletions src/features/private-chat/cleanup.js
Original file line number Diff line number Diff line change
@@ -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}
13 changes: 13 additions & 0 deletions src/features/private-chat/index.js
Original file line number Diff line number Diff line change
@@ -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}

0 comments on commit 7ac92ea

Please sign in to comment.