diff options
Diffstat (limited to 'src/lib/extensions')
-rw-r--r-- | src/lib/extensions/discord-akairo/BushClient.ts | 14 | ||||
-rw-r--r-- | src/lib/extensions/discord-akairo/BushClientUtil.ts | 31 | ||||
-rw-r--r-- | src/lib/extensions/discord.js/BushApplicationCommand.ts | 2 | ||||
-rw-r--r-- | src/lib/extensions/discord.js/BushChannel.d.ts | 7 | ||||
-rw-r--r-- | src/lib/extensions/discord.js/BushGuildMember.ts | 205 |
5 files changed, 123 insertions, 136 deletions
diff --git a/src/lib/extensions/discord-akairo/BushClient.ts b/src/lib/extensions/discord-akairo/BushClient.ts index 4c2b940..8de630f 100644 --- a/src/lib/extensions/discord-akairo/BushClient.ts +++ b/src/lib/extensions/discord-akairo/BushClient.ts @@ -9,6 +9,7 @@ import { MessageEditOptions, MessageOptions, MessagePayload, + PartialDMChannel, ReplyMessageOptions, Snowflake, Structures, @@ -83,6 +84,7 @@ export type BushThreadChannelResolvable = BushThreadChannel | Snowflake; export type BushApplicationCommandResolvable = BushApplicationCommand | Snowflake; export type BushGuildTextChannelResolvable = BushTextChannel | BushNewsChannel | Snowflake; export type BushChannelResolvable = BushChannel | Snowflake; +export type BushTextBasedChannels = PartialDMChannel | BushDMChannel | BushTextChannel | BushNewsChannel | BushThreadChannel; export interface BushFetchedThreads { threads: Collection<Snowflake, BushThreadChannel>; hasMore?: boolean; @@ -96,7 +98,7 @@ const rl = readline.createInterface({ type If<T extends boolean, A, B = null> = T extends true ? A : T extends false ? B : A | B; -export class BushClient<Ready extends boolean = boolean> extends AkairoClient { +export class BushClient<Ready extends boolean = boolean> extends AkairoClient<Ready> { public static preStart(): void { Structures.extend('GuildEmoji', () => BushGuildEmoji); Structures.extend('DMChannel', () => BushDMChannel); @@ -134,7 +136,7 @@ export class BushClient<Ready extends boolean = boolean> extends AkairoClient { public declare util: BushClientUtil; public declare ownerID: Snowflake[]; public db: Sequelize; - public logger: BushLogger; + public logger = BushLogger; public constants = BushConstants; public cache = BushCache; public constructor(config: Config) { @@ -156,7 +158,6 @@ export class BushClient<Ready extends boolean = boolean> extends AkairoClient { this.token = config.token; this.config = config; - // Create listener handler this.listenerHandler = new BushListenerHandler(this, { directory: path.join(__dirname, '..', '..', '..', 'listeners'), @@ -216,10 +217,9 @@ export class BushClient<Ready extends boolean = boolean> extends AkairoClient { port: this.config.db.port, logging: this.config.logging.db ? (sql) => this.logger.debug(sql) : false }); - this.logger = new BushLogger(this); } - get console(): BushLogger { + get console(): typeof BushLogger { return this.logger; } @@ -228,7 +228,7 @@ export class BushClient<Ready extends boolean = boolean> extends AkairoClient { } // Initialize everything - private async _init(): Promise<void> { + async #init(): Promise<void> { this.commandHandler.useListenerHandler(this.listenerHandler); this.commandHandler.useInhibitorHandler(this.inhibitorHandler); this.commandHandler.ignorePermissions = this.config.owners; @@ -296,7 +296,7 @@ export class BushClient<Ready extends boolean = boolean> extends AkairoClient { global.util = this.util; try { - await this._init(); + await this.#init(); await this.login(this.token); } catch (e) { await this.console.error('Start', chalk.red(e?.stack || e), false); diff --git a/src/lib/extensions/discord-akairo/BushClientUtil.ts b/src/lib/extensions/discord-akairo/BushClientUtil.ts index af64624..42d3d13 100644 --- a/src/lib/extensions/discord-akairo/BushClientUtil.ts +++ b/src/lib/extensions/discord-akairo/BushClientUtil.ts @@ -102,7 +102,7 @@ export class BushClientUtil extends ClientUtil { /** The client of this ClientUtil */ public declare readonly client: BushClient; /** The hastebin urls used to post to hastebin, attempts to post in order */ - public hasteURLs: string[] = [ + #hasteURLs: string[] = [ 'https://hst.sh', 'https://hasteb.in', 'https://hastebin.com', @@ -112,7 +112,7 @@ export class BushClientUtil extends ClientUtil { 'https://haste.unbelievaboat.com', 'https://haste.tyman.tech' ]; - public paginateEmojis = { + #paginateEmojis = { beginning: '853667381335162910', back: '853667410203770881', stop: '853667471110570034', @@ -121,7 +121,7 @@ export class BushClientUtil extends ClientUtil { }; /** A simple promise exec method */ - private exec = promisify(exec); + #exec = promisify(exec); /** * Creates this client util @@ -158,7 +158,7 @@ export class BushClientUtil extends ClientUtil { stdout: string; stderr: string; }> { - return await this.exec(command); + return await this.#exec(command); } /** @@ -167,7 +167,7 @@ export class BushClientUtil extends ClientUtil { * @returns The url of the posted text */ public async haste(content: string): Promise<string> { - for (const url of this.hasteURLs) { + for (const url of this.#hasteURLs) { try { const res: hastebinRes = await got.post(`${url}/documents`, { body: content }).json(); return `${url}/${res.key}`; @@ -301,7 +301,7 @@ export class BushClientUtil extends ClientUtil { text: string | null = null, deleteOnExit?: boolean ): Promise<void> { - const paginateEmojis = this.paginateEmojis; + const paginateEmojis = this.#paginateEmojis; if (deleteOnExit === undefined) deleteOnExit = true; if (embeds.length === 1) { @@ -407,7 +407,7 @@ export class BushClientUtil extends ClientUtil { /** Sends a message with a button for the user to delete it. */ public async sendWithDeleteButton(message: BushMessage | BushSlashMessage, options: MessageOptions): Promise<void> { - const paginateEmojis = this.paginateEmojis; + const paginateEmojis = this.#paginateEmojis; updateOptions(); const msg = await message.util.reply(options as MessageOptions & { split?: false }); const filter = (interaction: ButtonInteraction) => interaction.customId == 'paginate__stop' && interaction.message == msg; @@ -465,7 +465,7 @@ export class BushClientUtil extends ClientUtil { return tildes + language + '\n' + code2 + '\n' + tildes + (hasteOut.length ? '\n' + hasteOut : ''); } - private mapCredential(old: string) { + #mapCredential(old: string): string { const mapping = { ['token']: 'Main Token', ['devToken']: 'Dev Token', @@ -481,7 +481,7 @@ export class BushClientUtil extends ClientUtil { public redact(text: string) { for (const credentialName in client.config.credentials) { const credential = client.config.credentials[credentialName]; - const replacement = this.mapCredential(credentialName); + const replacement = this.#mapCredential(credentialName); const escapeRegex = /[.*+?^${}()|[\]\\]/g; text = text.replace(new RegExp(credential.toString().replace(escapeRegex, '\\$&'), 'g'), `[${replacement} Omitted]`); text = text.replace( @@ -702,7 +702,7 @@ export class BushClientUtil extends ClientUtil { const expires = options.duration ? new Date(new Date().getTime() + options.duration) : null; const user = client.users.resolveId(options.user); const guild = client.guilds.resolveId(options.guild); - const type = this.findTypeEnum(options.type); + const type = this.#findTypeEnum(options.type); const entry = options.extraInfo ? ActivePunishment.build({ user, type, guild, expires, modlog: options.modlog, extraInfo: options.extraInfo }) @@ -720,7 +720,7 @@ export class BushClientUtil extends ClientUtil { }): Promise<boolean> { const user = client.users.resolveId(options.user); const guild = client.guilds.resolveId(options.guild); - const type = this.findTypeEnum(options.type); + const type = this.#findTypeEnum(options.type); let success = true; @@ -743,7 +743,7 @@ export class BushClientUtil extends ClientUtil { return success; } - private findTypeEnum(type: 'mute' | 'ban' | 'role' | 'block') { + #findTypeEnum(type: 'mute' | 'ban' | 'role' | 'block') { const typeMap = { ['mute']: ActivePunishmentType.MUTE, ['ban']: ActivePunishmentType.BAN, @@ -915,4 +915,11 @@ export class BushClientUtil extends ClientUtil { return Argument.withInput(type); } })(); + + /** + * Wait an amount in seconds. + */ + public async sleep(s: number): Promise<unknown> { + return new Promise((resolve) => setTimeout(resolve, s * 1000)); + } } diff --git a/src/lib/extensions/discord.js/BushApplicationCommand.ts b/src/lib/extensions/discord.js/BushApplicationCommand.ts index 2acf7f8..171e2ad 100644 --- a/src/lib/extensions/discord.js/BushApplicationCommand.ts +++ b/src/lib/extensions/discord.js/BushApplicationCommand.ts @@ -4,7 +4,7 @@ import { ApplicationCommand, Snowflake } from 'discord.js'; import { BushClient } from '../discord-akairo/BushClient'; import { BushGuild } from './BushGuild'; -export class BushApplicationCommand<PermissionsFetchType = {}> extends ApplicationCommand { +export class BushApplicationCommand<PermissionsFetchType = {}> extends ApplicationCommand<PermissionsFetchType> { public declare readonly client: BushClient; public declare guild: BushGuild | null; diff --git a/src/lib/extensions/discord.js/BushChannel.d.ts b/src/lib/extensions/discord.js/BushChannel.d.ts index 482dd9b..5aa3422 100644 --- a/src/lib/extensions/discord.js/BushChannel.d.ts +++ b/src/lib/extensions/discord.js/BushChannel.d.ts @@ -1,10 +1,7 @@ import { Snowflake } from 'discord-api-types'; import { Channel, ChannelMention } from 'discord.js'; import { ChannelTypes } from 'discord.js/typings/enums'; -import { BushClient } from '../discord-akairo/BushClient'; -import { BushDMChannel } from './BushDMChannel'; -import { BushNewsChannel } from './BushNewsChannel'; -import { BushTextChannel } from './BushTextChannel'; +import { BushClient, BushTextBasedChannels } from '../discord-akairo/BushClient'; import { BushThreadChannel } from './BushThreadChannel'; export class BushChannel extends Channel { @@ -17,7 +14,7 @@ export class BushChannel extends Channel { public type: keyof typeof ChannelTypes; public delete(): Promise<BushChannel>; public fetch(force?: boolean): Promise<BushChannel>; - public isText(): this is BushTextChannel | BushDMChannel | BushNewsChannel | BushThreadChannel; + public isText(): this is BushTextBasedChannels; public isThread(): this is BushThreadChannel; public toString(): ChannelMention; } diff --git a/src/lib/extensions/discord.js/BushGuildMember.ts b/src/lib/extensions/discord.js/BushGuildMember.ts index 142b766..ec0e323 100644 --- a/src/lib/extensions/discord.js/BushGuildMember.ts +++ b/src/lib/extensions/discord.js/BushGuildMember.ts @@ -82,21 +82,16 @@ export class BushGuildMember extends GuildMember { public async warn(options: BushPunishmentOptions): Promise<{ result: WarnResponse; caseNum: number }> { const moderator = client.users.cache.get(client.users.resolveId(options.moderator || client.user)); // add modlog entry - const result = await util - .createModLogEntry( - { - type: ModLogType.WARN, - user: this, - moderator: moderator.id, - reason: options.reason, - guild: this.guild - }, - true - ) - .catch((e) => { - void client.console.error('warn', e, true, 1); - return { log: null, caseNum: null }; - }); + const result = await util.createModLogEntry( + { + type: ModLogType.WARN, + user: this, + moderator: moderator.id, + reason: options.reason, + guild: this.guild + }, + true + ); if (!result || !result.log) return { result: 'error creating modlog entry', caseNum: null }; // dm user @@ -113,33 +108,31 @@ export class BushGuildMember extends GuildMember { } public async addRole(options: AddRoleOptions): Promise<AddRoleResponse> { - const ifShouldAddRole = this.checkIfShouldAddRole(options.role); + const ifShouldAddRole = this.#checkIfShouldAddRole(options.role); if (ifShouldAddRole !== true) return ifShouldAddRole; const moderator = client.users.cache.get(client.users.resolveId(options.moderator || client.user)); if (options.addToModlog) { - const { log: modlog } = await util - .createModLogEntry({ - type: options.duration ? ModLogType.TEMP_PUNISHMENT_ROLE : ModLogType.PERM_PUNISHMENT_ROLE, - guild: this.guild, - moderator: moderator.id, - user: this, - reason: 'N/A' - }) - .catch(() => null); + const { log: modlog } = await util.createModLogEntry({ + type: options.duration ? ModLogType.TEMP_PUNISHMENT_ROLE : ModLogType.PERM_PUNISHMENT_ROLE, + guild: this.guild, + moderator: moderator.id, + user: this, + reason: 'N/A' + }); + if (!modlog) return 'error creating modlog entry'; - const punishmentEntrySuccess = await util - .createPunishmentEntry({ - type: 'role', - user: this, - guild: this.guild, - duration: options.duration, - modlog: modlog.id, - extraInfo: options.role.id - }) - .catch(() => null); + const punishmentEntrySuccess = await util.createPunishmentEntry({ + type: 'role', + user: this, + guild: this.guild, + duration: options.duration, + modlog: modlog.id, + extraInfo: options.role.id + }); + if (!punishmentEntrySuccess) return 'error creating role entry'; } @@ -150,30 +143,28 @@ export class BushGuildMember extends GuildMember { } public async removeRole(options: RemoveRoleOptions): Promise<RemoveRoleResponse> { - const ifShouldAddRole = this.checkIfShouldAddRole(options.role); + const ifShouldAddRole = this.#checkIfShouldAddRole(options.role); if (ifShouldAddRole !== true) return ifShouldAddRole; const moderator = client.users.cache.get(client.users.resolveId(options.moderator || client.user)); if (options.addToModlog) { - const { log: modlog } = await util - .createModLogEntry({ - type: ModLogType.PERM_PUNISHMENT_ROLE, - guild: this.guild, - moderator: moderator.id, - user: this, - reason: 'N/A' - }) - .catch(() => null); + const { log: modlog } = await util.createModLogEntry({ + type: ModLogType.PERM_PUNISHMENT_ROLE, + guild: this.guild, + moderator: moderator.id, + user: this, + reason: 'N/A' + }); + if (!modlog) return 'error creating modlog entry'; - const punishmentEntrySuccess = await util - .removePunishmentEntry({ - type: 'role', - user: this, - guild: this.guild - }) - .catch(() => null); + const punishmentEntrySuccess = await util.removePunishmentEntry({ + type: 'role', + user: this, + guild: this.guild + }); + if (!punishmentEntrySuccess) return 'error removing role entry'; } @@ -183,13 +174,13 @@ export class BushGuildMember extends GuildMember { return 'success'; } - private checkIfShouldAddRole(role: BushRole | Role) { + #checkIfShouldAddRole(role: BushRole | Role): true | 'user hierarchy' | 'role managed' | 'client hierarchy' { if (this.roles.highest.position <= role.position) { - return `user hierarchy`; + return 'user hierarchy'; } else if (role.managed) { - return `role managed`; + return 'role managed'; } else if (this.guild.me.roles.highest.position <= role.position) { - return `client hierarchy`; + return 'client hierarchy'; } return true; } @@ -212,28 +203,26 @@ export class BushGuildMember extends GuildMember { if (!muteSuccess) return 'error giving mute role'; // add modlog entry - const { log: modlog } = await util - .createModLogEntry({ - type: options.duration ? ModLogType.TEMP_MUTE : ModLogType.PERM_MUTE, - user: this, - moderator: moderator.id, - reason: options.reason, - duration: options.duration, - guild: this.guild - }) - .catch(() => null); + const { log: modlog } = await util.createModLogEntry({ + type: options.duration ? ModLogType.TEMP_MUTE : ModLogType.PERM_MUTE, + user: this, + moderator: moderator.id, + reason: options.reason, + duration: options.duration, + guild: this.guild + }); + if (!modlog) return 'error creating modlog entry'; // add punishment entry so they can be unmuted later - const punishmentEntrySuccess = await util - .createPunishmentEntry({ - type: 'mute', - user: this, - guild: this.guild, - duration: options.duration, - modlog: modlog.id - }) - .catch(() => null); + const punishmentEntrySuccess = await util.createPunishmentEntry({ + type: 'mute', + user: this, + guild: this.guild, + duration: options.duration, + modlog: modlog.id + }); + if (!punishmentEntrySuccess) return 'error creating mute entry'; // dm user @@ -242,7 +231,7 @@ export class BushGuildMember extends GuildMember { content: `You have been muted ${ options.duration ? 'for ' + util.humanizeDuration(options.duration) : 'permanently' } in **${this.guild}** for **${options.reason || 'No reason provided'}**.${ending ? `\n\n${ending}` : ''}` - }).catch(() => null); + }); if (!dmSuccess) return 'failed to dm'; @@ -267,25 +256,23 @@ export class BushGuildMember extends GuildMember { if (!muteSuccess) return 'error removing mute role'; //remove modlog entry - const { log: modlog } = await util - .createModLogEntry({ - type: ModLogType.UNMUTE, - user: this, - moderator: moderator.id, - reason: options.reason, - guild: this.guild - }) - .catch(() => null); + const { log: modlog } = await util.createModLogEntry({ + type: ModLogType.UNMUTE, + user: this, + moderator: moderator.id, + reason: options.reason, + guild: this.guild + }); + if (!modlog) return 'error creating modlog entry'; // remove mute entry - const removePunishmentEntrySuccess = await util - .removePunishmentEntry({ - type: 'mute', - user: this, - guild: this.guild - }) - .catch(() => null); + const removePunishmentEntrySuccess = await util.removePunishmentEntry({ + type: 'mute', + user: this, + guild: this.guild + }); + if (!removePunishmentEntrySuccess) return 'error removing mute entry'; //dm user @@ -353,28 +340,24 @@ export class BushGuildMember extends GuildMember { if (!banSuccess) return 'error banning'; // add modlog entry - const { log: modlog } = await util - .createModLogEntry({ - type: options.duration ? ModLogType.TEMP_BAN : ModLogType.PERM_BAN, - user: this, - moderator: moderator.id, - reason: options.reason, - duration: options.duration, - guild: this.guild - }) - .catch(() => null); + const { log: modlog } = await util.createModLogEntry({ + type: options.duration ? ModLogType.TEMP_BAN : ModLogType.PERM_BAN, + user: this, + moderator: moderator.id, + reason: options.reason, + duration: options.duration, + guild: this.guild + }); if (!modlog) return 'error creating modlog entry'; // add punishment entry so they can be unbanned later - const punishmentEntrySuccess = await util - .createPunishmentEntry({ - type: 'ban', - user: this, - guild: this.guild, - duration: options.duration, - modlog: modlog.id - }) - .catch(() => null); + const punishmentEntrySuccess = await util.createPunishmentEntry({ + type: 'ban', + user: this, + guild: this.guild, + duration: options.duration, + modlog: modlog.id + }); if (!punishmentEntrySuccess) return 'error creating ban entry'; if (!dmSuccess) return 'failed to dm'; |