2021-07-11 21:47:18 -04:00
const { MessageEmbed , Message } = require ( 'discord.js' ) ;
2021-07-16 21:10:49 -04:00
const CommandCategory = require ( '../model/command-category' ) ;
const CommandPermission = require ( '../model/command-permission' ) ;
2021-07-11 21:47:18 -04:00
const RGB _REGEX = /(\d{1,3})\D*,\D*(\d{1,3})\D*,\D*(\d{1,3})\D*/u ;
class EmbedDialog
{
/ * *
* @ param { Message } message
* /
constructor ( message ) {
this . embed = new MessageEmbed ( ) ;
this . message = message ;
this . channel = message . channel ;
this . destinationChannel = null ;
2021-07-11 22:29:11 -04:00
this . prompt = ( question , hideSkip = false ) => {
return this . channel . send ( ` ${ question } \n * ${ ! hideSkip ? '`skip` to skip, ' : '' } \` cancel \` to cancel* ` ) ;
} ;
this . isMessageSkip = message => message . content . toLowerCase ( ) === 'skip' ;
2021-07-11 21:47:18 -04:00
this . messageFilter = testedMessage => {
const byAuthor = testedMessage . author . id === message . author . id ;
const hasContent = testedMessage . cleanContent . trim ( ) . length > 0 ;
return byAuthor && hasContent ;
}
this . channelMessageFilter = testedMessage => {
2021-07-11 22:29:11 -04:00
return this . messageFilter ( testedMessage )
&& ( this . isMessageSkip ( testedMessage ) || testedMessage . mentions . channels . size > 0 ) ;
2021-07-11 21:47:18 -04:00
}
this . returnFirstOfCollection = collection => collection && collection . size ? collection . first ( ) : null ;
this . awaitOptions = { max : 1 , time : 5 * MINUTE , errors : [ 'time' ] } ;
/ * *
* @ param { function } filter
* @ returns { Promise < Message > }
* /
this . awaitMessage = ( filter = this . messageFilter ) => {
return this . channel . awaitMessages (
filter ,
this . awaitOptions
) . then ( async collection => {
let message = this . returnFirstOfCollection ( collection ) ;
if ( message && message . content . toLowerCase ( ) === 'cancel' ) {
message = null ;
await this . channel . send ( 'Cancelling embed creation.' ) ;
}
return message ;
} ) . catch ( async ( ) => {
await this . channel . send ( 'Time out, cancelling embed creation.' ) ;
} ) ;
} ;
}
async execute ( ) {
2021-07-11 22:29:11 -04:00
let confirmation = true ;
await this . prompt ( '#️⃣ In which **channel** would you like this embed to be posted?' ) ;
2021-07-11 21:47:18 -04:00
const channelMessage = await this . awaitMessage ( this . channelMessageFilter ) ;
if ( ! channelMessage ) {
return null ;
}
2021-07-11 22:29:11 -04:00
if ( this . isMessageSkip ( channelMessage ) ) {
this . destinationChannel = this . channel ;
confirmation = false ;
} else {
this . destinationChannel = channelMessage . mentions . channels . first ( ) ;
}
await this . prompt ( '📰 What do you want the **title** of this embed to be (you can also ping someone so it appears as they are saying what is going to be the description)?' ) ;
2021-07-11 21:47:18 -04:00
const titleMessage = await this . awaitMessage ( this . messageFilter ) ;
if ( ! titleMessage ) {
return null ;
}
2021-07-11 22:29:11 -04:00
if ( ! this . isMessageSkip ( titleMessage ) ) {
if ( titleMessage . mentions . members . size > 0 ) {
const member = titleMessage . mentions . members . first ( ) ;
this . embed . setAuthor ( member . displayName , member . user . displayAvatarURL ( { dynamic : true } ) ) ;
} else {
this . embed . setTitle ( titleMessage . content ) ;
}
2021-07-11 21:47:18 -04:00
}
2021-07-11 22:29:11 -04:00
await this . prompt ( '🎨 What do you want the **colour** of this embed to be?' ) ;
2021-07-11 21:47:18 -04:00
const colourMessage = await this . awaitMessage ( this . messageFilter ) ;
if ( ! colourMessage ) {
return null ;
}
2021-07-11 22:29:11 -04:00
if ( this . isMessageSkip ( colourMessage ) ) {
this . embed . setColor ( APP _MAIN _COLOUR ) ;
2021-07-11 21:47:18 -04:00
} else {
2021-07-11 22:29:11 -04:00
if ( colourMessage . content . startsWith ( '#' ) ) {
this . embed . setColor ( parseInt ( colourMessage . content . substr ( 1 ) , 16 ) ) ;
} else if ( colourMessage . content . startsWith ( '0x' ) ) {
this . embed . setColor ( parseInt ( colourMessage . content . substr ( 2 ) , 16 ) ) ;
} else if ( RGB _REGEX . test ( colourMessage . content ) ) {
const [ , red , green , blue ] = colourMessage . content . match ( RGB _REGEX ) ;
this . embed . setColor ( [ parseInt ( red ) , parseInt ( green ) , parseInt ( blue ) ] ) ;
} else {
this . embed . setColor ( colourMessage . content . toUpperCase ( ) . replace ( /[^A-Z]+/gu , '_' ) ) ;
}
2021-07-11 21:47:18 -04:00
}
2021-07-11 22:29:11 -04:00
await this . prompt ( '💬 What do you want the **description** (contents) of this embed to be?' , true ) ;
2021-07-11 21:47:18 -04:00
const descriptionMessage = await this . awaitMessage ( this . messageFilter ) ;
if ( ! descriptionMessage ) {
return null ;
}
this . embed . setDescription ( descriptionMessage . content ) ;
await this . destinationChannel . send ( this . embed ) ;
2021-07-11 22:29:11 -04:00
if ( confirmation ) {
return this . channel . send ( ` ✅ The embed has been posted in ${ this . destinationChannel } . ` ) ;
}
2021-07-11 21:47:18 -04:00
}
}
class Embed
{
static instance = null ;
constructor ( ) {
if ( Embed . instance !== null ) {
return Embed . instance ;
}
this . aliases = [ ] ;
2021-07-16 19:35:06 -04:00
this . category = CommandCategory . MODERATION ;
2021-07-11 22:29:11 -04:00
this . isAllowedForContext = CommandPermission . isMemberModOrHelper ;
2021-07-11 21:47:18 -04:00
this . description = 'Allows to post an embed' ;
}
/ * *
* @ param { Message } message
* @ param { Array } args
* /
async process ( message , args ) {
2021-07-11 22:29:11 -04:00
if ( args . length > 0 ) {
let destinationChannel = message . channel ;
let deleteMessage = true ;
if ( /<#\d+>/u . test ( args [ 0 ] ) ) {
destinationChannel = message . mentions . channels . first ( ) ;
args . shift ( ) ;
deleteMessage = false ;
}
const embed = new MessageEmbed ( ) ;
embed . setColor ( APP _MAIN _COLOUR ) ;
embed . setDescription ( args . join ( ' ' ) ) ;
await destinationChannel . send ( embed ) ;
if ( deleteMessage ) {
await message . delete ( ) ;
} else {
await message . react ( '✅' ) ;
}
} else {
const dialog = new EmbedDialog ( message ) ;
2021-07-11 21:47:18 -04:00
2021-07-11 22:29:11 -04:00
return dialog . execute ( ) ;
}
2021-07-11 21:47:18 -04:00
}
}
module . exports = new Embed ( ) ;