import { AbstractEmoji } from '@src/api/entities/emoji/AbstractEmoji' ; import { AbstractGuildEmoji } from '@src/api/entities/emoji/interfaces/AbstractGuildEmoji' ; import { GuildEmojiData } from '@src/api/entities/emoji/interfaces/GuildEmojiData' ; import { User } from '@src/api/entities/user' ; import { resolveRoleId , resolveUserId } from '@src/utils/resolve' ; import { DiscordooError , attach } from '@src/utils' ; import { RawGuildEmojiData } from '@src/api/entities/emoji/interfaces/RawGuildEmojiData' ; import { GuildEmojiEditData } from '@src/api/entities/emoji/interfaces/GuildEmojiEditData' ; import { RoleResolvable } from '@src/api/entities/role' ; import { ToJsonProperties } from '@src/api/entities/interfaces/ToJsonProperties' ; import { Json } from '@src/api/entities/interfaces/Json' ; import { EntitiesUtil } from '@src/api/entities/EntitiesUtil' ; import { CacheManagerGetOptions } from '@src/cache' ; import { filterAndMap } from '@src/utils/filterAndMap' ; export class GuildEmoji extends AbstractEmoji implements AbstractGuildEmoji { public available ! : boolean ; public guildId ! : string ; public managed ! : boolean ; public requiresColons ! : boolean ; public roles : string [ ] = [ ] ; public userId ? : string ; public deleted = false ; async init ( data ) { await super . init ( data ) ; attach ( this , data , [ [ 'available' , '' , false ] , [ 'managed' , '' , false ] , [ 'requiresColons' , 'requires_colons' , false ] , [ 'guildId' , 'guild_id' ] , [ 'userId' , 'user_id' ] , [ 'deleted' , '' , false ] , ] ) ; if ( data . roles ) { this . roles = [ ] ; for ( const role of data . roles ) { const id = resolveRoleId ( role ) ; if ( id ) this . roles . push ( id ) ; } } if ( data . user ) { this . userId = resolveUserId ( data . user ) ; } return this ; } guild ( options ? ) { return this . client . guilds . cache . get ( this . guildId , options ) ; } async user ( options ? ) { return this . userId !== undefined ? this . client . users . cache . get ( this . userId , options ) : undefined ; } async fetchUser ( ) { if ( this . managed ) return undefined ; if ( ! this . id ) return undefined ; const response = await this . client . internals . actions . getGuildEmoji ( this . guildId , this . id ) ; const User = EntitiesUtil . get ( 'User' ) ; if ( response . success ) { await this . init ( response . result ) ; return new User ( this . client ) . init ( response . result . user ) ; } return undefined ; } async edit ( data , reason ? ) { if ( ! this . guildId && ! data . guildId ) throw new DiscordooError ( 'Emoji' , 'Cannot edit emoji without guild id' ) ; if ( ! this . id && ! data . id ) throw new DiscordooError ( 'Emoji' , 'Cannot edit emoji without id' ) ; if ( this . name === undefined ) throw new DiscordooError ( 'Emoji' , 'Cannot edit emoji without name' ) ; const roles = filterAndMap < RoleResolvable , string > ( data . roles ?? [ ] , ( r ) => resolveRoleId ( r ) !== undefined , ( r ) => resolveRoleId ( r ) ) ; const response = await this . client . internals . actions . editGuildEmoji ( ( this . guildId ?? data . guildId ) ! , ( this . id ?? data . id ) ! , { roles , name : data . name ?? this . name } , reason ) ; if ( response . success ) { await this . init ( response . result ) ; } return this ; } setName ( name , reason ? ) { return this . edit ( { name } , reason ) ; } setRoles ( roles , reason ? ) { return this . edit ( { roles } , reason ) ; } async delete ( reason ? ) { if ( ! this . guildId ) throw new DiscordooError ( 'Emoji' , 'Cannot delete emoji without guild id' ) ; if ( ! this . id ) throw new DiscordooError ( 'Emoji' , 'Cannot delete emoji without id' ) ; const response = await this . client . internals . actions . deleteGuildEmoji ( this . guildId , this . id , reason ) ; if ( response . success ) { this . deleted = true ; } return this ; } toJson ( properties , obj ? ) { return super . toJson ( { ... properties , available : true , guildId : true , managed : true , requiresColons : true , roles : true , userId : true , deleted : true , } , obj ) ; } }