Skip to content

Commit

Permalink
added starboard, fixed util bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
sabattle committed Sep 4, 2020
1 parent 70e14ea commit 1551d82
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 15 deletions.
100 changes: 87 additions & 13 deletions src/events/messageReactionAdd.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,98 @@
const { MessageEmbed } = require('discord.js');
const { verify } = require('../utils/emojis.json');
const { stripIndent } = require('common-tags');

module.exports = async (client, messageReaction, user) => {

if (messageReaction.emoji.id != verify.split(':')[2].slice(0, -1) || client.user === user) return;
if (client.user === user) return;

const { verification_role_id: verificationRoleId, verification_message_id: verificationMessageId } =
client.db.settings.selectVerification.get(messageReaction.message.guild.id);
const verificationRole = messageReaction.message.guild.roles.cache.get(verificationRoleId);
const { message, emoji } = messageReaction;

if (!verificationRole || messageReaction.message.id != verificationMessageId) return;
// Verification
if (emoji.id === verify.split(':')[2].slice(0, -1)) {
const { verification_role_id: verificationRoleId, verification_message_id: verificationMessageId } =
client.db.settings.selectVerification.get(message.guild.id);
const verificationRole = message.guild.roles.cache.get(verificationRoleId);

const member = messageReaction.message.guild.members.cache.get(user.id);
if (!member.roles.cache.has(verificationRole)) {
try {
await member.roles.add(verificationRole);
} catch (err) {
return client.sendSystemErrorMessage(member.guild, 'verification', stripIndent`
Unable to assign verification role, please check the role hierarchy and ensure I have the Manage Roles permission
`, err.message);
if (!verificationRole || message.id != verificationMessageId) return;

const member = message.guild.members.cache.get(user.id);
if (!member.roles.cache.has(verificationRole)) {
try {
await member.roles.add(verificationRole);
} catch (err) {
return client.sendSystemErrorMessage(member.guild, 'verification',
stripIndent`Unable to assign verification role,` +
'please check the role hierarchy and ensure I have the Manage Roles permission'
, err.message);
}
}
}

// Starboard
if (emoji.name === '⭐' && message.author != user) {
const starboardChannelId = client.db.settings.selectStarboardChannelId.pluck().get(message.guild.id);
const starboardChannel = message.guild.channels.cache.get(starboardChannelId);
if (
!starboardChannel ||
!starboardChannel.viewable ||
!starboardChannel.permissionsFor(message.guild.me).has(['SEND_MESSAGES', 'EMBED_LINKS']) ||
message.channel === starboardChannel
) return;

const emojis = ['⭐', '🌟', '✨', '💫', '☄️'];
const messages = await starboardChannel.messages.fetch({ limit: 100 });
const starred = messages.find(m => {
return emojis.some(e => {
return m.content.startsWith(e) &&
m.embeds[0] &&
m.embeds[0].footer &&
m.embeds[0].footer.text == message.id;
});
});

// If message already in starboard
if (starred) {
const starCount = parseInt(starred.content.split(' ')[1].slice(2)) + 1;

// Determine emoji type
let emojiType;
if (starCount > 20) emojiType = emojis[4];
else if (starCount > 15) emojiType = emojis[3];
else if (starCount > 10) emojiType = emojis[2];
else if (starCount > 5) emojiType = emojis[1];
else emojiType = emojis[0];

const starMessage = await starboardChannel.messages.fetch(starred.id);
await starMessage.edit(`${emojiType} **${starCount} |** ${message.channel}`)
.catch(err => client.logger.error(err.stack));

// New starred message
} else {

// Check for attachment image
let image = '';
const attachment = message.attachments.array()[0];
if (attachment && attachment.url) {
const extension = attachment.url.split('.').pop();
if (/(jpg|jpeg|png|gif)/gi.test(extension)) image = attachment.url;
}

// Check for url
if (!image && message.embeds[0] && message.embeds[0].url) {
const extension = message.embeds[0].url.split('.').pop();
if (/(jpg|jpeg|png|gif)/gi.test(extension)) image = message.embeds[0].url;
}

const embed = new MessageEmbed()
.setAuthor(message.author.tag, message.author.displayAvatarURL({ dynamic: true}))
.setDescription(message.content)
.addField('Original', `[Jump!](${message.url})`)
.setImage(image)
.setTimestamp()
.setFooter(message.id)
.setColor('#ffac33');
await starboardChannel.send(`⭐ **1 |** ${message.channel}`, embed);
}
}
};
50 changes: 50 additions & 0 deletions src/events/messageReactionRemove.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
module.exports = async (client, messageReaction, user) => {

if (client.user === user) return;

const { message, emoji } = messageReaction;

// Starboard
if (emoji.name === '⭐' && message.author != user) {
const starboardChannelId = client.db.settings.selectStarboardChannelId.pluck().get(message.guild.id);
const starboardChannel = message.guild.channels.cache.get(starboardChannelId);
if (
!starboardChannel ||
!starboardChannel.viewable ||
!starboardChannel.permissionsFor(message.guild.me).has(['SEND_MESSAGES', 'EMBED_LINKS']) ||
message.channel === starboardChannel
) return;

const emojis = ['⭐', '🌟', '✨', '💫', '☄️'];
const messages = await starboardChannel.messages.fetch({ limit: 100 });

const starred = messages.find(m => {
return emojis.some(e => {
return m.content.startsWith(e) &&
m.embeds[0] &&
m.embeds[0].footer &&
m.embeds[0].footer.text == message.id;
});
});

// If message already in starboard
if (starred) {

const starCount = parseInt(starred.content.split(' ')[1].slice(2)) - 1;

// Determine emoji type
let emojiType;
if (starCount > 20) emojiType = emojis[4];
else if (starCount > 15) emojiType = emojis[3];
else if (starCount > 10) emojiType = emojis[2];
else if (starCount > 5) emojiType = emojis[1];
else emojiType = emojis[0];

const starMessage = await starboardChannel.messages.fetch(starred.id);
await starMessage.edit(`${emojiType} **${starCount} |** ${message.channel}`);

if (starCount == 0)
await starMessage.delete().catch(err => client.logger.error(err.stack));
}
}
};
6 changes: 4 additions & 2 deletions src/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ function getStatus(...args) {
* @param {string} message
*/
function replaceKeywords(message) {
return message
if (!message) return message;
else return message
.replace(/\?member/g, '`?member`')
.replace(/\?username/g, '`?username`')
.replace(/\?tag/g, '`?tag`')
Expand All @@ -133,7 +134,8 @@ function replaceKeywords(message) {
* @param {string} message
*/
function replaceCrownKeywords(message) {
return message
if (!message) return message;
else return message
.replace(/\?member/g, '`?member`')
.replace(/\?username/g, '`?username`')
.replace(/\?tag/g, '`?tag`')
Expand Down

0 comments on commit 1551d82

Please sign in to comment.