edit star/shameboard when reaction count change

This commit is contained in:
loicbersier 2019-12-27 20:27:15 +01:00
parent 739643ab9f
commit 450ab79c08
2 changed files with 140 additions and 13 deletions

View file

@ -1,6 +1,6 @@
const { Listener } = require('discord-akairo'); const { Listener } = require('discord-akairo');
const fs = require('fs'); const fs = require('fs');
let messageID = []; // Where reaction.message.id that entered a board will be stocked so it doesn't enter again let messageID = require('../../json/messageID.json'); // Save messages that ented starboard so we can edit/remove the embed
class MessageReactionAddListener extends Listener { class MessageReactionAddListener extends Listener {
@ -15,8 +15,6 @@ class MessageReactionAddListener extends Listener {
if (reaction.message.author == user) return; if (reaction.message.author == user) return;
let starboardChannel, shameboardChannel; let starboardChannel, shameboardChannel;
if (messageID.includes(reaction.message.id)) return;
// Starboard // Starboard
if (fs.existsSync(`./board/star${reaction.message.guild.id}.json`)) { if (fs.existsSync(`./board/star${reaction.message.guild.id}.json`)) {
starboardChannel = require(`../../board/star${reaction.message.guild.id}.json`); starboardChannel = require(`../../board/star${reaction.message.guild.id}.json`);
@ -28,11 +26,14 @@ class MessageReactionAddListener extends Listener {
staremote = this.client.util.resolveEmoji(staremote, reaction.message.guild.emojis).name; staremote = this.client.util.resolveEmoji(staremote, reaction.message.guild.emojis).name;
} }
if (reaction.emoji.name == staremote && reaction.count == starcount) { if (reaction.emoji.name == staremote) {
messageID.push(reaction.message.id); if (messageID[reaction.message.id] && reaction.count > starcount) {
return editEmbed('starboard', staremote, messageID[reaction.message.id], this.client);
} else if (reaction.count == starcount) {
return sendEmbed('starboard', staremote, this.client); return sendEmbed('starboard', staremote, this.client);
} }
} }
}
//Shameboard //Shameboard
if (fs.existsSync(`./board/shame${reaction.message.guild.id}.json`)) { if (fs.existsSync(`./board/shame${reaction.message.guild.id}.json`)) {
@ -41,12 +42,44 @@ class MessageReactionAddListener extends Listener {
let shamecount = shameboardChannel.count; 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 delete require.cache[require.resolve(`../../board/shame${reaction.message.guild.id}.json`)]; // Delete the boardChannel cache so it can reload it next time
if (this.client.util.resolveEmoji(shameemote, reaction.message.guild.emojis)) {
shameemote = this.client.util.resolveEmoji(shameemote, reaction.message.guild.emojis).name;
}
if (reaction.emoji.name == shameemote && reaction.count == shamecount) { if (reaction.emoji.name == shameemote) {
messageID.push(reaction.message.id); if (messageID[reaction.message.id] && reaction.count > shamecount) {
return editEmbed('shameboard', shameemote, messageID[reaction.message.id], this.client);
} else if (reaction.count == shamecount) {
return sendEmbed('shameboard', shameemote, this.client); return sendEmbed('shameboard', shameemote, this.client);
} }
} }
}
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);
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)
.setDescription(message.embeds[0].description)
.setFooter(reaction.count + ' ' + emote)
.setTimestamp();
if (reaction.message.guild.emojis.find(emoji => emoji.name === emote)) {
Embed.setFooter(reaction.count, reaction.message.guild.emojis.find(emoji => emoji.name === emote).url);
}
message.edit({ embed: Embed });
}
async function sendEmbed(name, emote, client) { async function sendEmbed(name, emote, client) {
let messageAttachments = reaction.message.attachments.map(u=> u.url); let messageAttachments = reaction.message.attachments.map(u=> u.url);
@ -74,15 +107,22 @@ class MessageReactionAddListener extends Listener {
if (reaction.message.channel.nsfw && !channel.nsfw) { if (reaction.message.channel.nsfw && !channel.nsfw) {
Embed.setDescription(`||${reaction.message.content}||`); Embed.setDescription(`||${reaction.message.content}||`);
if (messageAttachments != '') { if (messageAttachments != '') {
return channel.send(`||${messageAttachments}||`, { embed: Embed }); let message = await channel.send(`||${messageAttachments}||`, { embed: Embed });
messageID[reaction.message.id] = message.id;
//boardMessage.push({ reactionID: reaction.message.id, boardID: message.id });
} }
else { else {
return channel.send({embed: Embed}); let message = await channel.send({embed: Embed});
messageID[reaction.message.id] = message.id;
//boardMessage.push({ reactionID: reaction.message.id, boardID: message.id });
} }
} else { } else {
Embed.setDescription(reaction.message.content); Embed.setDescription(reaction.message.content);
return channel.send({ files: messageAttachments, embed: Embed }) let message = await channel.send({ files: messageAttachments, embed: Embed })
.catch(async () => channel.send(messageAttachments, { embed: Embed })); .catch(async () => channel.send(messageAttachments, { embed: Embed }));
messageID[reaction.message.id] = message.id;
//boardMessage.push({ reactionID: reaction.message.id, boardID: message.id });
} }
} }
} }

View file

@ -0,0 +1,87 @@
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'
});
}
async exec(reaction, user) {
if (reaction.message.author == user) return;
let starboardChannel, shameboardChannel;
// 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
if (this.client.util.resolveEmoji(staremote, reaction.message.guild.emojis)) {
staremote = this.client.util.resolveEmoji(staremote, reaction.message.guild.emojis).name;
}
if (reaction.emoji.name == staremote && reaction.count < starcount) {
console.log('probably removed by now');
let channel = this.client.channels.get(starboardChannel.starboard);
let message = await channel.messages.get(messageID[reaction.message.id]);
message.delete();
} else if (reaction.emoji.name == staremote && reaction.count >= starcount) {
return editEmbed('starboard', staremote, messageID[reaction.message.id], this.client);
}
}
//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
if (this.client.util.resolveEmoji(shameemote, reaction.message.guild.emojis)) {
shameemote = this.client.util.resolveEmoji(shameemote, reaction.message.guild.emojis).name;
}
if (reaction.emoji.name == shameemote && reaction.count < shamecount) {
console.log('probably removed by now');
let channel = this.client.channels.get(shameboardChannel.shameboard);
let message = await channel.messages.get(messageID[reaction.message.id]);
message.delete();
} else if (reaction.emoji.name == shameemote && reaction.count >= shamecount) {
return editEmbed('shameboard', shameemote, messageID[reaction.message.id], this.client);
}
}
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);
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)
.setDescription(message.embeds[0].description)
.setFooter(reaction.count + ' ' + emote)
.setTimestamp();
if (reaction.message.guild.emojis.find(emoji => emoji.name === emote)) {
Embed.setFooter(reaction.count, reaction.message.guild.emojis.find(emoji => emoji.name === emote).url);
}
message.edit({ embed: Embed });
}
}
}
module.exports = messageReactionRemoveListener;