community-relations/src/class/Queue.ts

171 lines
8.2 KiB
TypeScript
Raw Normal View History

2021-02-02 01:45:03 -05:00
/* eslint-disable no-await-in-loop */
2020-10-30 22:33:21 -04:00
/* eslint-disable no-eval */
2020-10-29 01:36:05 -04:00
import Bull from 'bull';
2020-11-01 19:46:28 -05:00
import cron from 'cron';
2021-02-02 01:45:03 -05:00
import { TextableChannel, TextChannel } from 'eris';
2020-10-30 22:33:21 -04:00
import { Client, RichEmbed } from '.';
2021-02-02 01:45:03 -05:00
import { ScoreInterface, InqType as InquiryType } from '../models';
2020-10-28 19:44:31 -04:00
2020-10-30 22:33:21 -04:00
import { apply as Apply } from '../commands';
2020-10-29 01:36:05 -04:00
export default class Queue {
2020-10-28 19:44:31 -04:00
public client: Client;
2020-10-29 01:36:05 -04:00
public queues: { score: Bull.Queue };
2020-10-28 19:44:31 -04:00
2020-10-29 01:36:05 -04:00
constructor(client: Client) {
2020-10-28 19:44:31 -04:00
this.client = client;
2020-10-29 01:36:05 -04:00
this.queues = {
2020-11-28 02:02:29 -05:00
score: new Bull('score', { prefix: 'queue::score' }),
2020-10-29 01:36:05 -04:00
};
this.setProcessors();
2020-11-01 19:48:55 -05:00
this.setCronJobs();
2020-11-01 19:46:28 -05:00
}
protected setCronJobs() {
const historialCommunityReportJob = new cron.CronJob('0 20 * * *', async () => {
try {
const reports = await this.client.db.Score.find().lean().exec();
const startDate = new Date();
for (const report of reports) {
2021-02-02 01:45:03 -05:00
const inqs = await this.client.db.Inquiry.find({ userID: report.userID });
2020-11-01 19:46:28 -05:00
const data = new this.client.db.ScoreHistorical({
userID: report.userID,
report,
2021-02-02 01:45:03 -05:00
inquiries: inqs.map((inq) => inq._id),
2020-11-01 19:46:28 -05:00
date: startDate,
});
await data.save();
}
} catch (err) {
this.client.util.handleError(err);
}
});
historialCommunityReportJob.start();
2020-10-28 19:44:31 -04:00
}
2020-10-29 01:45:01 -04:00
public async jobCounts() {
const data = {
waiting: 0,
active: 0,
completed: 0,
failed: 0,
delayed: 0,
};
for (const entry of Object.entries(this.queues)) {
// eslint-disable-next-line no-await-in-loop
const counts = await entry[1].getJobCounts();
data.waiting += counts.waiting;
data.active += counts.active;
data.completed += counts.completed;
data.failed += counts.failed;
data.delayed += counts.delayed;
}
return data;
}
2020-10-29 01:36:05 -04:00
protected listeners() {
this.queues.score.on('active', (job) => {
this.client.util.signale.pending(`${job.id} has become active.`);
2020-10-28 19:44:31 -04:00
});
2020-10-29 01:36:05 -04:00
this.queues.score.on('completed', (job) => {
this.client.util.signale.success(`Job with id ${job.id} has been completed`);
2020-10-28 19:44:31 -04:00
});
2020-10-30 22:33:21 -04:00
this.queues.score.on('error', async (err) => {
this.client.util.handleError(err);
});
2020-10-28 19:44:31 -04:00
}
2020-10-29 01:36:05 -04:00
protected setProcessors() {
2021-02-02 01:45:03 -05:00
this.queues.score.process('score::inquiry', async (job: Bull.Job<{ inqID: string, userID: string, name: string, type: InquiryType, reason?: string }>) => {
const member = this.client.util.resolveMember(job.data.userID, this.client.guilds.get(this.client.config.guildID));
const report = await this.client.db.Score.findOne({ userID: job.data.userID }).lean().exec();
const embed = new RichEmbed();
if (job.data.type === InquiryType.HARD) {
embed.setTitle('Inquiry Notification');
embed.setDescription(job.data.inqID);
embed.setColor('#800080');
embed.addField('Member', `${member.user.username}#${member.user.discriminator} | <@${job.data.userID}>`, true);
embed.addField('Type', 'HARD', true);
embed.addField('Department/Service', job.data.name.toUpperCase(), true);
embed.addField('Reason', job.data.reason ?? 'N/A', true);
embed.setTimestamp();
embed.setFooter(this.client.user.username, this.client.user.avatarURL);
if (report.notify === true) {
await this.client.getDMChannel(job.data.userID).then((chan) => {
chan.createMessage(`__**Community Score - Hard Pull Notification**__\n*You have signed up to be notified whenever your hard score has been pulled. See \`?score\` for more information.*\n\n**Department/Service:** ${job.data.name.toUpperCase()}`);
}).catch(() => {});
}
} else {
embed.setTitle('Inquiry Notification');
embed.setColor('#00FFFF');
embed.addField('Member', `${member.user.username}#${member.user.discriminator} | <@${job.data.userID}>`, true);
embed.addField('Type', 'SOFT', true);
embed.addField('Department/Service', job.data.name.toUpperCase(), true);
embed.setTimestamp();
embed.setFooter(this.client.user.username, this.client.user.avatarURL);
}
const log = <TextChannel> this.client.guilds.get(this.client.config.guildID).channels.get('611584771356622849');
log.createMessage({ embed }).catch(() => {});
});
2020-10-29 01:36:05 -04:00
this.queues.score.process('score::update', async (job: Bull.Job<{ score: ScoreInterface, total: number, activity: number, roles: number, moderation: number, cloudServices: number, other: number, staff: number }>) => {
await this.client.db.Score.updateOne({ userID: job.data.score.userID }, { $set: { total: job.data.total, activity: job.data.activity, roles: job.data.roles, moderation: job.data.moderation, cloudServices: job.data.cloudServices, other: job.data.other, staff: job.data.staff, lastUpdate: new Date() } });
if (!job.data.score.pin || job.data.score.pin?.length < 1) {
await this.client.db.Score.updateOne({ userID: job.data.score.userID }, { $set: { pin: [this.client.util.randomNumber(100, 999), this.client.util.randomNumber(10, 99), this.client.util.randomNumber(1000, 9999)] } });
}
});
2020-10-31 02:27:41 -04:00
this.queues.score.process('score::apply', async (job: Bull.Job<{ channelInformation: { messageID: string, guildID: string, channelID: string }, url: string, userID: string, func?: string }>) => {
2020-10-30 22:33:21 -04:00
const application = await Apply.apply(this.client, job.data.url, job.data.userID);
const guild = this.client.guilds.get(job.data.channelInformation.guildID);
const channel = <TextableChannel> guild.channels.get(job.data.channelInformation.channelID);
const message = await channel.getMessage(job.data.channelInformation.messageID);
2020-11-08 22:24:49 -05:00
const member = guild.members.get(job.data.userID);
2020-10-30 22:33:21 -04:00
await message.delete();
const embed = new RichEmbed();
embed.setTitle('Application Decision');
2020-11-08 22:24:49 -05:00
if (member) {
embed.setAuthor(member.username, member.avatarURL);
}
if (application.decision === 'APPROVED') {
embed.setColor('#50c878');
} else if (application.decision === 'DECLINED') {
embed.setColor('#fe0000');
} else if (application.decision === 'PRE-DECLINE') {
embed.setColor('#ffa500');
} else {
embed.setColor('#eeeeee');
}
2021-01-12 02:14:22 -05:00
const chan = await this.client.getDMChannel(job.data.userID);
if (chan && application.token) {
chan.createMessage(`__**Application Decision Document**__\n*This document provides detailed information about the decision given to you by EDS.*\n\nhttps://eds.libraryofcode.org/dec/${application.token}`);
}
2020-10-31 23:46:49 -04:00
embed.setDescription(`This application was processed by __${application.processedBy}__ on behalf of the vendor, department, or service who operates this application. Please contact the vendor for further information about your application if needed.`);
2020-10-30 22:33:21 -04:00
embed.addField('Status', application.decision, true);
2020-10-31 00:15:47 -04:00
embed.addField('User ID', job.data.userID, true);
2020-10-31 23:46:49 -04:00
embed.addField('Application ID', application.id, true);
2020-10-30 22:33:21 -04:00
embed.addField('Job ID', job.id.toString(), true);
2020-11-08 22:24:49 -05:00
embed.setFooter(`${this.client.user.username} via Electronic Decision Service [EDS]`, this.client.user.avatarURL);
2020-10-30 22:33:21 -04:00
embed.setTimestamp();
await channel.createMessage({ content: `<@${job.data.userID}>`, embed });
2020-10-31 02:27:41 -04:00
if (job.data.func) {
const func = eval(job.data.func);
if (application.status === 'SUCCESS' && application.decision === 'APPROVED') await func(this.client, job.data.userID);
}
2020-10-30 22:33:21 -04:00
});
2020-10-28 19:44:31 -04:00
}
2021-02-02 01:45:03 -05:00
public addInquiry(inqID: string, userID: string, name: string, type: InquiryType, reason?: string) {
return this.queues.score.add('score::inquiry', { inqID, userID, name, type, reason });
}
2020-10-29 01:36:05 -04:00
public updateScore(score: ScoreInterface, total: number, activity: number, roles: number, moderation: number, cloudServices: number, other: number, staff: number) {
2021-02-02 01:45:03 -05:00
return this.queues.score.add('score::update', { score, total, activity, roles, moderation, cloudServices, other, staff });
2020-10-28 19:44:31 -04:00
}
2020-10-30 22:33:21 -04:00
2020-10-31 02:27:41 -04:00
public processApplication(channelInformation: { messageID: string, guildID: string, channelID: string }, url: string, userID: string, func?: string) {
2020-10-30 22:33:21 -04:00
return this.queues.score.add('score::apply', { channelInformation, url, userID, func });
}
2020-10-28 19:44:31 -04:00
}