Merge branch 'ts-rewrite' of https://git.corgi.wtf/Galactic/galactic-bot into ts-rewrite

This commit is contained in:
D3vision 2023-12-07 23:44:00 +01:00
commit 2703460b84
6 changed files with 24 additions and 36 deletions

View File

@ -366,12 +366,12 @@ class ModerationManager implements Initialisable
break; break;
} }
} }
if (modpoints.multiplier) if (modpoints.multiplier)
expiration *= points; expiration *= points;
} }
const verify = async (infraction: InfractionClass, escalated = false): const verify = async (infraction: InfractionClass, escalated = false):
Promise<(InfractionFail | InfractionSuccess) & { escalation?: EscalationResult }> => Promise<(InfractionFail | InfractionSuccess) & { escalation?: EscalationResult }> =>
{ {
// const verification = await infraction.verify(info.executor, target, info.channel); // const verification = await infraction.verify(info.executor, target, info.channel);
@ -456,9 +456,9 @@ class ModerationManager implements Initialisable
let response = await verify(infraction); let response = await verify(infraction);
if (response.escalation) if (response.escalation)
{ {
if (Constant.Hierarchy[infraction.type] <= Constant.Hierarchy[response.escalation.type]) if (Constant.Hierarchy[infraction.type] <= Constant.Hierarchy[response.escalation.type])
{ {
// this is just to shut up the TS checker // this is just to shut up the TS checker
const escalationClass = Constant.Infractions[response.escalation.type] as typeof InfractionClass; const escalationClass = Constant.Infractions[response.escalation.type] as typeof InfractionClass;
@ -499,7 +499,6 @@ class ModerationManager implements Initialisable
points, expiration, timestamp: response.infraction.timestamp, id: response.infraction.id points, expiration, timestamp: response.infraction.timestamp, id: response.infraction.id
}); });
return result; return result;
} }
async _handleArguments (invoker: InvokerWrapper, targets: ModerationTargets, args: {[key: string]: CommandOption | undefined}) async _handleArguments (invoker: InvokerWrapper, targets: ModerationTargets, args: {[key: string]: CommandOption | undefined})
@ -569,13 +568,13 @@ class ModerationManager implements Initialisable
executor executor
}).execute(); }).execute();
} }
catch (err) catch (err)
{ {
const error = err as Error; const error = err as Error;
this.#logger.error(`Error when resolving infraction:\n${error.stack || error}`); this.#logger.error(`Error when resolving infraction:\n${error.stack || error}`);
} }
} }
else else
{ {
// Target left guild or channel was removed from the guild. What should happen in this situation? // Target left guild or channel was removed from the guild. What should happen in this situation?
// Maybe continue checking if the user rejoins, but the channel will always be gone. // Maybe continue checking if the user rejoins, but the channel will always be gone.
@ -614,9 +613,7 @@ class ModerationManager implements Initialisable
}, callBackAt - currentDate), }, callBackAt - currentDate),
infraction infraction
}); });
} }
} }
async removeCallback (infraction: InfractionClass | InfractionJSON, updateCase = false) async removeCallback (infraction: InfractionClass | InfractionJSON, updateCase = false)
@ -668,8 +665,7 @@ class ModerationManager implements Initialisable
async findLatestInfraction (type: InfractionType, target: ModerationTarget) async findLatestInfraction (type: InfractionType, target: ModerationTarget)
{ {
const callback = this.#callbacks.filter((c) =>
const callback = this.#callbacks.filter((c) =>
{ {
return c.infraction.type === type return c.infraction.type === type
&& c.infraction.target === target.id; && c.infraction.target === target.id;
@ -683,7 +679,6 @@ class ModerationManager implements Initialisable
{ sort: { timestamp: -1 } } { sort: { timestamp: -1 } }
); );
return result || null; return result || null;
} }
} }

View File

