diff --git a/src/client/components/EventHooker.ts b/src/client/components/EventHooker.ts index e331a7a..5acef82 100644 --- a/src/client/components/EventHooker.ts +++ b/src/client/components/EventHooker.ts @@ -71,15 +71,20 @@ class EventHooker return; } + let guild = null; // this.logger.debug(`Handler ${eventName}`); // Should probably move this elsewhere, but testing this out -- or maybe not, might be the most appropriate place for this const eventArgs = []; for (const arg of args) { if (arg && typeof arg === 'object' && 'guild' in arg && arg.guild) + { + const wrapper = this.#target.getGuildWrapper(arg.guild!.id); // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore - arg.guildWrapper = this.#target.getGuildWrapper(arg.guild!.id); + arg.guildWrapper = wrapper; + guild = wrapper; + } eventArgs.push(arg); } @@ -108,7 +113,7 @@ class EventHooker catch (error) { const err = error as Error; - this.#logger.error(`Event handler (${handler.name}) for ${eventName} errored:\n${err.stack}`); + this.#logger.error(`Event handler (${handler.name}) for ${eventName} errored in ${guild ? 'guild ' + guild.id : 'DMs'}:\n${err.stack}`); } } }); diff --git a/src/client/components/commands/moderation/History.ts b/src/client/components/commands/moderation/History.ts index f8eda5b..652b0b3 100644 --- a/src/client/components/commands/moderation/History.ts +++ b/src/client/components/commands/moderation/History.ts @@ -120,7 +120,7 @@ class HistoryCommand extends SlashCommand const { infractions } = this.client.storageManager.mongodb; const resultsAmt = await infractions.count(query); const unresolvedAmt = await infractions.count({ ...query, resolved: false }); - if (!resultsAmt) + if (!resultsAmt) return { emoji: 'failure', index: 'COMMAND_HISTORY_NORESULTS' }; const maxPage = Math.ceil(resultsAmt / pageSize); @@ -264,9 +264,12 @@ class HistoryCommand extends SlashCommand const member = await invoker.memberWrapper().catch(() => null); if (!member || !member.isAdmin()) return { emoji: 'failure', index: 'COMMAND_HISTORY_NO_EXPORT_PERMS' }; - const logs = await this.client.storageManager.mongodb.infractions.find({ - guild: guild.id, target: user?.id - }, { projection: { _id: 0 } }); + const query: { guild: string, target?: string } = { + guild: guild.id + }; + if (user) + query.target= user.id; + const logs = await this.client.storageManager.mongodb.infractions.find(query, { projection: { _id: 0 } }); const string = JSON.stringify(logs, null, 4); const attachment = new AttachmentBuilder(Buffer.from(string), { name: `${guild.id}-moderation.json` }); const bytes = Buffer.byteLength(attachment.attachment as Buffer); diff --git a/src/client/storage/interfaces/MongodbTable.ts b/src/client/storage/interfaces/MongodbTable.ts index f98f59c..6a86ffe 100644 --- a/src/client/storage/interfaces/MongodbTable.ts +++ b/src/client/storage/interfaces/MongodbTable.ts @@ -24,13 +24,12 @@ class MongodbTable extends Table // Data Search - find (query: Filter, opts?: FindOptions) + find (query: Filter, options?: FindOptions) { if (!this.provider.initialised) return Promise.reject(new Error('MongoDB is not connected.')); - query = this._handleData(query); - - const cursor = this.collection().find(query, opts); + ({ query } = this._handleData(query)); + const cursor = this.collection().find(query, options); // if (opts?.sort) // cursor.sort(opts.sort); // if (opts?.skip) @@ -43,14 +42,14 @@ class MongodbTable extends Table findOne (query: Filter, opts?: FindOptions) { - query = this._handleData(query); + ({ query } = this._handleData(query)); return this.collection() .findOne(query, opts); } aggregate (pipeline: T[], options?: AggregateOptions) { - pipeline = this._handleData(pipeline); + ({ query: pipeline } = this._handleData(pipeline)); return this.collection() .aggregate(pipeline, options) .toArray(); @@ -58,7 +57,7 @@ class MongodbTable extends Table random (query: Filter, amount = 1) { - query = this._handleData(query); + ({ query } = this._handleData(query)); if (amount > 100) amount = 100; return this.collection() @@ -70,35 +69,35 @@ class MongodbTable extends Table insertOne (data: OptionalUnlessRequiredId, options?: InsertOneOptions) { - data = this._handleData(data); + ({ query: data } = this._handleData(data)); return this.collection() .insertOne(data, options); } - insertMany (data: OptionalUnlessRequiredId[], options?: BulkWriteOptions) + insertMany (data: OptionalUnlessRequiredId[], options?: BulkWriteOptions) { - data = this._handleData(data); + ({ query: data } = this._handleData(data)); return this.collection() .insertMany(data, options); } deleteOne (query: Filter, options?: DeleteOptions) { - query = this._handleData(query); + ({ query } = this._handleData(query)); return this.collection() .deleteOne(query, options); } deleteMany (query: Filter, options?: DeleteOptions) { - query = this._handleData(query); + ({ query } = this._handleData(query)); return this.collection() .deleteMany(query, options); } updateOne (query: Filter, data: UpdateFilter, options?: UpdateOptions) { - query = this._handleData(query); + ({ query, options } = this._handleData(query, options)); return this.collection() .updateOne(query, data, options); } @@ -114,7 +113,7 @@ class MongodbTable extends Table push (query: Filter, data: UpdateFilter, options?: UpdateOptions) { - query = this._handleData(query); + ({ query } = this._handleData(query)); return this.collection().updateOne(query, { $push: data }, options); // return new Promise((resolve, reject) => // { @@ -145,33 +144,36 @@ class MongodbTable extends Table count (query: Filter, options?: CountDocumentsOptions) { - query = this._handleData(query); + ({ query } = this._handleData(query)); return this.collection().countDocuments(query, options); } - _handleData (data: T): T + _handleData(query: T, options: UpdateOptions = {}): { query: T, options: UpdateOptions } { // Convert data._id to Mongo ObjectIds - if ('_id' in data && !(data._id instanceof ObjectId)) + if ('_id' in query && !(query._id instanceof ObjectId)) { - if (typeof data._id === 'string') - data._id = new ObjectId(data._id); - else if (data._id instanceof Array) - data._id = { - $in: Object.values(data._id).map((id) => + if (typeof query._id === 'string') + query._id = new ObjectId(query._id); + else if (query._id instanceof Array) + query._id = { + $in: Object.values(query._id).map((id) => { return new ObjectId(id); }) }; } - else if (data instanceof Array) + else if (query instanceof Array) { - data.forEach(obj => + query.forEach(obj => { if ('_id' in obj && (typeof obj._id === 'string')) obj._id = new ObjectId(obj._id); }); } - return data; + + if (!('upsert' in options)) + options.upsert = true; + return { query, options }; } // Getters diff --git a/src/middleware/Controller.ts b/src/middleware/Controller.ts index bfe835b..8bb3452 100644 --- a/src/middleware/Controller.ts +++ b/src/middleware/Controller.ts @@ -160,6 +160,8 @@ class Controller extends EventEmitter await Promise.all(promises); this.logger.status('Shutdown complete, goodbye'); this.logger.close(); + // eslint-disable-next-line no-process-exit + process.exit(); } createShard (totalShards: number)