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-10-30 22:33:21 -04:00
|
|
|
import { TextableChannel } from 'eris';
|
|
|
|
import { Client, RichEmbed } from '.';
|
2020-10-29 01:36:05 -04:00
|
|
|
import { ScoreInterface } 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-10-30 22:47:55 -04:00
|
|
|
score: new Bull('score'),
|
2020-10-29 01:36:05 -04:00
|
|
|
};
|
|
|
|
this.setProcessors();
|
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() {
|
|
|
|
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-30 22:33:21 -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 }>) => {
|
|
|
|
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);
|
|
|
|
await message.delete();
|
|
|
|
const embed = new RichEmbed();
|
|
|
|
embed.setTitle('Application Decision');
|
|
|
|
embed.addField('Status', application.decision, true);
|
|
|
|
embed.addField('UserID', job.data.userID, true);
|
|
|
|
embed.addField('Job ID', job.id.toString(), true);
|
|
|
|
embed.setFooter(this.client.user.username, this.client.user.avatarURL);
|
|
|
|
embed.setTimestamp();
|
|
|
|
await channel.createMessage({ content: `<@${job.data.userID}>`, embed });
|
|
|
|
const func = eval(job.data.func);
|
|
|
|
if (application.status === 'SUCCESS' && application.decision === 'APPROVED') await func(this.client, job.data.userID);
|
|
|
|
});
|
2020-10-28 19:44:31 -04:00
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
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
|
|
|
|
|
|
|
public processApplication(channelInformation: { messageID: string, guildID: string, channelID: string }, url: string, userID: string, func: string) {
|
|
|
|
return this.queues.score.add('score::apply', { channelInformation, url, userID, func });
|
|
|
|
}
|
2020-10-28 19:44:31 -04:00
|
|
|
}
|