Add sync stdin command

This commit is contained in:
Isaac 2022-09-30 16:05:28 +01:00
parent 10ef6bb617
commit 8e0d9a6a5d
No known key found for this signature in database
GPG Key ID: 0DE40AE37BBA5C33
3 changed files with 81 additions and 59 deletions

63
src/lib/sync.js Normal file
View File

@ -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;
};

View File

@ -3,6 +3,7 @@ const md5 = require('md5');
const ms = require('ms'); const ms = require('ms');
const { version } = require('../../../package.json'); const { version } = require('../../../package.json');
const { msToMins } = require('../../lib/misc'); const { msToMins } = require('../../lib/misc');
const sync = require('../../lib/sync');
module.exports = class extends Listener { module.exports = class extends Listener {
constructor(client, options) { constructor(client, options) {
@ -22,63 +23,7 @@ module.exports = class extends Listener {
process.title = 'tickets'; process.title = 'tickets';
client.log.success('Connected to Discord as "%s"', client.user.tag); client.log.success('Connected to Discord as "%s"', client.user.tag);
// load total number of open tickets await sync(client);
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`);
// presence/activity // presence/activity
let next = 0; let next = 0;
@ -114,10 +59,10 @@ module.exports = class extends Listener {
next++; next++;
if (next === client.config.presence.activities.length) next = 0; if (next === client.config.presence.activities.length) next = 0;
}; };
setPresence(); setPresence();
if (client.config.presence.activities.length > 1) setInterval(() => setPresence(), client.config.presence.interval * 1000); if (client.config.presence.activities.length > 1) setInterval(() => setPresence(), client.config.presence.interval * 1000);
// stats posting
if (client.config.stats) { if (client.config.stats) {
const send = async () => { const send = async () => {
const tickets = await client.prisma.ticket.findMany({ 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); client.log.error('An error occurred whilst posting stats', stats, error);
} }
}; };
send(); send();
setInterval(() => send(), ms('12h')); setInterval(() => send(), ms('12h'));
} }

15
src/stdin/sync.js Normal file
View File

@ -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);
}
};