2020-10-01 01:43:38 +03:00
|
|
|
import { GuildPluginData } from "knub";
|
2020-07-22 23:15:40 +02:00
|
|
|
import { StarboardPluginType, TStarboardOpts } from "../types";
|
|
|
|
import { Message, GuildChannel, TextChannel, Embed } from "eris";
|
|
|
|
import moment from "moment-timezone";
|
2020-11-09 20:03:57 +02:00
|
|
|
import { EmbedWith, EMPTY_CHAR, messageLink } from "../../../utils";
|
2020-07-22 23:15:40 +02:00
|
|
|
import path from "path";
|
|
|
|
|
|
|
|
export async function saveMessageToStarboard(
|
2020-10-01 01:43:38 +03:00
|
|
|
pluginData: GuildPluginData<StarboardPluginType>,
|
2020-07-22 23:15:40 +02:00
|
|
|
msg: Message,
|
|
|
|
starboard: TStarboardOpts,
|
|
|
|
) {
|
|
|
|
const channel = pluginData.guild.channels.get(starboard.channel_id);
|
|
|
|
if (!channel) return;
|
|
|
|
|
2020-11-09 20:03:57 +02:00
|
|
|
const embed: EmbedWith<"footer" | "author" | "fields" | "timestamp"> = {
|
2020-07-22 23:15:40 +02:00
|
|
|
footer: {
|
|
|
|
text: `#${(msg.channel as GuildChannel).name}`,
|
|
|
|
},
|
|
|
|
author: {
|
|
|
|
name: `${msg.author.username}#${msg.author.discriminator}`,
|
|
|
|
},
|
|
|
|
fields: [],
|
|
|
|
timestamp: new Date(msg.timestamp).toISOString(),
|
|
|
|
};
|
|
|
|
|
|
|
|
if (msg.author.avatarURL) {
|
|
|
|
embed.author.icon_url = msg.author.avatarURL;
|
|
|
|
}
|
|
|
|
|
2020-12-23 02:37:08 +02:00
|
|
|
// The second condition here checks for messages with only an image link that is then embedded.
|
|
|
|
// The message content in that case is hidden by the Discord client, so we hide it here too.
|
|
|
|
if (msg.content && msg.embeds[0]?.thumbnail?.url !== msg.content) {
|
2020-07-22 23:15:40 +02:00
|
|
|
embed.description = msg.content;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merge media and - if copy_full_embed is enabled - fields and title from the first embed in the original message
|
|
|
|
if (msg.embeds.length > 0) {
|
|
|
|
if (msg.embeds[0].image) embed.image = msg.embeds[0].image;
|
|
|
|
|
|
|
|
if (starboard.copy_full_embed) {
|
|
|
|
if (msg.embeds[0].title) {
|
|
|
|
const titleText = msg.embeds[0].url ? `[${msg.embeds[0].title}](${msg.embeds[0].url})` : msg.embeds[0].title;
|
|
|
|
embed.fields.push({ name: EMPTY_CHAR, value: titleText });
|
|
|
|
}
|
|
|
|
|
|
|
|
if (msg.embeds[0].fields) embed.fields.push(...msg.embeds[0].fields);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there are no embeds, add the first image attachment explicitly
|
|
|
|
else if (msg.attachments.length) {
|
|
|
|
for (const attachment of msg.attachments) {
|
|
|
|
const ext = path
|
|
|
|
.extname(attachment.filename)
|
|
|
|
.slice(1)
|
|
|
|
.toLowerCase();
|
|
|
|
if (!["jpeg", "jpg", "png", "gif", "webp"].includes(ext)) continue;
|
|
|
|
|
|
|
|
embed.image = { url: attachment.url };
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
embed.fields.push({ name: EMPTY_CHAR, value: `[Jump to message](${messageLink(msg)})` });
|
|
|
|
|
|
|
|
const starboardMessage = await (channel as TextChannel).createMessage({ embed });
|
|
|
|
await pluginData.state.starboardMessages.createStarboardMessage(channel.id, msg.id, starboardMessage.id);
|
|
|
|
}
|