mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-05-10 12:25:02 +00:00
starboard: add star count to embed, update automatically
This commit is contained in:
parent
5ec33848b4
commit
9883610a3b
6 changed files with 155 additions and 77 deletions
|
@ -4,10 +4,8 @@ import { Message, GuildChannel, TextChannel, Embed } from "eris";
|
|||
import moment from "moment-timezone";
|
||||
import { EmbedWith, EMPTY_CHAR, messageLink } from "../../../utils";
|
||||
import path from "path";
|
||||
|
||||
const imageAttachmentExtensions = ["jpeg", "jpg", "png", "gif", "webp"];
|
||||
const audioAttachmentExtensions = ["wav", "mp3", "m4a"];
|
||||
const videoAttachmentExtensions = ["mp4", "mkv", "mov"];
|
||||
import { createStarboardEmbedFromMessage } from "./createStarboardEmbedFromMessage";
|
||||
import { createStarboardPseudoFooterForMessage } from "./createStarboardPseudoFooterForMessage";
|
||||
|
||||
export async function saveMessageToStarboard(
|
||||
pluginData: GuildPluginData<StarboardPluginType>,
|
||||
|
@ -17,73 +15,9 @@ export async function saveMessageToStarboard(
|
|||
const channel = pluginData.guild.channels.get(starboard.channel_id);
|
||||
if (!channel) return;
|
||||
|
||||
const embed: EmbedWith<"footer" | "author" | "fields" | "timestamp"> = {
|
||||
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;
|
||||
}
|
||||
|
||||
// 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) {
|
||||
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;
|
||||
} else if (msg.embeds[0].thumbnail) {
|
||||
embed.image = { url: msg.embeds[0].thumbnail.url };
|
||||
}
|
||||
|
||||
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 (imageAttachmentExtensions.includes(ext)) {
|
||||
embed.image = { url: attachment.url };
|
||||
break;
|
||||
}
|
||||
|
||||
if (audioAttachmentExtensions.includes(ext)) {
|
||||
embed.fields.push({ name: EMPTY_CHAR, value: `*Message contains an audio clip*` });
|
||||
break;
|
||||
}
|
||||
|
||||
if (videoAttachmentExtensions.includes(ext)) {
|
||||
embed.fields.push({ name: EMPTY_CHAR, value: `*Message contains a video*` });
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
embed.fields.push({ name: EMPTY_CHAR, value: `[Jump to message](${messageLink(msg)})` });
|
||||
const starCount = (await pluginData.state.starboardReactions.getAllReactionsForMessageId(msg.id)).length;
|
||||
const embed = createStarboardEmbedFromMessage(msg, Boolean(starboard.copy_full_embed));
|
||||
embed.fields!.push(createStarboardPseudoFooterForMessage(starboard, msg, starboard.star_emoji![0], starCount));
|
||||
|
||||
const starboardMessage = await (channel as TextChannel).createMessage({ embed });
|
||||
await pluginData.state.starboardMessages.createStarboardMessage(channel.id, msg.id, starboardMessage.id);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue