2019-12-27 20:27:15 +01:00
const { Listener } = require ( 'discord-akairo' ) ;
const fs = require ( 'fs' ) ;
let messageID = require ( '../../json/messageID.json' ) ; // Where reaction.message.id that entered a board will be stocked so it doesn't enter again
class messageReactionRemoveListener extends Listener {
constructor ( ) {
super ( 'messagereactionremove' , {
emitter : 'client' ,
event : 'messageReactionRemove'
} ) ;
}
2019-12-27 22:28:52 +01:00
async exec ( reaction ) {
if ( reaction . message . partial ) {
await reaction . message . fetch ( )
2020-06-17 15:29:00 +02:00
. catch ( err => {
return console . error ( err ) ;
2019-12-27 22:28:52 +01:00
} ) ;
2020-06-17 23:23:04 +02:00
}
let starboardChannel , shameboardChannel ;
let reactionCount = reaction . count ;
// If one of the reaction removed is the author of the message add 1 to the reaction count
reaction . users . cache . forEach ( user => {
if ( reaction . message . author == user ) reactionCount ++ ;
} ) ;
2020-06-17 15:42:46 +02:00
2020-06-17 23:23:04 +02:00
// Starboard
if ( fs . existsSync ( ` ./board/star ${ reaction . message . guild . id } .json ` ) ) {
starboardChannel = require ( ` ../../board/star ${ reaction . message . guild . id } .json ` ) ;
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
2020-06-17 15:42:46 +02:00
2020-06-17 23:23:04 +02:00
// Get name of the custom emoji
if ( reaction . message . guild . emojis . resolve ( staremote . replace ( /\D/g , '' ) ) ) {
staremote = reaction . message . guild . emojis . resolve ( staremote . replace ( /\D/g , '' ) ) ;
}
2020-06-17 15:42:46 +02:00
2020-06-17 23:23:04 +02:00
if ( messageID [ reaction . message . id ] && ( reaction . emoji == staremote || reaction . emoji . name == staremote ) && reactionCount < starcount ) {
let channel = this . client . channels . resolve ( starboardChannel . starboard ) ;
let message = await channel . messages . resolve ( messageID [ reaction . message . id ] ) ;
delete messageID [ reaction . message . id ] ;
// If it didn't find any message don't do anything
if ( ! message ) return ;
2020-06-17 15:42:46 +02:00
2020-06-17 23:23:04 +02:00
message . delete ( ) ;
} else if ( ( reaction . emoji == staremote || reaction . emoji . name == staremote ) && reactionCount >= starcount ) {
return editEmbed ( 'starboard' , staremote , messageID [ reaction . message . id ] , this . client ) ;
2019-12-27 20:27:15 +01:00
}
2020-06-17 23:23:04 +02:00
}
2019-12-27 20:27:15 +01:00
2020-06-17 23:23:04 +02:00
//Shameboard
if ( fs . existsSync ( ` ./board/shame ${ reaction . message . guild . id } .json ` ) ) {
shameboardChannel = require ( ` ../../board/shame ${ reaction . message . guild . id } .json ` ) ;
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
2020-06-17 15:42:46 +02:00
2020-06-17 23:23:04 +02:00
// Get name of the custom emoji
if ( reaction . message . guild . emojis . resolve ( shameemote . replace ( /\D/g , '' ) ) ) {
shameemote = reaction . message . guild . emojis . resolve ( shameemote . replace ( /\D/g , '' ) ) ;
}
2020-06-17 15:42:46 +02:00
2020-06-17 23:23:04 +02:00
if ( messageID [ reaction . message . id ] && ( reaction . emoji == shameemote || reaction . emoji . name == shameemote ) && reactionCount < shamecount ) {
let channel = this . client . channels . resolve ( shameboardChannel . shameboard ) ;
let message = await channel . messages . resolve ( messageID [ reaction . message . id ] ) ;
delete messageID [ reaction . message . id ] ;
message . delete ( ) ;
} else if ( ( reaction . emoji == shameemote || reaction . emoji . name == shameemote ) && reactionCount >= shamecount ) {
return editEmbed ( 'shameboard' , shameemote , messageID [ reaction . message . id ] , this . client ) ;
2019-12-27 20:27:15 +01:00
}
}
async function editEmbed ( name , emote , boardID , client ) {
let channel ;
if ( name == 'starboard' ) {
2020-03-04 02:08:04 +01:00
channel = client . channels . resolve ( starboardChannel . starboard ) ;
2019-12-27 20:27:15 +01:00
} else {
2020-03-04 02:08:04 +01:00
channel = client . channels . resolve ( shameboardChannel . shameboard ) ;
2019-12-27 20:27:15 +01:00
}
2020-03-04 02:08:04 +01:00
let message = await channel . messages . resolve ( boardID ) ;
2020-02-05 16:48:21 +01:00
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-06-17 15:42:46 +02: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 )
2020-03-04 02:08:04 +01:00
. setFooter ( ` ${ emote } ${ reactionCount } ` )
2019-12-27 20:27:15 +01:00
. setTimestamp ( ) ;
2020-03-04 02:08:04 +01:00
if ( reaction . message . guild . emojis . resolve ( emote ) ) Embed . setFooter ( reactionCount , reaction . message . guild . emojis . resolve ( emote ) . url ) ;
2019-12-27 20:27:15 +01:00
message . edit ( { embed : Embed } ) ;
}
}
}
module . exports = messageReactionRemoveListener ;