2020-07-22 23:15:40 +02:00
|
|
|
import { starboardEvt } from "../types";
|
2020-07-30 02:02:32 +03:00
|
|
|
import { Message, TextChannel } from "eris";
|
2020-10-01 01:43:38 +03:00
|
|
|
import { UnknownUser, resolveMember, noop, resolveUser } from "../../../utils";
|
2020-07-22 23:15:40 +02:00
|
|
|
import { saveMessageToStarboard } from "../util/saveMessageToStarboard";
|
|
|
|
|
|
|
|
export const StarboardReactionAddEvt = starboardEvt({
|
|
|
|
event: "messageReactionAdd",
|
|
|
|
|
|
|
|
async listener(meta) {
|
|
|
|
const pluginData = meta.pluginData;
|
|
|
|
|
|
|
|
let msg = meta.args.message as Message;
|
2020-10-16 02:13:54 +03:00
|
|
|
const userId = meta.args.member.id;
|
2020-07-22 23:15:40 +02:00
|
|
|
const emoji = meta.args.emoji;
|
|
|
|
|
|
|
|
if (!msg.author) {
|
|
|
|
// Message is not cached, fetch it
|
|
|
|
try {
|
|
|
|
msg = await msg.channel.getMessage(msg.id);
|
|
|
|
} catch (e) {
|
|
|
|
// Sometimes we get this event for messages we can't fetch with getMessage; ignore silently
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// No self-votes!
|
|
|
|
if (msg.author.id === userId) return;
|
|
|
|
|
2020-07-30 02:02:32 +03:00
|
|
|
const member = await resolveMember(pluginData.client, pluginData.guild, userId);
|
|
|
|
if (!member || member.bot) return;
|
|
|
|
|
|
|
|
const config = pluginData.config.getMatchingConfig({
|
|
|
|
member,
|
|
|
|
channelId: msg.channel.id,
|
|
|
|
categoryId: (msg.channel as TextChannel).parentID,
|
|
|
|
});
|
2020-07-22 23:15:40 +02:00
|
|
|
|
|
|
|
const applicableStarboards = Object.values(config.boards)
|
|
|
|
.filter(board => board.enabled)
|
|
|
|
// Can't star messages in the starboard channel itself
|
|
|
|
.filter(board => board.channel_id !== msg.channel.id)
|
|
|
|
// Matching emoji
|
|
|
|
.filter(board => {
|
2020-11-09 20:03:57 +02:00
|
|
|
return board.star_emoji!.some((boardEmoji: string) => {
|
2020-07-22 23:15:40 +02:00
|
|
|
if (emoji.id) {
|
|
|
|
// Custom emoji
|
|
|
|
const customEmojiMatch = boardEmoji.match(/^<?:.+?:(\d+)>?$/);
|
|
|
|
if (customEmojiMatch) {
|
|
|
|
return customEmojiMatch[1] === emoji.id;
|
|
|
|
}
|
|
|
|
|
|
|
|
return boardEmoji === emoji.id;
|
|
|
|
} else {
|
|
|
|
// Unicode emoji
|
|
|
|
return emoji.name === boardEmoji;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
for (const starboard of applicableStarboards) {
|
|
|
|
// Save reaction into the database
|
|
|
|
await pluginData.state.starboardReactions.createStarboardReaction(msg.id, userId).catch(noop);
|
|
|
|
|
|
|
|
// If the message has already been posted to this starboard, we don't need to do anything else
|
|
|
|
const starboardMessages = await pluginData.state.starboardMessages.getMatchingStarboardMessages(
|
|
|
|
starboard.channel_id,
|
|
|
|
msg.id,
|
|
|
|
);
|
|
|
|
if (starboardMessages.length > 0) continue;
|
|
|
|
|
|
|
|
const reactions = await pluginData.state.starboardReactions.getAllReactionsForMessageId(msg.id);
|
|
|
|
const reactionsCount = reactions.length;
|
|
|
|
if (reactionsCount >= starboard.stars_required) {
|
|
|
|
await saveMessageToStarboard(pluginData, msg, starboard);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|