From 2e83cb7d55b2f1410eca9605ccada09816c0155a Mon Sep 17 00:00:00 2001 From: Matthew R Date: Wed, 28 Oct 2020 19:44:31 -0400 Subject: [PATCH] changes to queue system --- src/class/Client.ts | 7 +++++- src/class/Queue.ts | 56 ++++++++++++++++++++++++++++++++++++++++++ src/class/index.ts | 1 + src/commands/whois.ts | 6 +++++ src/intervals/score.ts | 12 ++++++++- src/main.ts | 2 +- 6 files changed, 81 insertions(+), 3 deletions(-) create mode 100644 src/class/Queue.ts diff --git a/src/class/Client.ts b/src/class/Client.ts index 6b031c9..b56d638 100644 --- a/src/class/Client.ts +++ b/src/class/Client.ts @@ -2,7 +2,7 @@ import eris from 'eris'; import pluris from 'pluris'; import mongoose from 'mongoose'; import { promises as fs } from 'fs'; -import { Collection, Command, LocalStorage, Util, ServerManagement, Event } from '.'; +import { Collection, Command, LocalStorage, Queue, Util, ServerManagement, Event } from '.'; import { File, FileInterface, Member, MemberInterface, Merchant, MerchantInterface, Moderation, ModerationInterface, NNTrainingData, NNTrainingDataInterface, Note, NoteInterface, PagerNumber, PagerNumberInterface, Rank, RankInterface, Redirect, RedirectInterface, Score, ScoreInterface, Staff, StaffInterface, Stat, StatInterface } from '../models'; import { Config } from '../../types'; // eslint-disable-line @@ -21,6 +21,8 @@ export default class Client extends eris.Client { public serverManagement: ServerManagement; + public queue: { main: Queue }; + public db: { File: mongoose.Model, Member: mongoose.Model, Merchant: mongoose.Model, Moderation: mongoose.Model, NNTrainingData: mongoose.Model, Note: mongoose.Model, PagerNumber: mongoose.Model, Rank: mongoose.Model, Redirect: mongoose.Model, Score: mongoose.Model, Staff: mongoose.Model, Stat: mongoose.Model, local: { muted: LocalStorage } }; constructor(token: string, options?: eris.ClientOptions) { @@ -28,6 +30,9 @@ export default class Client extends eris.Client { this.commands = new Collection(); this.events = new Collection(); this.intervals = new Collection(); + this.queue = { + main: new Queue(this, 'main'), + }; this.db = { File, Member, Merchant, Moderation, NNTrainingData, Note, PagerNumber, Rank, Redirect, Score, Staff, Stat, local: { muted: new LocalStorage('muted') } }; } diff --git a/src/class/Queue.ts b/src/class/Queue.ts new file mode 100644 index 0000000..240c2c6 --- /dev/null +++ b/src/class/Queue.ts @@ -0,0 +1,56 @@ +import { EventEmitter } from 'events'; +import { v4 as uuid } from 'uuid'; +import { Client, Collection } from '.'; + +export type QueueID = string; + +export interface QueueEntry { + readonly id?: QueueID, + readonly enteredDate?: Date, + readonly submittedBy: string, + func: Function, +} + +export default class Queue extends EventEmitter { + public queueName: string; + + public client: Client; + + protected set: Collection; + + constructor(client: Client, queueName: string) { + super(); + this.client = client; + this.queueName = queueName; + this.set = new Collection(); + } + + public postListeners() { + this.on('newEntry', (data: QueueEntry) => { + this.client.util.signale.info(`New Entry in Queue ${this.queueName}\nEntered Date: ${data.enteredDate} | Submitted by: ${data.submittedBy}`); + }); + this.on('processing', (data: QueueEntry) => { + this.client.util.signale.pending(`Processing Entry in Queue ${this.queueName}\nEntered Date: ${data.enteredDate} | Submitted by: ${data.submittedBy}`); + }); + this.on('done', (data: QueueEntry) => { + this.client.util.signale.success(`Finished Entry in Queue ${this.queueName}\nEntered Date: ${data.enteredDate} | Submitted by: ${data.submittedBy}`); + }); + } + + public add(entry: QueueEntry) { + const data = this.set.add(uuid(), { enteredDate: new Date(), submittedBy: entry.submittedBy, func: entry.func }); + this.emit('newEntry', data); + this.do(data); + return data; + } + + protected async do(entry: QueueEntry) { + try { + this.emit('processing', entry); + await entry.func(this.client); + } finally { + this.set.delete(entry.id); + this.emit('done', entry); + } + } +} diff --git a/src/class/index.ts b/src/class/index.ts index 3b55fea..222802e 100644 --- a/src/class/index.ts +++ b/src/class/index.ts @@ -4,6 +4,7 @@ export { default as Command } from './Command'; export { default as Event } from './Event'; export { default as LocalStorage } from './LocalStorage'; export { default as Moderation } from './Moderation'; +export { default as Queue } from './Queue'; export { default as RichEmbed } from './RichEmbed'; export { default as Route } from './Route'; export { default as Server } from './Server'; diff --git a/src/commands/whois.ts b/src/commands/whois.ts index a16eadc..eab6181 100644 --- a/src/commands/whois.ts +++ b/src/commands/whois.ts @@ -22,6 +22,11 @@ export default class Whois extends Command { if (!args[0]) member = message.member; else { member = this.client.util.resolveMember(args.join(' '), this.mainGuild); + try { + if (!member) member = await this.mainGuild.getRESTMember(args[0]); + } catch { + return this.error(message.channel, 'Member not found.'); + } } if (!member) { @@ -75,6 +80,7 @@ export default class Whois extends Command { embed.addField('Created At', `${moment(new Date(member.user.createdAt)).format('dddd, MMMM Do YYYY, h:mm:ss A')} ET`, true); const score = await this.client.db.Score.findOne({ userID: member.id }); if (score) { + await this.client.db.Score.updateOne({ userID: member.id }, { $addToSet: { softInquiries: { name: 'Library of Code sp-us | Bureau of Community Reports', date: new Date() } } }); let totalScore = '0'; if (score.total < 200) totalScore = '---'; else if (score.total > 800) totalScore = '800'; diff --git a/src/intervals/score.ts b/src/intervals/score.ts index 9a1b02c..b697a95 100644 --- a/src/intervals/score.ts +++ b/src/intervals/score.ts @@ -162,10 +162,20 @@ export default async function calculateScore(client: Client): Promise { + await score.updateOne({ $set: { total, activity, roles, moderation, cloudServices, other, staff, lastUpdate: new Date() } }); + if (!score.pin || score.pin?.length < 1) { + await cl.db.Score.updateOne({ userID: member.id }, { $set: { pin: [cl.util.randomNumber(100, 999), cl.util.randomNumber(10, 99), cl.util.randomNumber(1000, 9999)] } }); + } + } }); // client.util.signale.debug(`SCORE SET - ${member.username}\nTotal: ${total}\nActivity: ${activity}\nRoles: ${roles}\nModeration: ${moderation}\nCloud Services: ${cloudServices}\nStaff: ${staff}`); } }; diff --git a/src/main.ts b/src/main.ts index 75bd73c..c1ef0dd 100644 --- a/src/main.ts +++ b/src/main.ts @@ -11,7 +11,7 @@ import { Config } from '../types'; // eslint-disable-line async function main(): Promise { const read = await fs.readFile('../config.yaml', 'utf8'); const config: Config = parse(read); - const client = new Client(config.token, { defaultImageFormat: 'png', restMode: true }); + const client = new Client(config.token, { defaultImageFormat: 'png', restMode: true, intents: ['guildBans', 'guildEmojis', 'guildInvites', 'guildMembers', 'guildMessageReactions', 'guildMessages', 'guildPresences', 'guildWebhooks', 'guilds', 'directMessages'] }); client.config = config; await client.loadDatabase(); client.loadPlugins();