Add presence & placeholders (closes #302)

This commit is contained in:
Isaac 2022-08-06 21:07:40 +01:00
parent 052c159157
commit 3318ddcaa6
No known key found for this signature in database
GPG Key ID: F4EAABEB0FFCC06A
2 changed files with 50 additions and 1 deletions

View File

@ -1,4 +1,5 @@
const { Listener } = require('@eartharoid/dbf'); const { Listener } = require('@eartharoid/dbf');
const ms = require('ms');
module.exports = class extends Listener { module.exports = class extends Listener {
constructor(client, options) { 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 = `"[Discord Tickets] ${this.client.user.tag}"`; // too long and gets cut off
process.title = 'tickets'; process.title = 'tickets';
this.client.log.success('Connected to Discord as "%s"', this.client.user.tag); 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);
} }
}; };

View File

@ -19,6 +19,16 @@ logs:
directory: ./logs directory: ./logs
enabled: true enabled: true
keepFor: 30 # days 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: overrides:
disableArchives: false disableArchives: false