@@ -20,6 +20,7 @@ import { colors, DAY, HOUR, MINUTE, MONTH, SECOND, WEEK, YEAR } from "../../comm
20
20
import Modlogs from "./modlogs.js" ;
21
21
import { TextBasedCommand } from "../../command-abstractions/text-based-command.js" ;
22
22
import {
23
+ moderation_state ,
23
24
moderation_type ,
24
25
moderation_entry ,
25
26
basic_moderation_with_user ,
@@ -161,6 +162,11 @@ export abstract class ModerationComponent extends BotComponent {
161
162
return false ;
162
163
}
163
164
165
+ database = this . wheatley . database . create_proxy < {
166
+ component_state : { id : "moderation" } & moderation_state ;
167
+ moderations : moderation_entry ;
168
+ } > ( ) ;
169
+
164
170
// Sorted by moderation end time
165
171
sleep_list : SleepList < mongo . WithId < moderation_entry > , mongo . BSON . ObjectId > ;
166
172
timer : NodeJS . Timer | null = null ;
@@ -196,11 +202,11 @@ export abstract class ModerationComponent extends BotComponent {
196
202
( async ( ) => {
197
203
ModerationComponent . moderations_count
198
204
. labels ( { type : this . type } )
199
- . set ( await this . wheatley . database . moderations . countDocuments ( { type : this . type } ) ) ;
205
+ . set ( await this . database . moderations . countDocuments ( { type : this . type } ) ) ;
200
206
if ( ! this . is_once_off ) {
201
207
ModerationComponent . active_moderations_count
202
208
. labels ( { type : this . type } )
203
- . set ( await this . wheatley . database . moderations . countDocuments ( { type : this . type , active : true } ) ) ;
209
+ . set ( await this . database . moderations . countDocuments ( { type : this . type , active : true } ) ) ;
204
210
}
205
211
} ) ( ) . catch ( this . wheatley . critical_error . bind ( this . wheatley ) ) ;
206
212
}
@@ -209,7 +215,7 @@ export abstract class ModerationComponent extends BotComponent {
209
215
this . update_counters ( ) ;
210
216
// Handle re-applications and sleep lists
211
217
// If once-off active is false so the rest is all fine
212
- const moderations = await this . wheatley . database . moderations . find ( { type : this . type , active : true } ) . toArray ( ) ;
218
+ const moderations = await this . database . moderations . find ( { type : this . type , active : true } ) . toArray ( ) ;
213
219
const moderations_to_sleep = moderations . filter (
214
220
entry => entry . duration !== null || entry . removed || entry . expunged ,
215
221
) ;
@@ -260,10 +266,10 @@ export abstract class ModerationComponent extends BotComponent {
260
266
assert ( ! this . is_once_off ) ;
261
267
// It's possible the moderation could have been removed or been updated by the time this runs, so fetch the
262
268
// current state
263
- const entry = unwrap ( await this . wheatley . database . moderations . findOne ( { _id : entry_from_sleep . _id } ) ) ;
269
+ const entry = unwrap ( await this . database . moderations . findOne ( { _id : entry_from_sleep . _id } ) ) ;
264
270
// Check for active but removed (can happen from data import)
265
271
if ( entry . active && ( entry . removed || entry . expunged ) ) {
266
- await this . wheatley . database . moderations . updateOne (
272
+ await this . database . moderations . updateOne (
267
273
{ _id : entry . _id } ,
268
274
{
269
275
$set : {
@@ -302,7 +308,7 @@ export abstract class ModerationComponent extends BotComponent {
302
308
}
303
309
// Check if moderation is still marked active, if so resolve it
304
310
if ( entry . active ) {
305
- await this . wheatley . database . moderations . updateOne (
311
+ await this . database . moderations . updateOne (
306
312
{ _id : entry . _id } ,
307
313
{
308
314
$set : {
@@ -389,7 +395,7 @@ export abstract class ModerationComponent extends BotComponent {
389
395
if ( this . is_once_off ) {
390
396
return ;
391
397
}
392
- const moderations = await this . wheatley . database . moderations
398
+ const moderations = await this . database . moderations
393
399
. find ( { user : member . user . id , type : this . type , active : true } )
394
400
. toArray ( ) ;
395
401
for ( const moderation of moderations ) {
@@ -403,20 +409,17 @@ export abstract class ModerationComponent extends BotComponent {
403
409
// Moderation entry handling
404
410
//
405
411
406
- async get_case_id ( ) {
407
- return ( await this . wheatley . database . get_bot_singleton ( ) ) . moderation_case_number ;
408
- }
409
-
410
412
async increment_case_id ( ) {
411
- const res = await this . wheatley . database . wheatley . updateOne (
412
- { id : "main " } ,
413
+ const res = await this . database . component_state . findOneAndUpdate (
414
+ { id : "moderation " } ,
413
415
{
414
416
$inc : {
415
- moderation_case_number : 1 ,
417
+ case_number : 1 ,
416
418
} ,
417
419
} ,
420
+ { upsert : true , returnDocument : "after" } ,
418
421
) ;
419
- assert ( res . acknowledged ) ;
422
+ return unwrap ( res ) . case_number ;
420
423
}
421
424
422
425
static case_id_mutex = new Mutex ( ) ;
@@ -426,9 +429,8 @@ export abstract class ModerationComponent extends BotComponent {
426
429
try {
427
430
await ModerationComponent . case_id_mutex . lock ( ) ;
428
431
await this . apply_moderation ( moderation ) ;
429
- moderation . case_number = await this . get_case_id ( ) ;
430
- const res = await this . wheatley . database . moderations . insertOne ( moderation ) ;
431
- await this . increment_case_id ( ) ;
432
+ moderation . case_number = await this . increment_case_id ( ) ;
433
+ const res = await this . database . moderations . insertOne ( moderation ) ;
432
434
if ( moderation . duration ) {
433
435
this . sleep_list . insert ( [
434
436
moderation . issued_at + moderation . duration ,
@@ -689,7 +691,7 @@ export abstract class ModerationComponent extends BotComponent {
689
691
) {
690
692
assert ( ! this . is_once_off ) ;
691
693
try {
692
- const res = await this . wheatley . database . moderations . findOneAndUpdate (
694
+ const res = await this . database . moderations . findOneAndUpdate (
693
695
{ user : user . id , type : this . type , active : true , ...additional_moderation_properties } ,
694
696
{
695
697
$set : {
0 commit comments