diff options
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/extensions/discord-akairo/BushClientUtil.ts | 35 | ||||
-rw-r--r-- | src/lib/extensions/discord.js/BushGuild.ts | 28 | ||||
-rw-r--r-- | src/lib/extensions/discord.js/BushGuildMember.ts | 24 | ||||
-rw-r--r-- | src/lib/models/ActivePunishment.ts | 4 | ||||
-rw-r--r-- | src/lib/utils/BushConstants.ts | 44 |
5 files changed, 70 insertions, 65 deletions
diff --git a/src/lib/extensions/discord-akairo/BushClientUtil.ts b/src/lib/extensions/discord-akairo/BushClientUtil.ts index ddc03d0..a7dd535 100644 --- a/src/lib/extensions/discord-akairo/BushClientUtil.ts +++ b/src/lib/extensions/discord-akairo/BushClientUtil.ts @@ -46,8 +46,10 @@ import { } from 'discord.js'; import got from 'got'; import humanizeDuration from 'humanize-duration'; +import _ from 'lodash'; import moment from 'moment'; import { inspect, InspectOptions, promisify } from 'util'; +import CommandErrorListener from '../../../listeners/commands/commandError'; import { ActivePunishment, ActivePunishmentType } from '../../models/ActivePunishment'; import { BushNewsChannel } from '../discord.js/BushNewsChannel'; import { BushTextChannel } from '../discord.js/BushTextChannel'; @@ -569,7 +571,7 @@ export class BushClientUtil extends ClientUtil { const res: hastebinRes = await got.post(`${url}/documents`, { body: content }).json(); return `${url}/${res.key}`; } catch { - void client.console.error('Haste', `Unable to upload haste to ${url}`); + void client.console.error('haste', `Unable to upload haste to ${url}`); } } return 'Unable to post'; @@ -994,7 +996,7 @@ export class BushClientUtil extends ClientUtil { const newValue = this.addOrRemoveFromArray(action, oldValue, value); row[key] = newValue; client.cache.global[key] = newValue; - return await row.save().catch((e) => client.logger.error('insertOrRemoveFromGlobal', e?.stack || e)); + return await row.save().catch((e) => util.handleError('insertOrRemoveFromGlobal', e)); } /** @@ -1110,8 +1112,8 @@ export class BushClientUtil extends ClientUtil { duration: duration, guild }); - const saveResult: ModLog | null = await modLogEntry.save().catch((e) => { - void client.console.error('createModLogEntry', e?.stack || e); + const saveResult: ModLog | null = await modLogEntry.save().catch(async (e) => { + await util.handleError('createModLogEntry', e); return null; }); @@ -1130,17 +1132,17 @@ export class BushClientUtil extends ClientUtil { extraInfo?: Snowflake; }): Promise<ActivePunishment | null> { const expires = options.duration ? new Date(new Date().getTime() + options.duration) : undefined; + client.console.debug(expires); + client.console.debug(typeof expires); const user = client.users.resolveId(options.user)!; const guild = client.guilds.resolveId(options.guild)!; const type = this.#findTypeEnum(options.type)!; - - const entry = options.extraInfo - ? ActivePunishment.build({ user, type, guild, expires, modlog: options.modlog??, extraInfo: options.extraInfo }) + ? ActivePunishment.build({ user, type, guild, expires, modlog: options.modlog, extraInfo: options.extraInfo }) : ActivePunishment.build({ user, type, guild, expires, modlog: options.modlog }); - return await entry.save().catch((e) => { - void client.console.error('createPunishmentEntry', e?.stack || e); + return await entry.save().catch(async (e) => { + await util.handleError('createPunishmentEntry', e); return null; }); } @@ -1159,15 +1161,15 @@ export class BushClientUtil extends ClientUtil { const entries = await ActivePunishment.findAll({ // finding all cases of a certain type incase there were duplicates or something where: { user, guild, type } - }).catch((e) => { - void client.console.error('removePunishmentEntry', e?.stack || e); + }).catch(async (e) => { + await util.handleError('removePunishmentEntry', e); success = false; }); if (entries) { // eslint-disable-next-line @typescript-eslint/no-misused-promises entries.forEach(async (entry) => { - await entry.destroy().catch((e) => { - void client.console.error('removePunishmentEntry', e?.stack || e); + await entry.destroy().catch(async (e) => { + await util.handleError('removePunishmentEntry', e); }); success = false; }); @@ -1357,6 +1359,13 @@ export class BushClientUtil extends ClientUtil { return new Promise((resolve) => setTimeout(resolve, s * 1000)); } + public async handleError(context: string, error: Error) { + await client.console.error(_.camelCase(context), `An error occurred:\n${error?.stack ?? (error as any)}`, false); + await client.console.channelError({ + embeds: [await CommandErrorListener.generateErrorEmbed({ type: 'unhandledRejection', error: error, context })] + }); + } + //~ modified from https://stackoverflow.com/questions/31054910/get-functions-methods-of-a-class //~ answer by Bruno Grieder //~ public getMethods(obj: any): string { diff --git a/src/lib/extensions/discord.js/BushGuild.ts b/src/lib/extensions/discord.js/BushGuild.ts index 2c3b4bd..09e355c 100644 --- a/src/lib/extensions/discord.js/BushGuild.ts +++ b/src/lib/extensions/discord.js/BushGuild.ts @@ -84,25 +84,21 @@ export class BushGuild extends Guild { if (!unbanSuccess) return 'error unbanning'; // add modlog entry - const modlog = await util - .createModLogEntry({ - type: ModLogType.UNBAN, - user, - moderator: moderator.id, - reason: options.reason, - guild: this - }) - .catch(() => null); + const modlog = await util.createModLogEntry({ + type: ModLogType.UNBAN, + user, + moderator: moderator.id, + reason: options.reason, + guild: this + }); if (!modlog) return 'error creating modlog entry'; // remove punishment entry - const removePunishmentEntrySuccess = await util - .removePunishmentEntry({ - type: 'ban', - user, - guild: this - }) - .catch(() => null); + const removePunishmentEntrySuccess = await util.removePunishmentEntry({ + type: 'ban', + user, + guild: this + }); if (!removePunishmentEntrySuccess) return 'error removing ban entry'; const userObject = client.users.cache.get(user); diff --git a/src/lib/extensions/discord.js/BushGuildMember.ts b/src/lib/extensions/discord.js/BushGuildMember.ts index e738b5e..f71a435 100644 --- a/src/lib/extensions/discord.js/BushGuildMember.ts +++ b/src/lib/extensions/discord.js/BushGuildMember.ts @@ -132,9 +132,10 @@ export class BushGuildMember extends GuildMember { }) : { log: null }; - if (!modlog) return 'error creating modlog entry'; + if (!modlog && options.addToModlog) return 'error creating modlog entry'; - if (options.addToModlog) { + if (options.addToModlog || options.duration) { + client.console.debug('got to punishment'); const punishmentEntrySuccess = await util.createPunishmentEntry({ type: 'role', user: this, @@ -212,7 +213,8 @@ export class BushGuildMember extends GuildMember { const muteSuccess = await this.roles .add(muteRole, `[Mute] ${moderator.tag} | ${options.reason ?? 'No reason provided.'}`) .catch(async (e) => { - await client.console.warn('muteRoleAddError', e?.stack || e); + await client.console.warn('muteRoleAddError', e); + client.console.debug(e); return false; }); if (!muteSuccess) return 'error giving mute role'; @@ -322,15 +324,13 @@ export class BushGuildMember extends GuildMember { if (!kickSuccess) return 'error kicking'; // add modlog entry - const { log: modlog } = await util - .createModLogEntry({ - type: ModLogType.KICK, - user: this, - moderator: moderator.id, - reason: options.reason, - guild: this.guild - }) - .catch(() => ({ log: null })); + const { log: modlog } = await util.createModLogEntry({ + type: ModLogType.KICK, + user: this, + moderator: moderator.id, + reason: options.reason, + guild: this.guild + }); if (!modlog) return 'error creating modlog entry'; if (!dmSuccess) return 'failed to dm'; return 'success'; diff --git a/src/lib/models/ActivePunishment.ts b/src/lib/models/ActivePunishment.ts index 10ae766..794560f 100644 --- a/src/lib/models/ActivePunishment.ts +++ b/src/lib/models/ActivePunishment.ts @@ -26,7 +26,7 @@ export interface ActivePunishmentModelCreationAttributes { guild: Snowflake; extraInfo?: Snowflake; expires?: Date; - modlog: string; + modlog?: string; } const NEVER_USED = 'This should never be executed'; @@ -140,7 +140,7 @@ export class ActivePunishment }, modlog: { type: DataTypes.STRING, - allowNull: false, + allowNull: true, references: { model: 'ModLogs', key: 'id' diff --git a/src/lib/utils/BushConstants.ts b/src/lib/utils/BushConstants.ts index dcc551e..f2ca327 100644 --- a/src/lib/utils/BushConstants.ts +++ b/src/lib/utils/BushConstants.ts @@ -68,37 +68,37 @@ export class BushConstants { // Somewhat stolen from @Mzato0001 public static TimeUnits: { [key: string]: { match: RegExp; value: number } } = { - years: { - match: / (?:(?<years>-?(?:\d+)?\.?\d+) *(?:years?|y))/im, - value: 1000 * 60 * 60 * 24 * 365.25 //leap years - }, - months: { - match: / (?:(?<months>-?(?:\d+)?\.?\d+) *(?:months?|mon|mo?))/im, - value: 1000 * 60 * 60 * 24 * 30.4375 // average of days in months including leap years + milliseconds: { + match: / (?:(?<milliseconds>-?(?:\d+)?\.?\d+) *(?:milliseconds?|msecs?|ms))/im, + value: 1 }, - weeks: { - match: / (?:(?<weeks>-?(?:\d+)?\.?\d+) *(?:weeks?|w))/im, - value: 1000 * 60 * 60 * 24 * 7 + seconds: { + match: / (?:(?<seconds>-?(?:\d+)?\.?\d+) *(?:seconds?|secs?|s))/im, + value: 1000 }, - days: { - match: / (?:(?<days>-?(?:\d+)?\.?\d+) *(?:days?|d))/im, - value: 1000 * 60 * 60 * 24 + minutes: { + match: / (?:(?<minutes>-?(?:\d+)?\.?\d+) *(?:minutes?|mins?))/im, + value: 1000 * 60 }, hours: { match: / (?:(?<hours>-?(?:\d+)?\.?\d+) *(?:hours?|hrs?|h))/im, value: 1000 * 60 * 60 }, - minutes: { - match: / (?:(?<minutes>-?(?:\d+)?\.?\d+) *(?:minutes?|mins?))/im, - value: 1000 * 60 + days: { + match: / (?:(?<days>-?(?:\d+)?\.?\d+) *(?:days?|d))/im, + value: 1000 * 60 * 60 * 24 }, - seconds: { - match: / (?:(?<seconds>-?(?:\d+)?\.?\d+) *(?:seconds?|secs?|s))/im, - value: 1000 + weeks: { + match: / (?:(?<weeks>-?(?:\d+)?\.?\d+) *(?:weeks?|w))/im, + value: 1000 * 60 * 60 * 24 * 7 }, - milliseconds: { - match: / (?:(?<milliseconds>-?(?:\d+)?\.?\d+) *(?:milliseconds?|msecs?|ms))/im, - value: 1 + months: { + match: / (?:(?<months>-?(?:\d+)?\.?\d+) *(?:months?|mon|mo?))/im, + value: 1000 * 60 * 60 * 24 * 30.4375 // average of days in months including leap years + }, + years: { + match: / (?:(?<years>-?(?:\d+)?\.?\d+) *(?:years?|y))/im, + value: 1000 * 60 * 60 * 24 * 365.25 //leap years } }; |