From 3318ddcaa690d013adcec7473c95ab4661aba964 Mon Sep 17 00:00:00 2001 From: Isaac Date: Sat, 6 Aug 2022 21:07:40 +0100 Subject: [PATCH] Add presence & placeholders (closes #302) --- src/listeners/client/ready.js | 39 +++++++++++++++++++++++++++++++++++ user/example.config.yml | 12 ++++++++++- 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/src/listeners/client/ready.js b/src/listeners/client/ready.js index 26a436f..092bfb5 100644 --- a/src/listeners/client/ready.js +++ b/src/listeners/client/ready.js @@ -1,4 +1,5 @@ const { Listener } = require('@eartharoid/dbf'); +const ms = require('ms'); module.exports = class extends Listener { constructor(client, options) { @@ -14,5 +15,43 @@ module.exports = class extends Listener { // process.title = `"[Discord Tickets] ${this.client.user.tag}"`; // too long and gets cut off process.title = 'tickets'; this.client.log.success('Connected to Discord as "%s"', this.client.user.tag); + + let next = 0; + const setPresence = async () => { + const cacheKey = 'cache/presence'; + let cached = await this.client.keyv.get(cacheKey); + + if (!cached) { + const tickets = await this.client.prisma.ticket.findMany({ + select: { + createdAt: true, + firstResponseAt: true, + }, + }); + cached = { + avgResolutionTime: ms(tickets.reduce((total, ticket) => total + (ticket.closedAt - ticket.createdAt), 0) ?? 1 / tickets.length), + avgResponseTime: ms(tickets.reduce((total, ticket) => total + (ticket.firstResponseAt - ticket.createdAt), 0) ?? 1 / tickets.length), + openTickets: tickets.filter(t => t.open).length, + totalTickets: tickets.length, + }; + await this.client.keyv.set(cacheKey, cached, ms('15m')); + } + const activity = this.client.config.presence.activities[next]; + activity.name = activity.name + .replace(/{+avgResolutionTime}+/gi, cached.avgResolutionTime) + .replace(/{+avgResponseTime}+/gi, cached.avgResponseTime) + .replace(/{+openTickets}+/gi, cached.openTickets) + .replace(/{+totalTickets}+/gi, cached.totalTickets); + this.client.user.setPresence({ + activities: [activity], + status: this.client.config.presence.status, + }); + next++; + if (next === this.client.config.presence.activities.length) next = 0; + + }; + + setPresence(); + if (this.client.config.presence.activities.length > 1) setInterval(() => setPresence(), this.client.config.presence.interval * 1000); } }; diff --git a/user/example.config.yml b/user/example.config.yml index 9a5da22..dd83e36 100644 --- a/user/example.config.yml +++ b/user/example.config.yml @@ -19,6 +19,16 @@ logs: directory: ./logs enabled: true keepFor: 30 # days - level: info + level: info # debug|verbose|info|success|warn|notice|error|critical +presence: + activities: + - name: /new + - name: with {totalTickets} tickets + - name: '{openTickets} tickets' + type: 3 # https://discord-api-types.dev/api/discord-api-types-v10/enum/ActivityType + - name: '{avgResponseTime} response time' + type: 3 + interval: 60 # seconds, only used if activities.length > 1 + status: online # online|idle|invisible|dnd overrides: disableArchives: false \ No newline at end of file