From cbd0bc3c42b0c009220f4b6b1bb02363afc7664d Mon Sep 17 00:00:00 2001 From: WubzyGD Date: Sat, 24 Oct 2020 14:31:18 -0600 Subject: [PATCH] Finished the starboard completely and added star schema --- events/messageReactionAdd.js | 41 ++++++++++++++++++++++++++++++++++++ models/starboard.js | 10 +++++++++ 2 files changed, 51 insertions(+) create mode 100644 events/messageReactionAdd.js create mode 100644 models/starboard.js diff --git a/events/messageReactionAdd.js b/events/messageReactionAdd.js new file mode 100644 index 0000000..8187c5b --- /dev/null +++ b/events/messageReactionAdd.js @@ -0,0 +1,41 @@ +const Discord = require("discord.js"); +const GuildData = require('../models/guild'); +const StarData = require('../models/starboard'); + +module.exports = async (client, reaction, user) => { + if (reaction.partial) {try {await reaction.fetch();} catch {return;}} + + if (reaction.emoji.name === "⭐") { + if (!reaction.message.guild) {return;} + let tg = await GuildData.findOne({gid: reaction.message.guild.id}); + if (!tg) {return;} + if (tg.starchannel.length && tg.starsenabled && reaction.message.guild.channels.cache.has(tg.starchannel) && reaction.message.guild.channels.cache.get(tg.starchannel).permissionsFor(client.user.id).has('SEND_MESSAGES')) { + if (reaction.message.channel.id === tg.starchannel) {return;} + let sd = await StarData.findOne({gid: reaction.message.guild.id}) ? await StarData.findOne({gid: reaction.message.guild.id}) : new StarData({gid: reaction.message.guild.id}); + + let starEmbed = new Discord.MessageEmbed() + .setTitle('Starred Message!') + .setDescription(`Sent by ${reaction.message.member.displayName} (<@${reaction.message.author.id}>) || Channel: ${reaction.message.channel.name} (<#${reaction.message.channel.id}>)\n[Jump to Message](${reaction.message.url})`) + .setThumbnail(reaction.message.author.avatarURL({size: 2048})) + .setColor('ebb931') + .setFooter("Natsuki", client.user.avatarURL()) + .setTimestamp(); + if (reaction.message.content.length) {starEmbed.addField("Message", reaction.message.content);} + starEmbed + .addField("Stars", `:star: ${reaction.count}`, true) + .addField(`${reaction.message.member.displayName.toLowerCase().endsWith('s') ? `${reaction.message.member.displayName}'` : `${reaction.message.member.displayName}'s`} StarBoard Count`, sd.starCount[reaction.message.author.id] ? sd.starCount[reaction.message.author.id] + 1 : 1, true); + if (reaction.message.attachments.size) {starEmbed.setImage(reaction.message.attachments.first().url);} + if (Object.keys(sd.stars).includes(reaction.message.id)) { + let starMessage = await reaction.message.guild.channels.cache.get(tg.starchannel).messages.fetch(sd.stars[reaction.message.id]); + if (starMessage) {await starMessage.edit(starEmbed);} + } else { + if (reaction.count < tg.starreq) {return;} + let starEmbedMessage = await reaction.message.guild.channels.cache.get(tg.starchannel).send(starEmbed); + sd.stars[reaction.message.id] = starEmbedMessage.id; + sd.starCount[reaction.message.author.id] = sd.starCount[reaction.message.author.id] ? sd.starCount[reaction.message.author.id] + 1 : 1; + sd.serverStarCount += 1; + sd.save(); + } + } + } +}; \ No newline at end of file diff --git a/models/starboard.js b/models/starboard.js new file mode 100644 index 0000000..df758e5 --- /dev/null +++ b/models/starboard.js @@ -0,0 +1,10 @@ +const mongoose = require('mongoose'); + +const StarSchema = new mongoose.Schema({ + gid: {type: String, unique: true}, + stars: {type: Object, default: {}}, + starCount: {type: Object, default: {}}, + serverStarCount: {type: Number, default: 0} +}); + +module.exports = mongoose.model('stars', StarSchema); \ No newline at end of file