forked from Galactic/galactic-bot
Fix missing upserts, fixed history exporting, log which server causes event handler failure
This commit is contained in:
parent
e2d8e31588
commit
c8fb823b05
@ -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}`);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
@ -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);
|
||||
|
@ -24,13 +24,12 @@ class MongodbTable<Default extends Document = Document> extends Table
|
||||
|
||||
// Data Search
|
||||
|
||||
find<T extends Document = Default> (query: Filter<T>, opts?: FindOptions)
|
||||
find<T extends Document = Default> (query: Filter<T>, options?: FindOptions)
|
||||
{
|
||||
if (!this.provider.initialised)
|
||||
return Promise.reject(new Error('MongoDB is not connected.'));
|
||||
query = this._handleData(query);
|
||||
|
||||
const cursor = this.collection<T>().find(query, opts);
|
||||
({ query } = this._handleData(query));
|
||||
const cursor = this.collection<T>().find(query, options);
|
||||
// if (opts?.sort)
|
||||
// cursor.sort(opts.sort);
|
||||
// if (opts?.skip)
|
||||
@ -43,14 +42,14 @@ class MongodbTable<Default extends Document = Document> extends Table
|
||||
|
||||
findOne<T extends Document = Default> (query: Filter<T>, opts?: FindOptions)
|
||||
{
|
||||
query = this._handleData(query);
|
||||
({ query } = this._handleData(query));
|
||||
return this.collection<T>()
|
||||
.findOne(query, opts);
|
||||
}
|
||||
|
||||
aggregate<T extends Document = Default> (pipeline: T[], options?: AggregateOptions)
|
||||
{
|
||||
pipeline = this._handleData(pipeline);
|
||||
({ query: pipeline } = this._handleData(pipeline));
|
||||
return this.collection<T>()
|
||||
.aggregate(pipeline, options)
|
||||
.toArray();
|
||||
@ -58,7 +57,7 @@ class MongodbTable<Default extends Document = Document> extends Table
|
||||
|
||||
random<T extends Document = Default> (query: Filter<T>, amount = 1)
|
||||
{
|
||||
query = this._handleData(query);
|
||||
({ query } = this._handleData(query));
|
||||
if (amount > 100)
|
||||
amount = 100;
|
||||
return this.collection<T>()
|
||||
@ -70,35 +69,35 @@ class MongodbTable<Default extends Document = Document> extends Table
|
||||
|
||||
insertOne<T extends Document = Default> (data: OptionalUnlessRequiredId<T>, options?: InsertOneOptions)
|
||||
{
|
||||
data = this._handleData(data);
|
||||
({ query: data } = this._handleData(data));
|
||||
return this.collection<T>()
|
||||
.insertOne(data, options);
|
||||
}
|
||||
|
||||
insertMany<T extends Document = Default> (data: OptionalUnlessRequiredId<T>[], options?: BulkWriteOptions)
|
||||
insertMany<T extends Document = Default> (data: OptionalUnlessRequiredId<T>[], options?: BulkWriteOptions)
|
||||
{
|
||||
data = this._handleData(data);
|
||||
({ query: data } = this._handleData(data));
|
||||
return this.collection<T>()
|
||||
.insertMany(data, options);
|
||||
}
|
||||
|
||||
deleteOne<T extends Document = Default> (query: Filter<T>, options?: DeleteOptions)
|
||||
{
|
||||
query = this._handleData(query);
|
||||
({ query } = this._handleData(query));
|
||||
return this.collection<T>()
|
||||
.deleteOne(query, options);
|
||||
}
|
||||
|
||||
deleteMany<T extends Document = Default> (query: Filter<T>, options?: DeleteOptions)
|
||||
{
|
||||
query = this._handleData(query);
|
||||
({ query } = this._handleData(query));
|
||||
return this.collection<T>()
|
||||
.deleteMany(query, options);
|
||||
}
|
||||
|
||||
updateOne<T extends Document = Default> (query: Filter<T>, data: UpdateFilter<T>, options?: UpdateOptions)
|
||||
{
|
||||
query = this._handleData(query);
|
||||
({ query, options } = this._handleData(query, options));
|
||||
return this.collection<T>()
|
||||
.updateOne(query, data, options);
|
||||
}
|
||||
@ -114,7 +113,7 @@ class MongodbTable<Default extends Document = Document> extends Table
|
||||
|
||||
push<T extends Document = Default> (query: Filter<T>, data: UpdateFilter<T>, options?: UpdateOptions)
|
||||
{
|
||||
query = this._handleData(query);
|
||||
({ query } = this._handleData(query));
|
||||
return this.collection<T>().updateOne(query, { $push: data }, options);
|
||||
// return new Promise((resolve, reject) =>
|
||||
// {
|
||||
@ -145,33 +144,36 @@ class MongodbTable<Default extends Document = Document> extends Table
|
||||
|
||||
count<T extends Document = Default> (query: Filter<T>, options?: CountDocumentsOptions)
|
||||
{
|
||||
query = this._handleData(query);
|
||||
({ query } = this._handleData(query));
|
||||
return this.collection<T>().countDocuments(query, options);
|
||||
}
|
||||
|
||||
_handleData<T extends (object | object[])> (data: T): T
|
||||
_handleData<T extends (object | object[])>(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
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user