Bugfixes to eventhooker and infraction dms

This commit is contained in:
Erik 2023-12-05 23:25:12 +02:00
parent f65604ec2b
commit 9d88266f41
12 changed files with 1205 additions and 1215 deletions

View File

@ -76,7 +76,7 @@ class EventHooker
const eventArgs = [];
for (const arg of args)
{
if (arg && typeof arg === 'object' && 'guild' in arg)
if (arg && typeof arg === 'object' && 'guild' in arg && arg.guild)
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
arg.guildWrapper = this.#target.getGuildWrapper(arg.guild!.id);

View File

@ -626,7 +626,7 @@ class ModerationManager implements Initialisable
if (updateCase)
await this.#client.storage.mongodb.infractions.updateOne(
{ id: infraction.id },
{ _callbacked: true }
{ $set: { _callbacked: true } }
).catch((e) =>
{
this.#logger.error(`Error during update of infraction:\n${e.stack || e}`);

View File

@ -132,7 +132,7 @@ class ImportCommand extends SlashCommand
for (const log of existingLogs)
{
log.case += highestOldId;
await this.client.mongodb.infractions.updateOne({ _id: log._id }, { case: log.case });
await this.client.mongodb.infractions.updateOne({ _id: log._id }, { $set: { case: log.case } });
}
await this.client.mongodb.infractions.insertMany(imported);
if (!guild.data.caseId)
@ -193,12 +193,12 @@ class ImportCommand extends SlashCommand
else if (version === '3')
{
delete webhook.feature;
await this.client.storageManager.mongodb.webhooks.updateOne({ feature: 'messages', guild: guild.id }, webhook);
await this.client.storageManager.mongodb.webhooks.updateOne({ feature: 'messages', guild: guild.id }, { $set: webhook });
}
}
if (permissions)
await this.client.storageManager.mongodb.permissions.updateOne({ guildId: guild.id }, permissions);
await this.client.storageManager.mongodb.permissions.updateOne({ guildId: guild.id }, { $set: permissions });
const { premium } = imported.settings;
delete imported.settings.premium;

View File

@ -48,9 +48,9 @@ class UtilityHook extends Observer
await this.client.storageManager.mongodb.roleCache.updateOne({
member: member.id, guild: guild.id
}, {
}, { $set: {
roles: storeThese, timestamp: Date.now()
});
} });
}
async automute (member: ExtendedGuildMember)

View File

@ -1,4 +1,4 @@
import { CommandParams, FormatParams } from '../../../../../@types/Client.js';
import { CommandOptionType, CommandParams, FormatParams } from '../../../../../@types/Client.js';
import { ProtectionSettings, ProtectionType } from '../../../../../@types/Settings.js';
import Util from '../../../../utilities/Util.js';
import DiscordClient from '../../../DiscordClient.js';
@ -49,11 +49,11 @@ class ProtectionSetting extends Setting
},
{
name: 'enabled',
description: 'Whether setting is active or not'
description: 'Whether setting is active or not',
type: CommandOptionType.BOOLEAN
}
]
});
}
async execute (invoker: InvokerWrapper<true>, opts: CommandParams, setting: ProtectionSettings)

View File

