3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-05-13 13:25:03 +00:00

Migrate Starboard to new Plugin structure

This commit is contained in:
Dark 2020-07-22 23:15:40 +02:00
parent a3d0ec03d9
commit 599a504b17
12 changed files with 443 additions and 0 deletions

View file

@ -0,0 +1,19 @@
import { TStarboardOpts, StarboardPluginType, defaultStarboardOpts } from "../types";
import { PluginData } from "knub";
export function getStarboardOptsForStarboardChannel(
pluginData: PluginData<StarboardPluginType>,
starboardChannel,
): TStarboardOpts[] {
const config = pluginData.config.getForChannel(starboardChannel);
const configs = Object.values(config.boards).filter(opts => opts.channel_id === starboardChannel.id);
configs.forEach(cfg => {
if (cfg.enabled == null) cfg.enabled = defaultStarboardOpts.enabled;
if (cfg.star_emoji == null) cfg.star_emoji = defaultStarboardOpts.star_emoji;
if (cfg.stars_required == null) cfg.stars_required = defaultStarboardOpts.stars_required;
if (cfg.copy_full_embed == null) cfg.copy_full_embed = false;
});
return configs;
}

View file

@ -0,0 +1,27 @@
import { SavedMessage } from "src/data/entities/SavedMessage";
import { PluginData } from "knub";
import { StarboardPluginType } from "../types";
import { removeMessageFromStarboard } from "./removeMessageFromStarboard";
import { removeMessageFromStarboardMessages } from "./removeMessageFromStarboardMessages";
export async function onMessageDelete(pluginData: PluginData<StarboardPluginType>, msg: SavedMessage) {
// Deleted source message
const starboardMessages = await pluginData.state.starboardMessages.getStarboardMessagesForMessageId(msg.id);
for (const starboardMessage of starboardMessages) {
removeMessageFromStarboard(pluginData, starboardMessage);
}
// Deleted message from the starboard
const deletedStarboardMessages = await pluginData.state.starboardMessages.getStarboardMessagesForStarboardMessageId(
msg.id,
);
if (deletedStarboardMessages.length === 0) return;
for (const starboardMessage of deletedStarboardMessages) {
removeMessageFromStarboardMessages(
pluginData,
starboardMessage.starboard_message_id,
starboardMessage.starboard_channel_id,
);
}
}

View file

@ -0,0 +1,12 @@
import { PartialConfigSchema, defaultStarboardOpts } from "../types";
import * as t from "io-ts";
export function preprocessStaticConfig(config: t.TypeOf<typeof PartialConfigSchema>) {
if (config.boards) {
for (const [name, opts] of Object.entries(config.boards)) {
config.boards[name] = Object.assign({}, defaultStarboardOpts, config.boards[name]);
}
}
return config;
}

View file

@ -0,0 +1,6 @@
import { StarboardMessage } from "src/data/entities/StarboardMessage";
import { noop } from "src/utils";
export async function removeMessageFromStarboard(pluginData, msg: StarboardMessage) {
await pluginData.client.deleteMessage(msg.starboard_channel_id, msg.starboard_message_id).catch(noop);
}

View file

@ -0,0 +1,3 @@
export async function removeMessageFromStarboardMessages(pluginData, starboard_message_id: string, channel_id: string) {
await pluginData.state.starboardMessages.deleteStarboardMessage(starboard_message_id, channel_id);
}

View file

@ -0,0 +1,70 @@
import { PluginData } from "knub";
import { StarboardPluginType, TStarboardOpts } from "../types";
import { Message, GuildChannel, TextChannel, Embed } from "eris";
import moment from "moment-timezone";
import { EMPTY_CHAR, messageLink } from "src/utils";
import path from "path";
export async function saveMessageToStarboard(
pluginData: PluginData<StarboardPluginType>,
msg: Message,
starboard: TStarboardOpts,
) {
const channel = pluginData.guild.channels.get(starboard.channel_id);
if (!channel) return;
const time = moment(msg.timestamp, "x").format("YYYY-MM-DD [at] HH:mm:ss [UTC]");
const embed: Embed = {
footer: {
text: `#${(msg.channel as GuildChannel).name}`,
},
author: {
name: `${msg.author.username}#${msg.author.discriminator}`,
},
fields: [],
timestamp: new Date(msg.timestamp).toISOString(),
type: "rich",
};
if (msg.author.avatarURL) {
embed.author.icon_url = msg.author.avatarURL;
}
if (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;
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);
}