2023-03-18 18:02:17 -04:00
const messageChannelTypes = require ( './messageChannelTypes.js' ) ;
2023-03-19 18:02:21 -04:00
const author = require ( './author.js' ) ;
const axios = require ( 'axios' ) ;
const Embed = require ( './embed' ) ;
2023-03-18 18:02:17 -04:00
2023-03-19 18:02:21 -04:00
class Channel {
/** @type {String} */
id ;
2023-03-18 18:02:17 -04:00
2023-03-19 18:02:21 -04:00
/** @type {String} */
# token ;
constructor ( token , id ) {
this . # token = token ;
this . id = id ;
}
/**
* @param {Object} inp
2023-03-20 12:45:20 -04:00
* @returns {Promise<message>}
2023-03-19 18:02:21 -04:00
*/
async send ( inp ) {
return new Promise ( async ( resolve ) => {
const toSend = ( typeof inp == 'string' ) ? inp : inp . content ;
const config = {
headers : {
Authorization : this . # token
}
}
var embds = undefined ;
if ( inp . embeds ) {
embds = [ ] ;
for ( const i of inp . embeds ) {
embds . push ( i . toJSON ( ) ) ;
}
}
const response = await axios . post ( ` https://discord.com/api/channels/ ${ this . id } /messages ` , {
content : toSend ,
message _reference : inp . message _reference || undefined ,
embeds : embds
} , config ) ;
resolve ( new message ( response . data , this . # token ) ) ;
} ) ;
}
}
class message {
/** @type {author} */
2023-03-18 18:02:17 -04:00
author ;
/** @type {Object} */
channel _id ;
/** @type {Object[]} */
mentions ;
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-03-18 18:02:17 -04:00
/** @type {String} */
guild _id ;
/** @type {Object} */
type ;
2023-03-19 18:02:21 -04:00
/** @type {Channel} */
channel
/** @type {String} */
# token ;
/**
* @param {String} content
* @returns {Promise<message>}
*/
reply ( inp ) {
return new Promise ( async ( resolve , reject ) => {
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
}
const toSend = ( typeof inp == 'string' ) ? { content : inp } : inp ;
toSend [ 'message_reference' ] = refObj ;
resolve ( await this . channel . send ( toSend ) ) ;
} ) ;
}
delete ( ) {
return new Promise ( async ( resolve , reject ) => {
try {
const config = {
headers : {
Authorization : this . # token
}
}
const response = await axios . delete ( ` https://discord.com/api/channels/ ${ this . channel . id } /messages/ ${ this . id } ` , config ) ;
resolve ( response . data ) ;
} catch ( err ) {
reject ( err ) ;
}
} )
}
/**
* @param {{content: String, embeds: [Object]} | String} inp
*/
async edit ( inp ) {
return new Promise ( async ( resolve , reject ) => {
const newMsg = ( typeof inp == "string" ) ? { content : inp } : inp ;
const config = {
headers : {
Authorization : this . # token
}
}
axios . patch ( ` https://discord.com/api/channels/ ${ this . channel . id } /messages/ ${ this . id } ` , newMsg , config ) . then ( ( response ) => {
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-03-19 18:02:21 -04:00
constructor ( msgRaw , token ) {
this . # token = token ;
2023-03-18 18:02:17 -04:00
for ( const k in this ) {
if ( msgRaw [ k ] != undefined ) {
if ( k == 'type' ) {
this . type = ( msgRaw [ 'guild_id' ] ) ? msgRaw [ k ] : 1 ;
2023-03-19 18:02:21 -04:00
}
else if ( k == "author" ) {
this [ k ] = new author ( msgRaw [ k ] ) ;
}
else {
if ( k == 'channel_id' ) {
this . channel = new Channel ( this . # token , msgRaw [ k ] ) ;
}
2023-03-18 18:02:17 -04:00
this [ k ] = msgRaw [ k ] ;
}
}
2023-03-19 18:02:21 -04:00
}
2023-03-18 18:02:17 -04:00
}
}
2023-03-20 12:45:20 -04:00
module . exports = { message , messageChannelTypes , Channel } ;