Merge pull request #82 from DarkView/k30_starboard
[K30] Migrated Starboard
This commit is contained in:
commit
9bbf60c58b
12 changed files with 443 additions and 0 deletions
118
backend/src/plugins/Starboard/StarboardPlugin.ts
Normal file
118
backend/src/plugins/Starboard/StarboardPlugin.ts
Normal file
|
@ -0,0 +1,118 @@
|
|||
import { PluginOptions } from "knub";
|
||||
import { ConfigSchema, StarboardPluginType } from "./types";
|
||||
import { zeppelinPlugin } from "../ZeppelinPluginBlueprint";
|
||||
import { trimPluginDescription } from "src/utils";
|
||||
import { GuildSavedMessages } from "src/data/GuildSavedMessages";
|
||||
import { GuildStarboardMessages } from "src/data/GuildStarboardMessages";
|
||||
import { GuildStarboardReactions } from "src/data/GuildStarboardReactions";
|
||||
import { onMessageDelete } from "./util/onMessageDelete";
|
||||
import { MigratePinsCmd } from "./commands/MigratePinsCmd";
|
||||
import { StarboardReactionAddEvt } from "./events/StarboardReactionAddEvt";
|
||||
import { StarboardReactionRemoveEvt, StarboardReactionRemoveAllEvt } from "./events/StarboardReactionRemoveEvts";
|
||||
|
||||
const defaultOptions: PluginOptions<StarboardPluginType> = {
|
||||
config: {
|
||||
can_migrate: false,
|
||||
boards: {},
|
||||
},
|
||||
|
||||
overrides: [
|
||||
{
|
||||
level: ">=100",
|
||||
config: {
|
||||
can_migrate: true,
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
export const StarboardPlugin = zeppelinPlugin<StarboardPluginType>()("starboard", {
|
||||
configSchema: ConfigSchema,
|
||||
defaultOptions,
|
||||
|
||||
info: {
|
||||
prettyName: "Starboard",
|
||||
description: trimPluginDescription(`
|
||||
This plugin allows you to set up starboards on your server. Starboards are like user voted pins where messages with enough reactions get immortalized on a "starboard" channel.
|
||||
`),
|
||||
configurationGuide: trimPluginDescription(`
|
||||
### Note on emojis
|
||||
To specify emoji in the config, you need to use the emoji's "raw form".
|
||||
To obtain this, post the emoji with a backslash in front of it.
|
||||
|
||||
- Example with a default emoji: "\:star:" => "⭐"
|
||||
- Example with a custom emoji: "\:mrvnSmile:" => "<:mrvnSmile:543000534102310933>"
|
||||
|
||||
### Basic starboard
|
||||
Any message on the server that gets 5 star reactions will be posted into the starboard channel (604342689038729226).
|
||||
|
||||
~~~yml
|
||||
starboard:
|
||||
config:
|
||||
boards:
|
||||
basic:
|
||||
channel_id: "604342689038729226"
|
||||
stars_required: 5
|
||||
~~~
|
||||
|
||||
### Custom star emoji
|
||||
This is identical to the basic starboard above, but accepts two emoji: the regular star and a custom :mrvnSmile: emoji
|
||||
|
||||
~~~yml
|
||||
starboard:
|
||||
config:
|
||||
boards:
|
||||
basic:
|
||||
channel_id: "604342689038729226"
|
||||
star_emoji: ["⭐", "<:mrvnSmile:543000534102310933>"]
|
||||
stars_required: 5
|
||||
~~~
|
||||
|
||||
### Limit starboard to a specific channel
|
||||
This is identical to the basic starboard above, but only works from a specific channel (473087035574321152).
|
||||
|
||||
~~~yml
|
||||
starboard:
|
||||
config:
|
||||
boards:
|
||||
basic:
|
||||
enabled: false # The starboard starts disabled and is then enabled in a channel override below
|
||||
channel_id: "604342689038729226"
|
||||
stars_required: 5
|
||||
overrides:
|
||||
- channel: "473087035574321152"
|
||||
config:
|
||||
boards:
|
||||
basic:
|
||||
enabled: true
|
||||
~~~
|
||||
`),
|
||||
},
|
||||
|
||||
// prettier-ignore
|
||||
commands: [
|
||||
MigratePinsCmd,
|
||||
],
|
||||
|
||||
// prettier-ignore
|
||||
events: [
|
||||
StarboardReactionAddEvt,
|
||||
StarboardReactionRemoveEvt,
|
||||
StarboardReactionRemoveAllEvt,
|
||||
],
|
||||
|
||||
onLoad(pluginData) {
|
||||
const { state, guild } = pluginData;
|
||||
|
||||
state.savedMessages = GuildSavedMessages.getGuildInstance(guild.id);
|
||||
state.starboardMessages = GuildStarboardMessages.getGuildInstance(guild.id);
|
||||
state.starboardReactions = GuildStarboardReactions.getGuildInstance(guild.id);
|
||||
|
||||
state.onMessageDeleteFn = msg => onMessageDelete(pluginData, msg);
|
||||
state.savedMessages.events.on("delete", state.onMessageDeleteFn);
|
||||
},
|
||||
|
||||
onUnload(pluginData) {
|
||||
pluginData.state.savedMessages.events.off("delete", pluginData.state.onMessageDeleteFn);
|
||||
},
|
||||
});
|
52
backend/src/plugins/Starboard/commands/MigratePinsCmd.ts
Normal file
52
backend/src/plugins/Starboard/commands/MigratePinsCmd.ts
Normal file
|
@ -0,0 +1,52 @@
|
|||
import { commandTypeHelpers as ct } from "../../../commandTypes";
|
||||
import { starboardCmd } from "../types";
|
||||
import { sendSuccessMessage, sendErrorMessage } from "src/pluginUtils";
|
||||
import { TextChannel } from "eris";
|
||||
import { saveMessageToStarboard } from "../util/saveMessageToStarboard";
|
||||
|
||||
export const MigratePinsCmd = starboardCmd({
|
||||
trigger: "starboard migrate_pins",
|
||||
permission: "can_migrate",
|
||||
|
||||
description: "Posts all pins from a channel to the specified starboard. The pins are NOT unpinned automatically.",
|
||||
|
||||
signature: {
|
||||
pinChannel: ct.textChannel(),
|
||||
starboardName: ct.string(),
|
||||
},
|
||||
|
||||
async run({ message: msg, args, pluginData }) {
|
||||
const config = await pluginData.config.get();
|
||||
const starboard = config.boards[args.starboardName];
|
||||
if (!starboard) {
|
||||
sendErrorMessage(pluginData, msg.channel, "Unknown starboard specified");
|
||||
return;
|
||||
}
|
||||
|
||||
const starboardChannel = pluginData.guild.channels.get(starboard.channel_id);
|
||||
if (!starboardChannel || !(starboardChannel instanceof TextChannel)) {
|
||||
sendErrorMessage(pluginData, msg.channel, "Starboard has an unknown/invalid channel id");
|
||||
return;
|
||||
}
|
||||
|
||||
msg.channel.createMessage(`Migrating pins from <#${args.pinChannel.id}> to <#${starboardChannel.id}>...`);
|
||||
|
||||
const pins = await args.pinChannel.getPins();
|
||||
pins.reverse(); // Migrate pins starting from the oldest message
|
||||
|
||||
for (const pin of pins) {
|
||||
const existingStarboardMessage = await pluginData.state.starboardMessages.getMatchingStarboardMessages(
|
||||
starboardChannel.id,
|
||||
pin.id,
|
||||
);
|
||||
if (existingStarboardMessage.length > 0) continue;
|
||||
await saveMessageToStarboard(pluginData, pin, starboard);
|
||||
}
|
||||
|
||||
sendSuccessMessage(
|
||||
pluginData,
|
||||
msg.channel,
|
||||
`Pins migrated from <#${args.pinChannel.id}> to <#${starboardChannel.id}>!`,
|
||||
);
|
||||
},
|
||||
});
|
|
@ -0,0 +1,74 @@
|
|||
import { starboardEvt } from "../types";
|
||||
import { Message } from "eris";
|
||||
import { UnknownUser, resolveMember, noop } from "src/utils";
|
||||
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;
|
||||
const userId = meta.args.userID;
|
||||
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;
|
||||
|
||||
const user = await resolveMember(pluginData.client, pluginData.guild, userId);
|
||||
if (user instanceof UnknownUser) return;
|
||||
if (user.bot) return;
|
||||
|
||||
const config = pluginData.config.getMatchingConfig({ member: user, channelId: msg.channel.id });
|
||||
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 => {
|
||||
return board.star_emoji.some((boardEmoji: string) => {
|
||||
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);
|
||||
}
|
||||
}
|
||||
},
|
||||
});
|
|
@ -0,0 +1,17 @@
|
|||
import { starboardEvt } from "../types";
|
||||
|
||||
export const StarboardReactionRemoveEvt = starboardEvt({
|
||||
event: "messageReactionRemove",
|
||||
|
||||
async listener(meta) {
|
||||
await meta.pluginData.state.starboardReactions.deleteStarboardReaction(meta.args.message.id, meta.args.userID);
|
||||
},
|
||||
});
|
||||
|
||||
export const StarboardReactionRemoveAllEvt = starboardEvt({
|
||||
event: "messageReactionRemoveAll",
|
||||
|
||||
async listener(meta) {
|
||||
await meta.pluginData.state.starboardReactions.deleteAllStarboardReactionsForMessageId(meta.args.message.id);
|
||||
},
|
||||
});
|
43
backend/src/plugins/Starboard/types.ts
Normal file
43
backend/src/plugins/Starboard/types.ts
Normal file
|
@ -0,0 +1,43 @@
|
|||
import * as t from "io-ts";
|
||||
import { BasePluginType, command, eventListener } from "knub";
|
||||
import { tNullable, tDeepPartial } from "src/utils";
|
||||
import { GuildSavedMessages } from "src/data/GuildSavedMessages";
|
||||
import { GuildStarboardMessages } from "src/data/GuildStarboardMessages";
|
||||
import { GuildStarboardReactions } from "src/data/GuildStarboardReactions";
|
||||
|
||||
const StarboardOpts = t.type({
|
||||
channel_id: t.string,
|
||||
stars_required: t.number,
|
||||
star_emoji: tNullable(t.array(t.string)),
|
||||
copy_full_embed: tNullable(t.boolean),
|
||||
enabled: tNullable(t.boolean),
|
||||
});
|
||||
export type TStarboardOpts = t.TypeOf<typeof StarboardOpts>;
|
||||
|
||||
export const ConfigSchema = t.type({
|
||||
boards: t.record(t.string, StarboardOpts),
|
||||
can_migrate: t.boolean,
|
||||
});
|
||||
export type TConfigSchema = t.TypeOf<typeof ConfigSchema>;
|
||||
|
||||
export const PartialConfigSchema = tDeepPartial(ConfigSchema);
|
||||
|
||||
export const defaultStarboardOpts: Partial<TStarboardOpts> = {
|
||||
star_emoji: ["⭐"],
|
||||
enabled: true,
|
||||
};
|
||||
|
||||
export interface StarboardPluginType extends BasePluginType {
|
||||
config: TConfigSchema;
|
||||
|
||||
state: {
|
||||
savedMessages: GuildSavedMessages;
|
||||
starboardMessages: GuildStarboardMessages;
|
||||
starboardReactions: GuildStarboardReactions;
|
||||
|
||||
onMessageDeleteFn;
|
||||
};
|
||||
}
|
||||
|
||||
export const starboardCmd = command<StarboardPluginType>();
|
||||
export const starboardEvt = eventListener<StarboardPluginType>();
|
|
@ -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;
|
||||
}
|
27
backend/src/plugins/Starboard/util/onMessageDelete.ts
Normal file
27
backend/src/plugins/Starboard/util/onMessageDelete.ts
Normal 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,
|
||||
);
|
||||
}
|
||||
}
|
12
backend/src/plugins/Starboard/util/preprocessStaticConfig.ts
Normal file
12
backend/src/plugins/Starboard/util/preprocessStaticConfig.ts
Normal 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;
|
||||
}
|
|
@ -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);
|
||||
}
|
|
@ -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);
|
||||
}
|
70
backend/src/plugins/Starboard/util/saveMessageToStarboard.ts
Normal file
70
backend/src/plugins/Starboard/util/saveMessageToStarboard.ts
Normal 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);
|
||||
}
|
|
@ -13,6 +13,7 @@ import { GuildConfigReloaderPlugin } from "./GuildConfigReloader/GuildConfigRelo
|
|||
import { CasesPlugin } from "./Cases/CasesPlugin";
|
||||
import { MutesPlugin } from "./Mutes/MutesPlugin";
|
||||
import { TagsPlugin } from "./Tags/TagsPlugin";
|
||||
import { StarboardPlugin } from "./Starboard/StarboardPlugin";
|
||||
import { ChannelArchiverPlugin } from "./ChannelArchiver/ChannelArchiverPlugin";
|
||||
|
||||
// prettier-ignore
|
||||
|
@ -25,6 +26,7 @@ export const guildPlugins: Array<ZeppelinPluginBlueprint<any>> = [
|
|||
MessageSaverPlugin,
|
||||
NameHistoryPlugin,
|
||||
RemindersPlugin,
|
||||
StarboardPlugin,
|
||||
TagsPlugin,
|
||||
UsernameSaverPlugin,
|
||||
UtilityPlugin,
|
||||
|
|
Loading…
Add table
Reference in a new issue