import { AbstractEntity } from '@src/api/entities/AbstractEntity' ; import { ToJsonProperties } from '@src/api/entities/interfaces/ToJsonProperties' ; import { Json } from '@src/api/entities/interfaces/Json' ; import { GuildMember , MessageAttachment , MessageContent , MessageCreateOptions , MessageData , MessageEmbed , RawMessageData , ReadonlyMessageFlagsUtil , User } from '@src/api' ; import { MessageReactionsManager } from '@src/api/managers/reactions' ; import { Keyspaces , MessageTypes } from '@src/constants' ; import { attach , resolveMessageReaction } from '@src/utils' ; import { AnyWritableChannel } from '@src/api/entities/channel/interfaces/AnyWritableChannel' ; import { CacheManagerGetOptions } from '@src/cache' ; import { EntitiesUtil } from '@src/api/entities/EntitiesUtil' ; export class Message extends AbstractEntity { public attachments ! : MessageAttachment [ ] ; public authorId ! : string ; public channelId ! : string ; public content ! : string ; public deleted ! : boolean ; public editedTimestamp ? : number ; public embeds ! : MessageEmbed [ ] ; public flags ! : ReadonlyMessageFlagsUtil ; public guildId ? : string ; public id ! : string ; public nonce ! : string | number ; public pinned ! : boolean ; public reactions ! : MessageReactionsManager ; public referencedMessageId ? : string ; public createdTimestamp ! : number ; public tts ! : boolean ; public type ! : MessageTypes ; public webhookId ! : string ; async init ( data ) { attach ( this , data , [ [ 'channelId' , 'channel_id' ] , [ 'guildId' , 'guild_id' ] , 'content' , 'id' , 'nonce' , 'pinned' , 'tts' , 'type' , 'deleted' , [ 'webhookId' , 'webhook_id' ] , [ 'createdTimestamp' , 'created_timestamp' ] , [ 'editedTimestamp' , 'edited_timestamp' ] , ] ) ; if ( data . embeds ?. length ) { this . embeds = data . embeds . map ( v => new MessageEmbed ( v ) ) ; } if ( this . createdTimestamp ) { this . createdTimestamp = new Date ( this . createdTimestamp ) . getTime ( ) ; } if ( this . editedTimestamp ) { this . editedTimestamp = new Date ( this . editedTimestamp ) . getTime ( ) ; } if ( 'authorId' in data ) { this . authorId = data . authorId ; } else { this . authorId = data . author . id ; } if ( 'flags' in data ) { this . flags = new ReadonlyMessageFlagsUtil ( data . flags ) ; } else if ( ! this . flags ) { this . flags = new ReadonlyMessageFlagsUtil ( ) ; } if ( data . attachments ?. length ) { this . attachments = data . attachments . map ( v => new MessageAttachment ( v ) ) ; } if ( ! this . reactions ) { this . reactions = new MessageReactionsManager ( this . client , { messageId : this . id , channelId : this . channelId } ) ; } if ( data . reactions ?. length ) { await this . reactions . cache . clear ( ) ; for await ( const reactionData of data . reactions ) { const reaction = await resolveMessageReaction ( this . client , reactionData ) ; if ( reaction ) await this . reactions . cache . set ( reaction . emojiId , reaction ) ; } } if ( 'referenced_message' in data && data . referenced_message ) { const Message = EntitiesUtil . get ( 'Message' ) ; const msg = await new Message ( this . client ) . init ( data . referenced_message ) ; await this . client . messages . cache . set ( msg . id , msg , { storage : this . channelId } ) ; this . referencedMessageId = msg . id ; } if ( typeof this . deleted !== 'boolean' ! ) { this . deleted = false ; } return this ; } reply ( content , options ? ) { return this . client . messages . create ( this . channelId , content , { ... options , messageReference : { guild : this . guildId , channel : this . channelId , message : this . id , } } ) ; } async delete ( reason ? ) { const result = await this . client . messages . deleteOne ( this . channelId , this . id , reason ) ; if ( result ) { this . deleted = true ; return this ; } return undefined ; } get createdDate ( ) : Date { return new Date ( this . createdTimestamp ) ; } get editedDate ( ) : Date | undefined { return this . editedTimestamp ? new Date ( this . editedTimestamp ) : undefined ; } author ( options ? ) { return this . client . users . cache . get ( this . authorId , options ) ; } async guild ( options ? ) { return this . guildId ? this . client . guilds . cache . get ( this . guildId , options ) : undefined ; } async channel ( options ? ) { return this . client . internals . cache . get ( Keyspaces . CHANNELS , this . guildId ?? this . authorId , 'channelEntityKey' , this . channelId , options ) ; } async member ( options ? ) { return this . guildId ? this . client . internals . cache . get ( Keyspaces . GUILD_MEMBERS , this . guildId , 'GuildMember' , this . authorId , options ) : undefined ; } async reference ( options ? ) { return this . referencedMessageId ? this . client . internals . cache . get ( Keyspaces . MESSAGES , this . channelId , 'Message' , this . referencedMessageId , options ) : undefined ; } toJson ( properties = { } , obj ? ) { return super . toJson ( { ... properties , channelId : true , authorId : true , guildId : true , content : true , deleted : true , id : true , nonce : true , pinned : true , tts : true , type : true , webhookId : true , createdTimestamp : true , editedTimestamp : true , embeds : true , flags : true , referencedMessageId : true , } , obj ) ; } }