import { AbstractEntity } from '@src/api/entities/AbstractEntity' ; import { PermissionOverwriteTypes , ToJsonOverrideSymbol } from '@src/constants' ; import { ChannelResolvable , Json , ReadonlyPermissions , ToJsonProperties } from '@src/api' ; import { PermissionOverwriteResolvable } from '@src/api/entities/overwrites/interfaces/PermissionOverwriteResolvable' ; import { DiscordooError , resolveChannelId , resolvePermissionOverwriteToRaw } from '@src/utils' ; import { CacheManagerGetOptions } from '@src/cache' ; import { AnyGuildChannel } from '@src/api/entities/channel/interfaces/AnyGuildChannel' ; import { PermissionOverwriteEditOptions } from '@src/api/managers/overwrites/PermissionOverwriteEditOptions' ; export class PermissionOverwrite extends AbstractEntity { public id ! : string ; public type ! : PermissionOverwriteTypes ; public allow ! : ReadonlyPermissions ; public deny ! : ReadonlyPermissions ; public channelId ! : string ; async init ( data ) { const overwrite = resolvePermissionOverwriteToRaw ( data , this . allow && this . deny ? this : undefined ) ; this . id = overwrite . id ; this . type = overwrite . type ; this . allow = new ReadonlyPermissions ( overwrite . allow ) ; this . deny = new ReadonlyPermissions ( overwrite . deny ) ; if ( data . channel ) { const id = resolveChannelId ( data . channel ) ; if ( ! id && ! this . channelId ) { throw new DiscordooError ( 'PermissionOverwrite' , 'Cannot operate without channel id.' ) ; } if ( id ) this . channelId = id ; } return this ; } channel ( options ? ) { return this . client . channels . cache . get ( this . channelId , options ) as Promise < AnyGuildChannel | undefined > ; } async edit ( overwrite , options ? ) { const result = await this . client . overwrites . upsert ( this . channelId , overwrite , { ... options , existing : this } ) ; return result ? this : undefined ; } async delete ( reason ? ) { const result = await this . client . overwrites . delete ( this . channelId , this . id , reason ) ; return result ? this : undefined ; } toJson ( properties = { } , obj ? ) { return super . toJson ( { ... properties , id : true , type : true , allow : true , deny : true , channel : { override : ToJsonOverrideSymbol , value : this . channelId } , } , obj ) ; } }