@ -175,7 +175,6 @@ class LockdownInfraction extends Infraction
async verify () async verify ()
{ {
const perms = (this.target as GuildChannel).permissionsFor(this.client.user!); const perms = (this.target as GuildChannel).permissionsFor(this.client.user!);
const missing = perms?.missing([ 'ManageRoles', 'SendMessages', 'AddReactions', 'ViewChannel' ]); const missing = perms?.missing([ 'ManageRoles', 'SendMessages', 'AddReactions', 'ViewChannel' ]);
if (!perms || missing?.length) if (!perms || missing?.length)

View File

@ -191,7 +191,6 @@ class MuteInfraction extends Infraction
return this._fail('COMMAND_MUTE_MISSING_MANAGEROLE_PERM'); return this._fail('COMMAND_MUTE_MISSING_MANAGEROLE_PERM');
return super._verify(); return super._verify();
} }
async resolve (_staff: UserWrapper, _reason: string, _notify: boolean): Promise<void | ResolveResult> async resolve (_staff: UserWrapper, _reason: string, _notify: boolean): Promise<void | ResolveResult>
@ -208,7 +207,6 @@ class MuteInfraction extends Infraction
error = false; error = false;
const settings = await this.guild.settings(); const settings = await this.guild.settings();
const { removedRoles = [], muteType = settings.mute.type, muteRole = settings.mute.role } = this.data || {}; const { removedRoles = [], muteType = settings.mute.type, muteRole = settings.mute.role } = this.data || {};
// TODO: Change this to not rely on the member // TODO: Change this to not rely on the member
const member = await this.guild.memberWrapper(this.targetId!).catch(() => null); const member = await this.guild.memberWrapper(this.targetId!).catch(() => null);

View File

@ -5,7 +5,7 @@ import { WarnData } from '../../../@types/Infractions.js';
import { MemberWrapper } from '../components/wrappers/index.js'; import { MemberWrapper } from '../components/wrappers/index.js';
import { InfractionTargetType, InfractionType } from '../../../@types/Client.js'; import { InfractionTargetType, InfractionType } from '../../../@types/Client.js';
class WarnInfraction extends Infraction class WarnInfraction extends Infraction
{ {
static get Type (): InfractionType static get Type (): InfractionType
{ {
@ -21,12 +21,10 @@ class WarnInfraction extends Infraction
constructor (client: DiscordClient, logger: LoggerClient, opts: WarnData) constructor (client: DiscordClient, logger: LoggerClient, opts: WarnData)
{ {
if (opts.fetched)
if (opts.fetched)
super(client, logger, opts); super(client, logger, opts);
else else
{ {
super(client, logger, { super(client, logger, {
targetType: 'USER', targetType: 'USER',
type: opts.type, type: opts.type,
@ -47,9 +45,7 @@ class WarnInfraction extends Infraction
if (!(opts.target instanceof MemberWrapper)) if (!(opts.target instanceof MemberWrapper))
throw new Error('Guild member required'); throw new Error('Guild member required');
this.member = opts.target; this.member = opts.target;
} }
} }
async execute () async execute ()
@ -60,13 +56,11 @@ class WarnInfraction extends Infraction
async verify () async verify ()
{ {
// NOTE: If I want to readd permission checking for escalations. // NOTE: If I want to readd permission checking for escalations.
// const permissions = await this.client.permissions.execute(this.message, this.message.command); // const permissions = await this.client.permissions.execute(this.message, this.message.command);
// if(permissions.error) return super._fail('COMMAND_WARN_INSUFFICIENTPERMISSIONS'); // if(permissions.error) return super._fail('COMMAND_WARN_INSUFFICIENTPERMISSIONS');
return super._verify(); return super._verify();
} }
} }

View File

@ -82,8 +82,8 @@ class CommandOption
throw new Error('Type cannot be an array here'); throw new Error('Type cannot be an array here');
this.#type = options.type ?? CommandOptionType.STRING; this.#type = options.type ?? CommandOptionType.STRING;
this.#required = Boolean(options.required); this.#required = Boolean(options.required);
this.#autocomplete = options.autocomplete || false; this.#autocomplete = options.autocomplete ?? false;
this.#choices = options.choices || []; // Used for STRING/INTEGER/NUMBER types. this.#choices = options.choices ?? []; // Used for STRING/INTEGER/NUMBER types.
this.#options = []; this.#options = [];
if (options.options) if (options.options)
@ -99,12 +99,12 @@ class CommandOption
// Used for INTEGER/NUMBER/FLOAT types. // Used for INTEGER/NUMBER/FLOAT types.
if (typeof options.minimum === 'number') if (typeof options.minimum === 'number')
this.#minimum = options.minimum; this.#minimum = options.minimum;
if (typeof options.maximum === 'number') if (typeof options.maximum === 'number')
this.#maximum = options.maximum; this.#maximum = options.maximum;
if (typeof this.#maximum === 'undefined' || this.#maximum > Number.MAX_SAFE_INTEGER) if (typeof this.#maximum === 'undefined' || this.#maximum > Number.MAX_SAFE_INTEGER)
this.#maximum = Number.MAX_SAFE_INTEGER; this.#maximum = Number.MAX_SAFE_INTEGER;
this.#slashOption = options.slashOption || false; this.#slashOption = options.slashOption ?? false;
this.#flag = options.flag ?? false; // used with message based command options this.#flag = options.flag ?? false; // used with message based command options
this.#valueOptional = options.valueOptional ?? false; this.#valueOptional = options.valueOptional ?? false;
if (this.#valueOptional && typeof options.defaultValue === 'undefined') if (this.#valueOptional && typeof options.defaultValue === 'undefined')
@ -115,7 +115,7 @@ class CommandOption
// this.words = options.words ?? null; // Used when parsing strings if the command has multiple string types that aren't flags // this.words = options.words ?? null; // Used when parsing strings if the command has multiple string types that aren't flags
// Used in cloned options when parsing final value // Used in cloned options when parsing final value
this.#guild = options.guild || null; this.#guild = options.guild ?? null;
this.#rawValue = options.rawValue ?? null; // Raw value input from Discord. -- use ?? where the value is potentially false, otherwise we end up with false -> null this.#rawValue = options.rawValue ?? null; // Raw value input from Discord. -- use ?? where the value is potentially false, otherwise we end up with false -> null
} }
@ -194,7 +194,7 @@ class CommandOption
return `__${usage.name.replace('》', '').trim()}__\n${usage.value}`; return `__${usage.name.replace('》', '').trim()}__\n${usage.value}`;
}).join('\n\n'); }).join('\n\n');
} }
else if (CommandOptionType[this.type] === 'SUB_COMMAND') else if (CommandOptionType[this.type] === 'SUB_COMMAND')
{ {
if (this.options.length) if (this.options.length)
value = this.options.map((opt) => opt.usage(guild, true).value).join('\n'); value = this.options.map((opt) => opt.usage(guild, true).value).join('\n');
@ -294,7 +294,8 @@ class CommandOption
continue; continue;
if (PointsReg.test(str)) if (PointsReg.test(str))
{ {
value = num; removed = [ str ]; value = num;
removed = [ str ];
break; break;
} }
const index = this.#rawValue.indexOf(str); const index = this.#rawValue.indexOf(str);
@ -302,7 +303,8 @@ class CommandOption
const tmp = str + next; const tmp = str + next;
if (PointsReg.test(tmp)) if (PointsReg.test(tmp))
{ {
value = num; removed = [ str, next ]; value = num;
removed = [ str, next ];
break; break;
} }
} }

