2019-01-01 03:49:46 +01:00
const { Listener } = require ( 'discord-akairo' ) ;
2019-06-28 03:50:16 +02:00
const fs = require ( 'fs' ) ;
2019-12-27 20:27:15 +01:00
let messageID = require ( '../../json/messageID.json' ) ; // Save messages that ented starboard so we can edit/remove the embed
2019-01-01 03:49:46 +01:00
2019-06-28 03:50:16 +02:00
2019-01-01 03:49:46 +01:00
class MessageReactionAddListener extends Listener {
2019-01-02 08:09:45 +01:00
constructor ( ) {
super ( 'messagereactionadd' , {
emitter : 'client' ,
2019-01-02 21:46:12 +01:00
event : 'messageReactionAdd'
2019-01-02 08:09:45 +01:00
} ) ;
}
2019-12-27 22:06:16 +01:00
async exec ( reaction ) {
2019-12-27 22:28:52 +01:00
if ( reaction . message . partial ) {
await reaction . message . fetch ( )
. catch ( ( ) => {
return ;
} ) ;
}
2019-12-26 18:37:40 +01:00
let starboardChannel , shameboardChannel ;
2019-12-27 22:06:16 +01:00
let reactionCount = reaction . count ;
// If one of the reaction is the author of the message remove 1 to the reaction count
reaction . users . forEach ( user => {
if ( reaction . message . author == user ) reactionCount -- ;
} ) ;
2019-12-27 18:48:15 +01:00
2019-07-11 17:30:26 +02:00
// Starboard
2019-06-28 03:50:16 +02:00
if ( fs . existsSync ( ` ./board/star ${ reaction . message . guild . id } .json ` ) ) {
starboardChannel = require ( ` ../../board/star ${ reaction . message . guild . id } .json ` ) ;
2019-12-26 18:37:40 +01:00
let staremote = starboardChannel . emote ;
let starcount = starboardChannel . count ;
delete require . cache [ require . resolve ( ` ../../board/star ${ reaction . message . guild . id } .json ` ) ] ; // Delete the boardChannel cache so it can reload it next time
2019-12-27 22:06:16 +01:00
// Get name of the custom emoji
2019-12-26 18:37:40 +01:00
if ( this . client . util . resolveEmoji ( staremote , reaction . message . guild . emojis ) ) {
staremote = this . client . util . resolveEmoji ( staremote , reaction . message . guild . emojis ) . name ;
}
2019-06-30 22:47:25 +02:00
2019-12-27 20:27:15 +01:00
if ( reaction . emoji . name == staremote ) {
2019-12-27 22:06:16 +01:00
if ( messageID [ reaction . message . id ] && reactionCount > starcount ) {
2019-12-27 20:27:15 +01:00
return editEmbed ( 'starboard' , staremote , messageID [ reaction . message . id ] , this . client ) ;
2019-12-27 22:06:16 +01:00
} else if ( reactionCount == starcount ) {
2019-12-27 20:27:15 +01:00
return sendEmbed ( 'starboard' , staremote , this . client ) ;
}
2019-06-30 22:47:25 +02:00
}
2019-02-15 01:09:08 +01:00
}
2019-06-30 22:47:25 +02:00
2019-07-11 17:30:26 +02:00
//Shameboard
2019-06-28 03:50:16 +02:00
if ( fs . existsSync ( ` ./board/shame ${ reaction . message . guild . id } .json ` ) ) {
shameboardChannel = require ( ` ../../board/shame ${ reaction . message . guild . id } .json ` ) ;
2019-12-26 18:37:40 +01:00
let shameemote = shameboardChannel . emote ;
let shamecount = shameboardChannel . count ;
delete require . cache [ require . resolve ( ` ../../board/shame ${ reaction . message . guild . id } .json ` ) ] ; // Delete the boardChannel cache so it can reload it next time
2019-12-27 22:06:16 +01:00
// Get name of the custom emoji
2019-12-27 20:27:15 +01:00
if ( this . client . util . resolveEmoji ( shameemote , reaction . message . guild . emojis ) ) {
shameemote = this . client . util . resolveEmoji ( shameemote , reaction . message . guild . emojis ) . name ;
}
2019-02-14 18:44:21 +01:00
2019-12-27 20:27:15 +01:00
if ( reaction . emoji . name == shameemote ) {
2019-12-27 22:06:16 +01:00
if ( messageID [ reaction . message . id ] && reactionCount > shamecount ) {
2019-12-27 20:27:15 +01:00
return editEmbed ( 'shameboard' , shameemote , messageID [ reaction . message . id ] , this . client ) ;
2019-12-27 22:06:16 +01:00
} else if ( reactionCount == shamecount ) {
2019-12-27 20:27:15 +01:00
return sendEmbed ( 'shameboard' , shameemote , this . client ) ;
}
2019-06-30 22:47:25 +02:00
}
2019-03-30 03:50:35 +01:00
}
2019-03-26 02:46:36 +01:00
2019-12-27 20:27:15 +01:00
async function editEmbed ( name , emote , boardID , client ) {
let channel ;
if ( name == 'starboard' ) {
channel = client . channels . get ( starboardChannel . starboard ) ;
} else {
channel = client . channels . get ( shameboardChannel . shameboard ) ;
}
let message = await channel . messages . get ( boardID ) ;
2020-01-19 13:35:47 +01:00
// If the message doesn't have embeds assume it got deleted so don't do anything
if ( ! message ) return ;
2019-12-31 12:12:35 +01:00
// If the original embed description is empty make this embed empty ( and not undefined )
2020-01-06 22:54:14 +01:00
let description = message . embeds [ 0 ] . description ;
2020-02-05 16:48:21 +01:00
if ( ! message . embeds [ 0 ] . description || message . embeds [ 0 ] . description == undefined )
2019-12-31 12:12:35 +01:00
description = '' ;
2019-12-27 20:27:15 +01:00
let Embed = client . util . embed ( )
. setColor ( reaction . message . member . displayHexColor )
. setAuthor ( reaction . message . author . username , reaction . message . author . displayAvatarURL ( ) )
. addField ( 'Jump to' , ` [message](https://discordapp.com/channels/ ${ reaction . message . guild . id } / ${ reaction . message . channel . id } / ${ reaction . message . id } ) ` , true )
. addField ( 'Channel' , reaction . message . channel , true )
2019-12-31 12:12:35 +01:00
. setDescription ( description )
2019-12-27 22:06:16 +01:00
. setFooter ( reactionCount + ' ' + emote )
2019-12-27 20:27:15 +01:00
. setTimestamp ( ) ;
if ( reaction . message . guild . emojis . find ( emoji => emoji . name === emote ) ) {
2019-12-27 22:06:16 +01:00
Embed . setFooter ( reactionCount , reaction . message . guild . emojis . find ( emoji => emoji . name === emote ) . url ) ;
2019-12-27 20:27:15 +01:00
}
message . edit ( { embed : Embed } ) ;
}
2019-12-06 18:48:51 +01:00
async function sendEmbed ( name , emote , client ) {
2020-02-29 18:13:41 +01:00
let messageAttachments = reaction . message . attachments . map ( u => u . url ) [ 0 ] ;
2019-12-26 18:37:40 +01:00
// Should change this so it automatically pic the channel ( I'm lazy right now )
2019-03-30 03:50:35 +01:00
let channel ;
if ( name == 'starboard' ) {
2019-12-26 18:37:40 +01:00
channel = client . channels . get ( starboardChannel . starboard ) ;
2019-03-30 03:50:35 +01:00
} else {
2019-12-26 18:37:40 +01:00
channel = client . channels . get ( shameboardChannel . shameboard ) ;
2019-03-13 18:57:54 +01:00
}
2019-01-15 19:22:23 +01:00
2019-11-27 17:05:47 +01:00
let Embed = client . util . embed ( )
2019-01-04 19:43:10 +01:00
. setColor ( reaction . message . member . displayHexColor )
2019-01-14 20:27:27 +01:00
. setAuthor ( reaction . message . author . username , reaction . message . author . displayAvatarURL ( ) )
2019-06-22 17:48:17 +02:00
. addField ( 'Jump to' , ` [message](https://discordapp.com/channels/ ${ reaction . message . guild . id } / ${ reaction . message . channel . id } / ${ reaction . message . id } ) ` , true )
. addField ( 'Channel' , reaction . message . channel , true )
2019-12-27 22:06:16 +01:00
. setFooter ( reactionCount + ' ' + emote )
2019-01-02 10:21:21 +01:00
. setTimestamp ( ) ;
2019-01-02 08:09:45 +01:00
2019-12-26 18:37:40 +01:00
if ( reaction . message . guild . emojis . find ( emoji => emoji . name === emote ) ) {
2019-12-27 22:06:16 +01:00
Embed . setFooter ( reactionCount , reaction . message . guild . emojis . find ( emoji => emoji . name === emote ) . url ) ;
2019-12-26 18:37:40 +01:00
}
2020-02-19 18:38:32 +01:00
let description = '' ;
2020-02-29 18:13:41 +01:00
2020-02-19 18:38:32 +01:00
if ( reaction . message . embeds [ 0 ] ) {
2020-02-29 18:13:41 +01:00
console . log ( reaction . message . embeds [ 0 ] . url ) ;
if ( reaction . message . embeds [ 0 ] . type == 'image' ) {
messageAttachments = reaction . message . embeds [ 0 ] . url ;
}
2020-02-19 18:38:32 +01:00
if ( reaction . message . embeds [ 0 ] . description ) {
description = reaction . message . embeds [ 0 ] . description ;
2020-02-29 18:13:41 +01:00
} else if ( reaction . message . content ) {
description = reaction . message . content ;
2020-02-19 18:38:32 +01:00
}
2020-02-29 18:13:41 +01:00
} else if ( reaction . message . content ) {
2020-02-19 18:38:32 +01:00
description = reaction . message . content ;
}
2020-02-29 18:13:41 +01:00
// if message come from nsfw channel and the star/shameboard channel isn't nsfw put it in spoiler
2019-08-15 16:40:17 +02:00
if ( reaction . message . channel . nsfw && ! channel . nsfw ) {
2020-01-06 22:54:14 +01:00
Embed . setDescription ( ` || ${ description } || ` ) ;
2019-08-12 08:28:04 +02:00
if ( messageAttachments != '' ) {
2019-12-27 20:27:15 +01:00
let message = await channel . send ( ` || ${ messageAttachments } || ` , { embed : Embed } ) ;
messageID [ reaction . message . id ] = message . id ;
2020-02-29 18:13:41 +01:00
} else {
2019-12-27 20:27:15 +01:00
let message = await channel . send ( { embed : Embed } ) ;
messageID [ reaction . message . id ] = message . id ;
2019-08-12 08:28:04 +02:00
}
} else {
2020-01-06 22:54:14 +01:00
Embed . setDescription ( description ) ;
2020-02-29 18:13:41 +01:00
let message = await channel . send ( { files : [ messageAttachments ] , embed : Embed } )
. catch ( async ( ) => channel . send ( messageAttachments , { embed : Embed } ) ) ;
2019-12-27 20:27:15 +01:00
messageID [ reaction . message . id ] = message . id ;
2019-08-12 08:28:04 +02:00
}
2019-01-02 08:09:45 +01:00
}
}
2019-01-01 03:49:46 +01:00
}
module . exports = MessageReactionAddListener ;