diff options
author | IRONM00N <64110067+IRONM00N@users.noreply.github.com> | 2022-02-21 16:08:38 -0500 |
---|---|---|
committer | IRONM00N <64110067+IRONM00N@users.noreply.github.com> | 2022-02-21 16:08:38 -0500 |
commit | 5116ccf230c933c290676d033a5639b3913ee03b (patch) | |
tree | 7ebd8bb012b22c016e8d38d79b551cf43155d4dd /src/lib/extensions | |
parent | d3464427ea9b08b54a0444795bf4aedab55d3afc (diff) | |
download | tanzanite-5116ccf230c933c290676d033a5639b3913ee03b.tar.gz tanzanite-5116ccf230c933c290676d033a5639b3913ee03b.tar.bz2 tanzanite-5116ccf230c933c290676d033a5639b3913ee03b.zip |
perf: make massban actually work
Diffstat (limited to 'src/lib/extensions')
-rw-r--r-- | src/lib/extensions/discord-akairo/BushClientUtil.ts | 17 | ||||
-rw-r--r-- | src/lib/extensions/discord.js/BushClientEvents.ts | 7 | ||||
-rw-r--r-- | src/lib/extensions/discord.js/BushGuild.ts | 85 |
3 files changed, 101 insertions, 8 deletions
diff --git a/src/lib/extensions/discord-akairo/BushClientUtil.ts b/src/lib/extensions/discord-akairo/BushClientUtil.ts index ecfa360..9903140 100644 --- a/src/lib/extensions/discord-akairo/BushClientUtil.ts +++ b/src/lib/extensions/discord-akairo/BushClientUtil.ts @@ -678,16 +678,17 @@ export class BushClientUtil extends ClientUtil { */ public async resolveNonCachedUser(user: UserResolvable | undefined | null): Promise<BushUser | undefined> { if (user == null) return undefined; - const id = - user instanceof User || user instanceof GuildMember || user instanceof ThreadMember - ? user.id + const resolvedUser = + user instanceof User + ? <BushUser>user + : user instanceof GuildMember + ? <BushUser>user.user + : user instanceof ThreadMember + ? <BushUser>user.user : user instanceof Message - ? user.author.id - : typeof user === 'string' - ? user + ? <BushUser>user.author : undefined; - if (!id) return undefined; - else return await client.users.fetch(id).catch(() => undefined); + return resolvedUser ?? (await client.users.fetch(user as Snowflake).catch(() => undefined)); } /** diff --git a/src/lib/extensions/discord.js/BushClientEvents.ts b/src/lib/extensions/discord.js/BushClientEvents.ts index 50b198d..e6cf93f 100644 --- a/src/lib/extensions/discord.js/BushClientEvents.ts +++ b/src/lib/extensions/discord.js/BushClientEvents.ts @@ -1,4 +1,5 @@ import type { + BanResponse, BushApplicationCommand, BushClient, BushDMChannel, @@ -264,6 +265,12 @@ export interface BushClientEvents extends AkairoClientEvents { channelsSuccessMap: Collection<Snowflake, boolean>, all?: boolean ]; + massBan: [ + moderator: BushGuildMember, + guild: BushGuild, + reason: string | undefined, + results: Collection<Snowflake, BanResponse> + ]; } type Setting = GuildSettings | 'enabledFeatures' | 'blacklistedChannels' | 'blacklistedUsers' | 'disabledCommands'; diff --git a/src/lib/extensions/discord.js/BushGuild.ts b/src/lib/extensions/discord.js/BushGuild.ts index 80799fd..155f32c 100644 --- a/src/lib/extensions/discord.js/BushGuild.ts +++ b/src/lib/extensions/discord.js/BushGuild.ts @@ -236,6 +236,69 @@ export class BushGuild extends Guild { } /** + * {@link bushBan} with less resolving and checks + * @param options Options for banning the user. + * @returns A string status message of the ban. + * **Preconditions:** + * - {@link me} has the `BanMembers` permission + * **Warning:** + * - Doesn't emit bushBan Event + */ + public async massBanOne(options: GuildMassBanOneOptions): Promise<BanResponse> { + if (this.bans.cache.has(options.user)) return banResponse.ALREADY_BANNED; + + const ret = await (async () => { + // add modlog entry + const { log: modlog } = await Moderation.createModLogEntrySimple({ + type: ModLogType.PERM_BAN, + user: options.user, + moderator: options.moderator, + reason: options.reason, + duration: 0, + guild: this.id + }); + if (!modlog) return banResponse.MODLOG_ERROR; + + let dmSuccessEvent: boolean | undefined = undefined; + // dm user + if (this.members.cache.has(options.user)) { + dmSuccessEvent = await Moderation.punishDM({ + modlog: modlog.id, + guild: this, + user: options.user, + punishment: 'banned', + duration: 0, + reason: options.reason ?? undefined, + sendFooter: true + }); + } + + // ban + const banSuccess = await this.bans + .create(options.user, { + reason: `${options.moderator} | ${options.reason}`, + deleteMessageDays: options.deleteDays + }) + .catch(() => false); + if (!banSuccess) return banResponse.ACTION_ERROR; + + // add punishment entry so they can be unbanned later + const punishmentEntrySuccess = await Moderation.createPunishmentEntry({ + type: 'ban', + user: options.user, + guild: this, + duration: 0, + modlog: modlog.id + }); + if (!punishmentEntrySuccess) return banResponse.PUNISHMENT_ENTRY_ADD_ERROR; + + if (!dmSuccessEvent) return banResponse.DM_ERROR; + return banResponse.SUCCESS; + })(); + return ret; + } + + /** * Unbans a user, dms them, creates a mod log entry, and destroys the punishment entry. * @param options Options for unbanning the user. * @returns A status message of the unban. @@ -414,6 +477,28 @@ export interface GuildBushUnbanOptions { evidence?: string; } +export interface GuildMassBanOneOptions { + /** + * The user to ban + */ + user: Snowflake; + + /** + * The reason to ban the user + */ + reason: string; + + /** + * The moderator who banned the user + */ + moderator: Snowflake; + + /** + * The number of days to delete the user's messages for + */ + deleteDays?: number; +} + /** * Options for banning a user */ |