View File

@ -242,7 +242,7 @@ class Infraction
throw new Error('Expected this method to be implemented in a subclass'); throw new Error('Expected this method to be implemented in a subclass');
} }
async save () async save ()
{ {
const { json } = this; const { json } = this;
const filter: {id: string, _id?: ObjectId} = { id: this.id }; const filter: {id: string, _id?: ObjectId} = { id: this.id };
@ -251,7 +251,7 @@ class Infraction
if (json.points && typeof json.points !== 'number') if (json.points && typeof json.points !== 'number')
throw new Error('Invalid points type'); throw new Error('Invalid points type');
return this.#client.mongodb.infractions.updateOne(filter, { $set: this.json }) return this.#client.mongodb.infractions.updateOne(filter, { $set: this.json })
.catch((error: Error) => .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)}`); this.#logger.error(`There was an issue saving infraction data to the database.\n${error.stack || error}\nInfraction data:\n${inspect(this.json)}`);
}); });
@ -259,9 +259,9 @@ class Infraction
hyperlink (modLogMessage = false) hyperlink (modLogMessage = false)
{ {
if (this.#hyperlink) if (this.#hyperlink)
return this.#hyperlink; return this.#hyperlink;
if (modLogMessage) if (modLogMessage)
return `https://discord.com/channels/${this.#guildId}/${this.#modLogId}/${this.#modLogMessageId}`; return `https://discord.com/channels/${this.#guildId}/${this.#modLogId}/${this.#modLogMessageId}`;
return `https://discord.com/channels/${this.#guildId}/${this.#channelId}/${this.#messageId}`; return `https://discord.com/channels/${this.#guildId}/${this.#channelId}/${this.#messageId}`;
} }