@ -121,7 +121,7 @@ class GuildWrapper
const cb = { timeout: setTimeout(handler.bind(this), diff, data), data };
this.#callbacks.set(data.id, cb);
if (update)
await this.#client.mongodb.callbacks.updateOne({ id: data.id, guild: this.id }, data);
await this.#client.mongodb.callbacks.updateOne({ id: data.id, guild: this.id }, { $set: data });
}
async removeCallback (id: string)
@ -228,7 +228,7 @@ class GuildWrapper
data.settings = settings as GuildSettings;
data._version = configVersion;// '3.slash.2';
await this.#client.mongodb.guilds.deleteOne({ guildId: this.id });
await this.#client.mongodb.guilds.updateOne({ guildId: this.id }, data);
await this.#client.mongodb.guilds.updateOne({ guildId: this.id }, { $set: data });
}
this.#data = data;
return data;
@ -277,7 +277,7 @@ class GuildWrapper
{
try
{
await this.#client.mongodb.guilds.updateOne({ guildId: this.id }, { _version: configVersion, ...data });
await this.#client.mongodb.guilds.updateOne({ guildId: this.id }, { $set: { _version: configVersion, ...data } });
this.#data = { ...this.#data, ...data, _version: configVersion };
this.#storageLog(`Database update: Data (guild:${this.id})`);
}
@ -319,7 +319,7 @@ class GuildWrapper
throw new Error('Permissions not loaded');
try
{
await this.#client.mongodb.permissions.updateOne({ guildId: this.id }, this.#permissions, { upsert: true });
await this.#client.mongodb.permissions.updateOne({ guildId: this.id }, { $set: this.#permissions }, { upsert: true });
}
catch (error)
{
@ -423,7 +423,7 @@ class GuildWrapper
}
this.#webhooks.set(feature, webhook);
await this.#client.mongodb.webhooks.updateOne({ feature, guild: this.id }, { hookId: webhook.id, token: webhook.token });
await this.#client.mongodb.webhooks.updateOne({ feature, guild: this.id }, { $set: { hookId: webhook.id, token: webhook.token } });
}
/**

View File

@ -152,7 +152,7 @@ class UserWrapper
{
await this.#client.mongodb.users.updateOne(
{ guildId: this.id },
data
{ $set: data }
);
this.#settings = {
...this.#settings,

View File

@ -112,7 +112,7 @@ class UnlockdownInfraction extends Infraction
if (callback)
await this.client.moderation.removeCallback(callback.infraction, true);
else
await this.client.mongodb.infractions.updateOne({ id: latest.id }, { _callbacked: true });
await this.client.mongodb.infractions.updateOne({ id: latest.id }, { $set: { _callbacked: true } });
}
await this.handle();

View File

@ -166,7 +166,6 @@ class Infraction
async handle ()
{
// Infraction was fetched from database, i.e. was already executed previously
if (this.#fetched)
throw new Error('Cannot handle a fetched Infraction');
@ -197,7 +196,6 @@ class Infraction
this.#dmLogMessage = await this.#moderationLog.send({ embeds: [ await this.#embed() ] }).catch(null);
this.#modLogMessageId = this.#dmLogMessage?.id || null;
}
}
else
{
@ -218,30 +216,25 @@ class Infraction
.replace(/\{infraction\}/ugim, this.dictionary.past)
.replace(/\{from\|on\}/ugim, Constants.RemovedInfractions.includes(this.#type!) ? 'from' : 'on'); // add more if you want i should probably add a better system for this...
if (Util.isSendable(this.#target))
{
const logMessage = await this.#target.send({
content: message,
embeds: [ await this.#embed(true) ]
}).catch(null);
this.#dmLogMessageId = logMessage.id;
}).catch(() => null);
this.#dmLogMessageId = logMessage?.id ?? null;
}
}
}
if (this.#duration)
{
await this.#client.moderation.handleCallbacks([ this.json ]);
}
/* LMAOOOO PLEASE DONT JUDGE ME */
if (this.#data.roles)
delete this.#data.roles;
return this.save();
}
execute (): Promise<InfractionSuccess | InfractionFail>
@ -254,7 +247,7 @@ class Infraction
const filter: {id: string, _id?: ObjectId} = { id: this.id };
if (this.#mongoId)
filter._id = this.#mongoId;
return this.#client.mongodb.infractions.updateOne(filter, this.json)
return this.#client.mongodb.infractions.updateOne(filter, { $set: this.json })
.catch((error: Error) =>
{
this.#logger.error(`There was an issue saving infraction data to the database.\n${error.stack || error}\nInfraction data:\n${inspect(this.json)}`);
@ -322,7 +315,6 @@ class Infraction
if (this.description && this.description instanceof Function)
description += this.description(dm);
if (this.#resolved)
{
description += '\n' + this.#guild.format('INFRACTION_RESOLVED');
@ -345,14 +337,12 @@ class Infraction
}
embed.description = description;
return embed;
}
description (_dm: boolean): string
{
throw new Error('Description is to be implemented by a subclass');
return '';
}
protected get client ()

View File

@ -124,7 +124,7 @@ class Util
// eslint-disable-next-line @typescript-eslint/no-explicit-any
static isSendable (obj: any): obj is { send: () => Promise<Message>}
{
return Util.has(obj, 'send') && typeof obj.send === 'function';
return typeof obj.send === 'function';
}
// static hasProperty<T> (obj: any, name: string): obj is { [key in typeof name]: T }