diff options
author | IRONM00N <64110067+IRONM00N@users.noreply.github.com> | 2021-12-30 21:36:34 -0500 |
---|---|---|
committer | IRONM00N <64110067+IRONM00N@users.noreply.github.com> | 2021-12-30 21:36:34 -0500 |
commit | 5481971d8dae490a467dd592ca301e72acb2254a (patch) | |
tree | 4fe3c5b5f50b8d0eda04018f8906ec806b1d9b56 /src/lib/extensions | |
parent | 83db032fb91996c926a5d007a9e5fa4abed65871 (diff) | |
download | tanzanite-5481971d8dae490a467dd592ca301e72acb2254a.tar.gz tanzanite-5481971d8dae490a467dd592ca301e72acb2254a.tar.bz2 tanzanite-5481971d8dae490a467dd592ca301e72acb2254a.zip |
A bit of fixing
Diffstat (limited to 'src/lib/extensions')
-rw-r--r-- | src/lib/extensions/discord.js/BushClientEvents.d.ts | 8 | ||||
-rw-r--r-- | src/lib/extensions/discord.js/BushDMChannel.ts | 15 | ||||
-rw-r--r-- | src/lib/extensions/discord.js/BushGuild.ts | 62 | ||||
-rw-r--r-- | src/lib/extensions/discord.js/BushGuildMember.ts | 15 | ||||
-rw-r--r-- | src/lib/extensions/discord.js/BushThreadChannel.ts | 8 | ||||
-rw-r--r-- | src/lib/extensions/discord.js/BushThreadManager.d.ts | 2 |
6 files changed, 75 insertions, 35 deletions
diff --git a/src/lib/extensions/discord.js/BushClientEvents.d.ts b/src/lib/extensions/discord.js/BushClientEvents.d.ts index 6dc94a1..16eccd4 100644 --- a/src/lib/extensions/discord.js/BushClientEvents.d.ts +++ b/src/lib/extensions/discord.js/BushClientEvents.d.ts @@ -306,14 +306,14 @@ export interface BushClientEvents extends AkairoClientEvents { ]; bushLockdown: [ moderator: BushGuildMember, - reason?: string | undefined, - channel?: BushGuildTextBasedChannel, + reason: string | undefined, + channelsSuccessMap: Collection<Snowflake, boolean>, all?: boolean ]; bushUnlockdown: [ moderator: BushGuildMember, - reason?: string | undefined, - channel?: BushGuildTextBasedChannel, + reason: string | undefined, + channelsSuccessMap: Collection<Snowflake, boolean>, all?: boolean ]; } diff --git a/src/lib/extensions/discord.js/BushDMChannel.ts b/src/lib/extensions/discord.js/BushDMChannel.ts index 9df9275..1af3ca1 100644 --- a/src/lib/extensions/discord.js/BushDMChannel.ts +++ b/src/lib/extensions/discord.js/BushDMChannel.ts @@ -1,4 +1,11 @@ -import type { BushClient, BushMessageManager, BushUser } from '#lib'; +import type { + BushBaseGuildVoiceChannel, + BushClient, + BushMessageManager, + BushTextBasedChannel, + BushThreadChannel, + BushUser +} from '#lib'; import { DMChannel } from 'discord.js'; import type { RawDMChannelData } from 'discord.js/typings/rawDataTypes'; @@ -14,3 +21,9 @@ export class BushDMChannel extends DMChannel { super(client, data); } } + +export interface BushDMChannel extends DMChannel { + isText(): this is BushTextBasedChannel; + isVoice(): this is BushBaseGuildVoiceChannel; + isThread(): this is BushThreadChannel; +} diff --git a/src/lib/extensions/discord.js/BushGuild.ts b/src/lib/extensions/discord.js/BushGuild.ts index d182be4..ea8b67e 100644 --- a/src/lib/extensions/discord.js/BushGuild.ts +++ b/src/lib/extensions/discord.js/BushGuild.ts @@ -157,7 +157,7 @@ export class BushGuild extends Guild { * @param options Options for banning the user. * @returns A string status message of the ban. */ - public async bushBan(options: BushBanOptions): Promise<BanResponse> { + public async bushBan(options: GuildBushBanOptions): Promise<GuildBanResponse> { // checks if (!this.me!.permissions.has('BAN_MEMBERS')) return 'missing permissions'; @@ -213,7 +213,7 @@ export class BushGuild extends Guild { * @param options Options for unbanning the user. * @returns A status message of the unban. */ - public async bushUnban(options: BushUnbanOptions): Promise<UnbanResponse> { + public async bushUnban(options: GuildBushUnbanOptions): Promise<GuildUnbanResponse> { let caseID: string | undefined = undefined; let dmSuccessEvent: boolean | undefined = undefined; const user = (await util.resolveNonCachedUser(options.user))!; @@ -291,17 +291,23 @@ export class BushGuild extends Guild { const moderator = this.members.resolve(options.moderator); if (!moderator) return 'moderator not found'; + const errors = new Collection<Snowflake, Error>(); + const success = new Collection<Snowflake, boolean>(); const ret = await (async (): Promise<LockdownResponse> => { - const errors = new Collection<Snowflake, Error>(); - let successCount = 0; - for (const _channel of mappedChannels) { const channel = _channel!; + if (!channel.isText() && !channel.isThread()) { + errors.set(channel.id, new Error('wrong channel type')); + success.set(channel.id, false); + continue; + } if (!channel.permissionsFor(this.me!.id)?.has(['MANAGE_CHANNELS'])) { errors.set(channel.id, new Error('client no permission')); + success.set(channel.id, false); continue; } else if (!channel.permissionsFor(options.moderator)?.has(['MANAGE_CHANNELS'])) { errors.set(channel.id, new Error('moderator no permission')); + success.set(channel.id, false); continue; } @@ -309,26 +315,36 @@ export class BushGuild extends Guild { options.reason ?? 'No reason provided' }`; - if (channel.isThread()) { - const lockdownSuccess = await channel.parent?.permissionOverwrites - .edit(this.id, { SEND_MESSAGES_IN_THREADS: options.unlock ? null : false }, { reason }) - .catch((e) => e); - if (lockdownSuccess instanceof Error) errors.set(channel.id, lockdownSuccess); - else successCount++; + const permissionOverwrites = channel.isThread() ? channel.parent!.permissionOverwrites : channel.permissionOverwrites; + const perms = { [channel.isThread() ? 'SEND_MESSAGES_IN_THREADS' : 'SEND_MESSAGES']: options.unlock ? null : false }; + const permsForMe = { [channel.isThread() ? 'SEND_MESSAGES_IN_THREADS' : 'SEND_MESSAGES']: options.unlock ? null : true }; // so I can send messages in the channel + + const changePermSuccess = await permissionOverwrites.edit(this.id, perms, { reason }).catch((e) => e); + if (changePermSuccess instanceof Error) { + errors.set(channel.id, changePermSuccess); + success.set(channel.id, false); } else { - const lockdownSuccess = await channel.permissionOverwrites - .edit(this.id, { SEND_MESSAGES: options.unlock ? null : false }, { reason }) - .catch((e) => e); - if (lockdownSuccess instanceof Error) errors.set(channel.id, lockdownSuccess); - else successCount++; + success.set(channel.id, true); + await permissionOverwrites.edit(this.me!, permsForMe, { reason }); + await channel.send({ + embeds: [ + { + author: { name: moderator.user.tag, iconURL: moderator.displayAvatarURL({ dynamic: true }) }, + title: `This channel has been ${options.unlock ? 'un' : ''}locked`, + description: options.reason ?? 'No reason provided', + color: options.unlock ? util.colors.discord.GREEN : util.colors.discord.RED, + timestamp: Date.now() + } + ] + }); } } if (errors.size) return errors; - else return `success: ${successCount}`; + else return `success: ${success.filter((c) => c === true).size}`; })(); - client.emit(options.unlock ? 'bushUnlockdown' : 'bushLockdown', moderator, options.reason, options.channel); + client.emit(options.unlock ? 'bushUnlockdown' : 'bushLockdown', moderator, options.reason, success, options.all); return ret; } } @@ -336,7 +352,7 @@ export class BushGuild extends Guild { /** * Options for unbanning a user */ -export interface BushUnbanOptions { +export interface GuildBushUnbanOptions { /** * The user to unban */ @@ -356,7 +372,7 @@ export interface BushUnbanOptions { /** * Options for banning a user */ -export interface BushBanOptions { +export interface GuildBushBanOptions { /** * The user to ban */ @@ -388,17 +404,17 @@ export interface BushBanOptions { evidence?: string; } -export type PunishmentResponse = 'success' | 'missing permissions' | 'error creating modlog entry'; +export type GuildPunishmentResponse = 'success' | 'missing permissions' | 'error creating modlog entry'; /** * Response returned when banning a user */ -export type BanResponse = PunishmentResponse | 'error banning' | 'error creating ban entry'; +export type GuildBanResponse = GuildPunishmentResponse | 'error banning' | 'error creating ban entry'; /** * Response returned when unbanning a user */ -export type UnbanResponse = PunishmentResponse | 'user not banned' | 'error unbanning' | 'error removing ban entry'; +export type GuildUnbanResponse = GuildPunishmentResponse | 'user not banned' | 'error unbanning' | 'error removing ban entry'; /** * Options for locking down channel(s) diff --git a/src/lib/extensions/discord.js/BushGuildMember.ts b/src/lib/extensions/discord.js/BushGuildMember.ts index ffca507..e7df926 100644 --- a/src/lib/extensions/discord.js/BushGuildMember.ts +++ b/src/lib/extensions/discord.js/BushGuildMember.ts @@ -1,14 +1,14 @@ /* eslint-disable @typescript-eslint/no-unused-vars */ import { BushClientEvents, - BushGuildTextBasedChannel, - BushGuildTextChannelResolvable, - BushThreadChannelResolvable, Moderation, ModLogType, type BushClient, type BushGuild, + type BushGuildTextBasedChannel, + type BushGuildTextChannelResolvable, type BushRole, + type BushThreadChannelResolvable, type BushUser } from '#lib'; import { GuildMember, MessageEmbed, type Partialize, type Role } from 'discord.js'; @@ -44,7 +44,8 @@ export class BushGuildMember extends GuildMember { const dmSuccess = await this.send({ content: `You have been ${punishment} in **${this.guild.name}** ${ duration !== null && duration !== undefined ? (duration ? `for ${util.humanizeDuration(duration)} ` : 'permanently ') : '' - }for **${reason?.trim() ?? 'No reason provided'}**.`, + // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing + }for **${reason?.trim() || 'No reason provided'}**.`, embeds: dmEmbed ? [dmEmbed] : undefined }).catch(() => false); return !!dmSuccess; @@ -559,7 +560,8 @@ export class BushGuildMember extends GuildMember { ? `for ${util.humanizeDuration(options.duration)} ` : 'permanently ' : '' - }for **${options.reason?.trim() ?? 'No reason provided'}**.` + // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing + }for **${options.reason?.trim() || 'No reason provided'}**.` }).catch(() => false); dmSuccessEvent = !!dmSuccess; @@ -633,7 +635,8 @@ export class BushGuildMember extends GuildMember { // dm user const dmSuccess = await this.send({ content: `You have been unblocked from <#${channel.id}> in **${this.guild.name}** for **${ - options.reason?.trim() ?? 'No reason provided' + // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing + options.reason?.trim() || 'No reason provided' }**.` }).catch(() => false); dmSuccessEvent = !!dmSuccess; diff --git a/src/lib/extensions/discord.js/BushThreadChannel.ts b/src/lib/extensions/discord.js/BushThreadChannel.ts index 4310e83..3c8859c 100644 --- a/src/lib/extensions/discord.js/BushThreadChannel.ts +++ b/src/lib/extensions/discord.js/BushThreadChannel.ts @@ -1,9 +1,11 @@ import type { + BushBaseGuildVoiceChannel, BushClient, BushGuild, BushGuildMember, BushMessageManager, BushNewsChannel, + BushTextBasedChannel, BushTextChannel, BushThreadMemberManager } from '#lib'; @@ -25,3 +27,9 @@ export class BushThreadChannel extends ThreadChannel { super(guild, data, client, fromInteraction); } } + +export interface BushThreadChannel extends ThreadChannel { + isText(): this is BushTextBasedChannel; + isVoice(): this is BushBaseGuildVoiceChannel; + isThread(): this is BushThreadChannel; +} diff --git a/src/lib/extensions/discord.js/BushThreadManager.d.ts b/src/lib/extensions/discord.js/BushThreadManager.d.ts index 1366d68..6b3340d 100644 --- a/src/lib/extensions/discord.js/BushThreadManager.d.ts +++ b/src/lib/extensions/discord.js/BushThreadManager.d.ts @@ -64,7 +64,7 @@ export class BushThreadManager<AllowedThreadType> extends CachedManager<Snowflak * .then(channel => console.log(channel.name)) * .catch(console.error); */ - public fetch(options: ThreadChannelResolvable, cacheOptions?: BaseFetchOptions): Promise<ThreadChannel | null>; + public fetch(options: ThreadChannelResolvable, cacheOptions?: BaseFetchOptions): Promise<BushThreadChannel | null>; public fetch(options?: FetchThreadsOptions, cacheOptions?: { cache?: boolean }): Promise<FetchedThreads>; /** |