-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: delete connections after 24h of inactivity (#70)
* feat: delete connections after 24h of inactivity * fix: JS-0327 and JS-0116 * final touchups and testing done * rename files * chore: fix condition for running tasks in production build * hi * chore: update recurring task interval for syncing botlist stats * feat: disconnect idle connections after 24 hours
- Loading branch information
Showing
14 changed files
with
175 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import db from '../../utils/Db.js'; | ||
import Logger from '../../utils/Logger.js'; | ||
import { ClusterManager } from 'discord-hybrid-sharding'; | ||
import { modifyConnection } from '../../utils/ConnectedList.js'; | ||
import { APIActionRowComponent, APIButtonComponent, Snowflake } from 'discord.js'; | ||
import { buildConnectionButtons } from '../network/components.js'; | ||
import { simpleEmbed } from '../../utils/Utils.js'; | ||
import { stripIndents } from 'common-tags'; | ||
import { emojis } from '../../utils/Constants.js'; | ||
import 'dotenv/config'; | ||
|
||
export default async (manager: ClusterManager) => { | ||
const connections = await db.connectedList.findMany({ | ||
where: { | ||
connected: true, | ||
lastActive: { not: null, lte: new Date(Date.now() - 24 * 60 * 60 * 1000) }, | ||
}, | ||
}); | ||
|
||
if (!connections) return; | ||
|
||
const reconnectButtonArr: { | ||
channelId: Snowflake; | ||
button: APIActionRowComponent<APIButtonComponent>; | ||
}[] = []; | ||
|
||
// Loop through the data | ||
connections.forEach(async ({ channelId, lastActive }) => { | ||
Logger.debug( | ||
`[InterChat]: Channel ${channelId} is older than 24 hours: ${lastActive?.toLocaleString()} - ${new Date().toLocaleString()}`, | ||
); | ||
|
||
// Create the button | ||
reconnectButtonArr.push({ | ||
channelId, | ||
button: buildConnectionButtons(false, channelId, { | ||
customCustomId: 'inactiveConnect', | ||
}).toJSON(), | ||
}); | ||
|
||
// disconnect the channel | ||
await modifyConnection({ channelId }, { lastActive: null, connected: false }); | ||
}); | ||
|
||
const embed = simpleEmbed( | ||
stripIndents` | ||
## ${emojis.timeout} Paused Due to Inactivity | ||
Connection to this hub has been stopped. **Click the button** below to resume chatting (or alternatively, \`/connection\`). | ||
`, | ||
).toJSON(); | ||
|
||
await manager.broadcastEval( | ||
(client, { _connections, _embed, buttons }) => { | ||
_connections.forEach(async (connection) => { | ||
const channel = await client.channels.fetch(connection.channelId).catch(() => null); | ||
const button = buttons.find((b) => b.channelId === connection.channelId)?.button; | ||
|
||
if (!channel?.isTextBased() || !button) return; | ||
|
||
// remove it since we are done with it | ||
_connections.splice(_connections.indexOf(connection), 1); | ||
|
||
await channel.send({ embeds: [_embed], components: [button] }).catch(() => null); | ||
}); | ||
}, | ||
{ context: { _connections: connections, _embed: embed, buttons: reconnectButtonArr } }, | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.