From 8e0d9a6a5dc0d32deadf6c72c602e30ce5c14bfc Mon Sep 17 00:00:00 2001 From: Isaac Date: Fri, 30 Sep 2022 16:05:28 +0100 Subject: [PATCH] Add `sync` stdin command --- src/lib/sync.js | 63 +++++++++++++++++++++++++++++++++++ src/listeners/client/ready.js | 62 ++-------------------------------- src/stdin/sync.js | 15 +++++++++ 3 files changed, 81 insertions(+), 59 deletions(-) create mode 100644 src/lib/sync.js create mode 100644 src/stdin/sync.js diff --git a/src/lib/sync.js b/src/lib/sync.js new file mode 100644 index 0000000..ff30a66 --- /dev/null +++ b/src/lib/sync.js @@ -0,0 +1,63 @@ +/** + * @param {import("client")} client + */ +module.exports = async client => { + // load total number of open tickets + const categories = await client.prisma.category.findMany({ + select: { + cooldown: true, + id: true, + tickets: { + select: { + createdById: true, + guildId: true, + id: true, + }, + where: { open: true }, + }, + }, + }); + let deleted = 0; + let ticketCount = 0; + let cooldowns = 0; + for (const category of categories) { + ticketCount += category.tickets.length; + client.tickets.$.categories[category.id] = { total: category.tickets.length }; + for (const ticket of category.tickets) { + if (client.tickets.$.categories[category.id][ticket.createdById]) client.tickets.$.categories[category.id][ticket.createdById]++; + else client.tickets.$.categories[category.id][ticket.createdById] = 1; + /** @type {import("discord.js").Guild} */ + const guild = client.guilds.cache.get(ticket.guildId); + if (guild && guild.available && !client.channels.cache.has(ticket.id)) { + deleted += 0; + await client.tickets.close(ticket.id); + } + + } + if (category.cooldown) { + const recent = await client.prisma.ticket.findMany({ + orderBy: { createdAt: 'asc' }, + select: { + createdAt: true, + createdById: true, + }, + where: { + categoryId: category.id, + createdAt: { gt: new Date(Date.now() - category.cooldown) }, + }, + }); + cooldowns += recent.length; + for (const ticket of recent) { + const cacheKey = `cooldowns/category-member:${category.id}-${ticket.createdById}`; + const expiresAt = ticket.createdAt.getTime() + category.cooldown; + const TTL = expiresAt - Date.now(); + await client.keyv.set(cacheKey, expiresAt, TTL); + } + } + } + // const ticketCount = categories.reduce((total, category) => total + category.tickets.length, 0); + client.log.info(`Cached ticket count of ${categories.length} categories (${ticketCount} open tickets)`); + client.log.info(`Loaded ${cooldowns} active cooldowns`); + client.log.info(`Closed ${deleted} deleted tickets`); + return true; +}; \ No newline at end of file diff --git a/src/listeners/client/ready.js b/src/listeners/client/ready.js index ca75621..eef6218 100644 --- a/src/listeners/client/ready.js +++ b/src/listeners/client/ready.js @@ -3,6 +3,7 @@ const md5 = require('md5'); const ms = require('ms'); const { version } = require('../../../package.json'); const { msToMins } = require('../../lib/misc'); +const sync = require('../../lib/sync'); module.exports = class extends Listener { constructor(client, options) { @@ -22,63 +23,7 @@ module.exports = class extends Listener { process.title = 'tickets'; client.log.success('Connected to Discord as "%s"', client.user.tag); - // load total number of open tickets - const categories = await client.prisma.category.findMany({ - select: { - cooldown: true, - id: true, - tickets: { - select: { - createdById: true, - guildId: true, - id: true, - }, - where: { open: true }, - }, - }, - }); - let deleted = 0; - let ticketCount = 0; - let cooldowns = 0; - for (const category of categories) { - ticketCount += category.tickets.length; - client.tickets.$.categories[category.id] = { total: category.tickets.length }; - for (const ticket of category.tickets) { - if (client.tickets.$.categories[category.id][ticket.createdById]) client.tickets.$.categories[category.id][ticket.createdById]++; - else client.tickets.$.categories[category.id][ticket.createdById] = 1; - /** @type {import("discord.js").Guild} */ - const guild = client.guilds.cache.get(ticket.guildId); - if (guild && guild.available && !client.channels.cache.has(ticket.id)) { - deleted += 0; - await client.tickets.close(ticket.id); - } - - } - if (category.cooldown) { - const recent = await client.prisma.ticket.findMany({ - orderBy: { createdAt: 'asc' }, - select: { - createdAt: true, - createdById: true, - }, - where: { - categoryId: category.id, - createdAt: { gt: new Date(Date.now() - category.cooldown) }, - }, - }); - cooldowns += recent.length; - for (const ticket of recent) { - const cacheKey = `cooldowns/category-member:${category.id}-${ticket.createdById}`; - const expiresAt = ticket.createdAt.getTime() + category.cooldown; - const TTL = expiresAt - Date.now(); - await client.keyv.set(cacheKey, expiresAt, TTL); - } - } - } - // const ticketCount = categories.reduce((total, category) => total + category.tickets.length, 0); - client.log.info(`Cached ticket count of ${categories.length} categories (${ticketCount} open tickets)`); - client.log.info(`Loaded ${cooldowns} active cooldowns`); - client.log.info(`Closed ${deleted} deleted tickets`); + await sync(client); // presence/activity let next = 0; @@ -114,10 +59,10 @@ module.exports = class extends Listener { next++; if (next === client.config.presence.activities.length) next = 0; }; - setPresence(); if (client.config.presence.activities.length > 1) setInterval(() => setPresence(), client.config.presence.interval * 1000); + // stats posting if (client.config.stats) { const send = async () => { const tickets = await client.prisma.ticket.findMany({ @@ -159,7 +104,6 @@ module.exports = class extends Listener { client.log.error('An error occurred whilst posting stats', stats, error); } }; - send(); setInterval(() => send(), ms('12h')); } diff --git a/src/stdin/sync.js b/src/stdin/sync.js new file mode 100644 index 0000000..b022b93 --- /dev/null +++ b/src/stdin/sync.js @@ -0,0 +1,15 @@ +const { StdinCommand } = require('@eartharoid/dbf'); +const sync = require('../lib/sync'); + +module.exports = class extends StdinCommand { + constructor(client, options) { + super(client, { + ...options, + id: 'sync', + }); + } + + async run() { + await sync(this.client); + } +}; \ No newline at end of file