diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/arguments/roleWithDuation.ts | 8 | ||||
-rw-r--r-- | src/commands/moderation/role.ts | 15 | ||||
-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 | ||||
-rw-r--r-- | src/listeners/commands/commandError.ts | 15 | ||||
-rw-r--r-- | src/listeners/message/level.ts | 2 | ||||
-rw-r--r-- | src/listeners/other/consoleListener.ts | 2 |
10 files changed, 91 insertions, 86 deletions
diff --git a/src/arguments/roleWithDuation.ts b/src/arguments/roleWithDuation.ts index de83ccf..03a6035 100644 --- a/src/arguments/roleWithDuation.ts +++ b/src/arguments/roleWithDuation.ts @@ -7,8 +7,12 @@ export const roleWithDurationTypeCaster: BushArgumentTypeCaster = async ( // eslint-disable-next-line prefer-const let { duration, contentWithoutTime } = client.util.parseDuration(phrase); if (contentWithoutTime === null || contentWithoutTime === undefined) return null; - if (contentWithoutTime[0] === ' ') contentWithoutTime = contentWithoutTime.replace(' ', ''); + contentWithoutTime = contentWithoutTime.trim(); const role = await util.arg.cast('role', client.commandHandler.resolver, message, contentWithoutTime); - if (!role) return null; + if (!role) { + client.console.debug(contentWithoutTime); + client.console.debug(duration); + return null; + } return { duration, role }; }; diff --git a/src/commands/moderation/role.ts b/src/commands/moderation/role.ts index ff0e049..15af014 100644 --- a/src/commands/moderation/role.ts +++ b/src/commands/moderation/role.ts @@ -65,10 +65,7 @@ export default class RoleCommand extends BushCommand { type: [['add'], ['remove']], prompt: { start: 'Would you like to `add` or `remove` a role?', - retry: (...arg) => { - console.debug(...arg); - return '{error} Choose whether you would you like to `add` or `remove` a role.'; - } + retry: '{error} Choose whether you would you like to `add` or `remove` a role.' } }; @@ -77,10 +74,7 @@ export default class RoleCommand extends BushCommand { type: 'member', prompt: { start: `What user do you want to ${action} the role ${action === 'add' ? 'to' : 'from'}?`, - retry: (...arg) => { - console.debug(...arg); - return `{error} Choose a valid user to ${action} the role ${action === 'add' ? 'to' : 'from'}.`; - } + retry: `{error} Choose a valid user to ${action} the role ${action === 'add' ? 'to' : 'from'}.` } }; @@ -92,10 +86,7 @@ export default class RoleCommand extends BushCommand { start: `What role do you want to ${action} ${action === 'add' ? 'to' : 'from'} the user${ action === 'add' ? ', and for how long' : '' }?`, - retry: (...arg) => { - console.debug(...arg); - return `{error} Choose a valid role to ${action}.`; - } + retry: `{error} Choose a valid role to ${action}.` } }; 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 } }; diff --git a/src/listeners/commands/commandError.ts b/src/listeners/commands/commandError.ts index 1cf7c08..d24c0f0 100644 --- a/src/listeners/commands/commandError.ts +++ b/src/listeners/commands/commandError.ts @@ -1,6 +1,7 @@ -import { BushCommandHandlerEvents, BushListener } from '@lib'; import { AkairoMessage, Command, GuildTextBasedChannels } from 'discord-akairo'; -import { DMChannel, Message, MessageEmbed } from 'discord.js'; +import { DMChannel, Formatters, Message, MessageEmbed } from 'discord.js'; +import { BushCommandHandlerEvents } from '../../lib/extensions/discord-akairo/BushCommandHandler'; +import { BushListener } from '../../lib/extensions/discord-akairo/BushListener'; export default class CommandErrorListener extends BushListener { public constructor() { @@ -70,7 +71,7 @@ export default class CommandErrorListener extends BushListener { command?: Command; channel?: string; } - | { error: Error | any; type: 'uncaughtException' | 'unhandledRejection' } + | { error: Error | any; type: 'uncaughtException' | 'unhandledRejection'; context?: string } ): Promise<MessageEmbed> { const embed = new MessageEmbed().setColor(util.colors.error).setTimestamp(); if (options.type === 'command-user') { @@ -128,8 +129,12 @@ export default class CommandErrorListener extends BushListener { if (options.type === 'command-dev' || options.type === 'command-log') embed.setTitle(`${options.isSlash ? 'Slash ' : ''}CommandError #\`${options.errorNum}\``); - else if (options.type === 'uncaughtException') embed.setTitle('Uncaught Exception'); - else if (options.type === 'unhandledRejection') embed.setTitle('Unhandled Promise Rejection'); + else if (options.type === 'uncaughtException') + embed.setTitle(`${options.context ? `[${Formatters.bold(options.context)}] An Error Occurred` : 'Uncaught Exception'}`); + else if (options.type === 'unhandledRejection') + embed.setTitle( + `${options.context ? `[${Formatters.bold(options.context)}] An Error Occurred` : 'Unhandled Promise Rejection'}` + ); return embed; } } diff --git a/src/listeners/message/level.ts b/src/listeners/message/level.ts index 7722db7..e567932 100644 --- a/src/listeners/message/level.ts +++ b/src/listeners/message/level.ts @@ -30,7 +30,7 @@ export default class LevelListener extends BushListener { }); const xpToGive = Level.genRandomizedXp(); const success = await user.increment('xp', { by: xpToGive }).catch((e) => { - void client.logger.error('level', e?.stack || e); + void util.handleError('level', e); return false; }); if (success) diff --git a/src/listeners/other/consoleListener.ts b/src/listeners/other/consoleListener.ts index 4b3a6bd..c983a58 100644 --- a/src/listeners/other/consoleListener.ts +++ b/src/listeners/other/consoleListener.ts @@ -41,7 +41,7 @@ export default class ConsoleListener extends BushListener { output = await output; console.log(output); } catch (e) { - console.error(e?.stack || e); + console.error(e); } } else if (line.startsWith('stop')) { process.exit(0); |