2023-04-02 09:49:47 -04:00
import author from './User.js' ;
2023-04-01 21:49:27 -04:00
import axios from 'axios' ;
2023-04-04 22:06:12 -04:00
import { Channel } from '../guilds/Channel.js' ;
2023-04-07 19:13:15 -04:00
import Guild from '../guilds/Guild.js' ;
2023-04-07 20:35:03 -04:00
import { DataManager } from '../DataManager.js' ;
2023-04-08 16:33:58 -04:00
import { MessageActionRow } from './MessageActionRow.js' ;
2023-03-19 18:02:21 -04:00
2023-04-07 20:35:03 -04:00
export class message extends DataManager {
2023-03-19 18:02:21 -04:00
/** @type {author} */
2023-03-18 18:02:17 -04:00
author ;
2023-04-01 21:49:27 -04:00
/** @type {String} */
2023-03-18 18:02:17 -04:00
channel _id ;
/** @type {Object[]} */
mentions ;
2024-05-16 11:06:50 -07:00
2023-03-19 18:02:21 -04:00
/** @type {Object[]} */
mention _roles ;
/** @type {Boolean} */
mention _everyone ;
/** @type {message} */
referenced _message ;
/** @type {String} */
timestamp ;
/** @type {Boolean} */
pinned ;
2023-03-18 18:02:17 -04:00
/** @type {Object} */
member ;
/** @type {String} */
id ;
/** @type {Object} */
content ;
/** @type {Object[]} */
attachments ;
2023-03-19 18:02:21 -04:00
/** @type {Object[]} */
embeds ;
2023-04-01 21:49:27 -04:00
/** @type {Guild} */
guild ;
2023-03-18 18:02:17 -04:00
/** @type {String} */
guild _id ;
/** @type {Object} */
type ;
2023-03-19 18:02:21 -04:00
/** @type {Channel} */
2023-04-01 21:49:27 -04:00
channel ;
2023-03-19 18:02:21 -04:00
2023-04-08 16:33:58 -04:00
/** @type {MessageActionRow[]} */
components ;
2023-03-19 18:02:21 -04:00
/**
2023-04-08 16:33:58 -04:00
* @param {MessageActionRow} ar
*/
addComponents ( ar ) {
if ( this . components . length > 5 ) throw "MAXIMUM SIZE REACHED (5)" ;
this . components . push ( ar ) ;
}
/**
* @param {String | message} content
2023-03-19 18:02:21 -04:00
* @returns {Promise<message>}
*/
reply ( inp ) {
return new Promise ( async ( resolve , reject ) => {
2023-04-08 16:33:58 -04:00
try {
const refObj = {
message _id : this . id ,
channel _id : this . channel . id ,
guild _id : this . guild _id ,
fail _if _not _exists : false //when sending, whether to error if the referenced message doesn't exist instead of sending as a normal (non-reply) message, default true
}
2024-05-16 11:06:50 -07:00
const toSend = ( typeof inp == 'string' ) ? { content : inp } : structuredClone ( inp ) ;
2023-04-08 16:33:58 -04:00
toSend [ 'message_reference' ] = refObj ;
2024-05-16 11:06:50 -07:00
2023-04-08 16:33:58 -04:00
if ( inp . components ) {
toSend [ "components" ] = [ ] ;
for ( const k of inp . components ) {
toSend [ "components" ] . push ( k . toObj ( ) ) ;
}
}
2023-03-19 18:02:21 -04:00
2023-04-08 16:33:58 -04:00
resolve ( await this . channel . send ( toSend ) ) ;
2024-05-16 11:06:50 -07:00
} catch ( err ) {
2023-04-08 16:33:58 -04:00
throw err ;
}
2023-03-19 18:02:21 -04:00
} ) ;
}
2024-05-16 11:06:50 -07:00
async delete ( ) {
try {
const response = await this . client . axiosCustom . delete ( ` /channels/ ${ this . channel . id } /messages/ ${ this . id } ` ) ;
return response . data ;
} catch ( err ) {
throw err ;
}
}
/**
* @param {string} reaction
*/
async react ( reaction ) {
this . axiosCustom . put ( ` /channels/ ${ this . channel . id } /messages/ ${ this . id } /reactions/ ${ reaction } /@me ` ) ;
2023-03-19 18:02:21 -04:00
}
/**
* @param {{content: String, embeds: [Object]} | String} inp
*/
async edit ( inp ) {
return new Promise ( async ( resolve , reject ) => {
2024-05-16 11:06:50 -07:00
const newMsg = ( typeof inp == "string" ) ? { content : inp } : inp ;
2023-04-08 16:33:58 -04:00
if ( this . components ) {
newMsg . components = [ ] ;
for ( const k of this . components ) {
newMsg . components . push ( k . toObj ( ) ) ;
}
}
2024-05-16 11:06:50 -07:00
2023-04-07 20:12:16 -04:00
this . client . axiosCustom . patch ( ` /channels/ ${ this . channel . id } /messages/ ${ this . id } ` , newMsg ) . then ( ( response ) => {
2023-03-19 18:02:21 -04:00
resolve ( response . data ) ;
} ) . catch ( ( err ) => {
reject ( ` REQUEST FAILED WITH CODE ${ err . response . data . code } AND THE FOLLOWING REASON: \n ${ err . response . data . message } ` ) ;
} ) ;
} ) ;
}
2023-03-18 18:02:17 -04:00
/**
* @param {Object} msgRaw
*/
2023-04-07 20:12:16 -04:00
constructor ( msgRaw , client , guild ) {
super ( client ) ;
2023-04-02 09:49:47 -04:00
this . guild = guild ;
2023-03-19 18:02:21 -04:00
2023-04-08 16:33:58 -04:00
for ( const k in msgRaw ) {
if ( k == 'type' ) {
this . type = ( msgRaw [ 'guild_id' ] ) ? msgRaw [ k ] : 1 ;
}
else if ( k == "author" ) {
this [ k ] = new author ( msgRaw [ k ] ) ;
}
else {
if ( k == 'channel_id' ) {
2024-05-16 11:06:50 -07:00
this . channel = new Channel ( { id : msgRaw [ k ] } , this . guild , client ) ;
2023-03-19 18:02:21 -04:00
}
2023-04-08 16:33:58 -04:00
this [ k ] = msgRaw [ k ] ;
2023-03-18 18:02:17 -04:00
}
2023-03-19 18:02:21 -04:00
}
2023-04-08 16:33:58 -04:00
if ( this . components == undefined ) this . components = [ ] ;
2023-03-18 18:02:17 -04:00
}
}
2024-05-16 11:06:50 -07:00
export { messageChannelTypes } from './messageChannelTypes.js' ;