Skip to content

Commit 4824a76

Browse files
committed
Introduce queueItemReferences
1 parent 425d8ee commit 4824a76

File tree

3 files changed

+285
-9
lines changed

3 files changed

+285
-9
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"bluebird": "^3.7.2",
2121
"discord.js": "^14.3.0",
2222
"ramda": "^0.28.0",
23+
"sharp": "^0.31.0",
2324
"uuid": "^8.3.2",
2425
"winston": "^3.8.1"
2526
},
@@ -29,6 +30,7 @@
2930
"@types/chai": "^4.3.3",
3031
"@types/mocha": "^9.1.1",
3132
"@types/ramda": "^0.28.15",
33+
"@types/sharp": "^0.31.0",
3234
"@types/sinon": "^10.0.13",
3335
"@types/uuid": "^8.3.4",
3436
"chai": "^4.3.6",

src/bot.ts

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ export class Bot extends Client {
1515
partials: [Partials.Message, Partials.Channel, Partials.Reaction],
1616
...options,
1717
})
18+
1819
this.queue = []
20+
this.queueItemReferences = [] // TODO: Can we store this elsewhere and keep non-json references? Redis?
1921
this.processing = null
2022
this.config = config
2123
this.log = logger
@@ -28,8 +30,14 @@ export class Bot extends Client {
2830
*/
2931
public queue: QueueItem[]
3032

33+
/**
34+
* Array of previously used QueueItems.
35+
*/
36+
public queueItemReferences: QueueItem[]
37+
3138
/**
3239
* Current QueueItem processing.
40+
* TODO: Support multi-processing
3341
*/
3442
public processing: QueueItem | null
3543

@@ -66,20 +74,57 @@ export class Bot extends Client {
6674
public addQueue = (queueItem: QueueItem): number => this.queue.push(queueItem)
6775

6876
/**
69-
* Deletes a QueueItem from queue. Throws if it can't find a QueueItem for uuid.
77+
* Deletes a QueueItem from queue, adds it to queueItemReferences. Throws if it can't find a QueueItem for uuid.
7078
* @param uuid UUID of QueueItem.
7179
* @returns Array of deleted QueueItems.
7280
*/
7381
public removeQueue = (uuid: string): QueueItem[] => {
7482
const queueIndex = this.queue.findIndex(queueItem => queueItem.uuid === uuid)
7583

7684
if (queueIndex === -1) return []
77-
return this.queue.splice(queueIndex, 1)
85+
const queueItem = this.queue.splice(queueIndex, 1)
86+
this.queueItemReferences.push(queueItem[0])
87+
return queueItem
88+
}
89+
90+
/**
91+
* Deletes a QueueItem from QueueItem reference storage. Throws if it can't find a QueueItem for uuid.
92+
* @param uuid UUID of QueueItem.
93+
* @returns Array of deleted QueueItems.
94+
*/
95+
public removeQueueItemReference = (uuid: string): QueueItem[] => {
96+
const queueIndex = this.queueItemReferences.findIndex(queueItem => queueItem.uuid === uuid)
97+
98+
if (queueIndex === -1) return []
99+
return this.queueItemReferences.splice(queueIndex, 1)
78100
}
79101

102+
/**
103+
* Finds a QueueItem in queue storage by uuid.
104+
* @param uuid UUID of QueueItem.
105+
* @returns Array of found QueueItems.
106+
*/
80107
public findQueue = (uuid: string): QueueItem | undefined => {
81108
return this.queue.find(queueItem => queueItem.uuid === uuid)
82109
}
110+
111+
/**
112+
* Finds a QueueItem in reference storage by uuid.
113+
* @param uuid UUID of QueueItem.
114+
* @returns Array of found QueueItems.
115+
*/
116+
public findQueueItemReference = (uuid: string): QueueItem | undefined => {
117+
return this.queueItemReferences.find(queueItem => queueItem.uuid === uuid)
118+
}
119+
120+
/**
121+
* Finds a QueueItem in reference storage by message ID.
122+
* @param uuid UUID of QueueItem.
123+
* @returns Array of found QueueItems.
124+
*/
125+
public findQueueItemReferenceByMessageID = (messageId: string): QueueItem | undefined => {
126+
return this.queueItemReferences.find(queueItem => queueItem.messageId === messageId)
127+
}
83128
}
84129

85130
module.exports = async (config: typeof Config, logger: Logger, options: {[k: string]: string}) => {

0 commit comments

Comments
 (0)