Migrate MessageSaver to new Plugin structure
This commit is contained in:
parent
b6257b9189
commit
f83d7122b9
7 changed files with 221 additions and 0 deletions
47
backend/src/plugins/MessageSaver/MessageSaverPlugin.ts
Normal file
47
backend/src/plugins/MessageSaver/MessageSaverPlugin.ts
Normal file
|
@ -0,0 +1,47 @@
|
||||||
|
import { zeppelinPlugin } from "../ZeppelinPluginBlueprint";
|
||||||
|
import { ConfigSchema, MessageSaverPluginType } from "./types";
|
||||||
|
import { GuildSavedMessages } from "../../data/GuildSavedMessages";
|
||||||
|
import { PluginOptions } from "knub";
|
||||||
|
import { MessageCreateEvt, MessageUpdateEvt, MessageDeleteEvt, MessageDeleteBulkEvt } from "./events/SaveMessagesEvts";
|
||||||
|
import { SaveMessagesToDBCmd } from "./commands/SaveMessagesToDB";
|
||||||
|
import { SavePinsToDBCmd } from "./commands/SavePinsToDB";
|
||||||
|
|
||||||
|
const defaultOptions: PluginOptions<MessageSaverPluginType> = {
|
||||||
|
config: {
|
||||||
|
can_manage: false,
|
||||||
|
},
|
||||||
|
overrides: [
|
||||||
|
{
|
||||||
|
level: ">=100",
|
||||||
|
config: {
|
||||||
|
can_manage: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
};
|
||||||
|
|
||||||
|
export const MessageSaverPlugin = zeppelinPlugin<MessageSaverPluginType>()("message_saver", {
|
||||||
|
configSchema: ConfigSchema,
|
||||||
|
defaultOptions,
|
||||||
|
|
||||||
|
showInDocs: false,
|
||||||
|
|
||||||
|
// prettier-ignore
|
||||||
|
commands: [
|
||||||
|
SaveMessagesToDBCmd,
|
||||||
|
SavePinsToDBCmd,
|
||||||
|
],
|
||||||
|
|
||||||
|
// prettier-ignore
|
||||||
|
events: [
|
||||||
|
MessageCreateEvt,
|
||||||
|
MessageUpdateEvt,
|
||||||
|
MessageDeleteEvt,
|
||||||
|
MessageDeleteBulkEvt,
|
||||||
|
],
|
||||||
|
|
||||||
|
onLoad(pluginData) {
|
||||||
|
const { state, guild } = pluginData;
|
||||||
|
state.savedMessages = GuildSavedMessages.getGuildInstance(guild.id);
|
||||||
|
},
|
||||||
|
});
|
|
@ -0,0 +1,31 @@
|
||||||
|
import { messageSaverCmd } from "../types";
|
||||||
|
import { commandTypeHelpers as ct } from "../../../commandTypes";
|
||||||
|
import { saveMessagesToDB } from "../saveMessagesToDB";
|
||||||
|
import { TextChannel } from "eris";
|
||||||
|
import { sendSuccessMessage } from "src/pluginUtils";
|
||||||
|
|
||||||
|
export const SaveMessagesToDBCmd = messageSaverCmd({
|
||||||
|
trigger: "save_messages_to_db",
|
||||||
|
permission: "can_manage",
|
||||||
|
source: "guild",
|
||||||
|
|
||||||
|
signature: {
|
||||||
|
channel: ct.textChannel(),
|
||||||
|
ids: ct.string({ catchAll: true }),
|
||||||
|
},
|
||||||
|
|
||||||
|
async run({ message: msg, args, pluginData }) {
|
||||||
|
await msg.channel.createMessage("Saving specified messages...");
|
||||||
|
const { savedCount, failed } = await saveMessagesToDB(pluginData, args.channel, args.ids.trim().split(" "));
|
||||||
|
|
||||||
|
if (failed.length) {
|
||||||
|
sendSuccessMessage(
|
||||||
|
pluginData,
|
||||||
|
msg.channel,
|
||||||
|
`Saved ${savedCount} messages. The following messages could not be saved: ${failed.join(", ")}`,
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
sendSuccessMessage(pluginData, msg.channel, `Saved ${savedCount} messages!`);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
});
|
35
backend/src/plugins/MessageSaver/commands/SavePinsToDB.ts
Normal file
35
backend/src/plugins/MessageSaver/commands/SavePinsToDB.ts
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
import { messageSaverCmd } from "../types";
|
||||||
|
import { commandTypeHelpers as ct } from "../../../commandTypes";
|
||||||
|
import { saveMessagesToDB } from "../saveMessagesToDB";
|
||||||
|
import { sendSuccessMessage } from "src/pluginUtils";
|
||||||
|
|
||||||
|
export const SavePinsToDBCmd = messageSaverCmd({
|
||||||
|
trigger: "save_pins_to_db",
|
||||||
|
permission: "can_manage",
|
||||||
|
source: "guild",
|
||||||
|
|
||||||
|
signature: {
|
||||||
|
channel: ct.textChannel(),
|
||||||
|
},
|
||||||
|
|
||||||
|
async run({ message: msg, args, pluginData }) {
|
||||||
|
await msg.channel.createMessage(`Saving pins from <#${args.channel.id}>...`);
|
||||||
|
|
||||||
|
const pins = await args.channel.getPins();
|
||||||
|
const { savedCount, failed } = await saveMessagesToDB(
|
||||||
|
pluginData,
|
||||||
|
args.channel,
|
||||||
|
pins.map(m => m.id),
|
||||||
|
);
|
||||||
|
|
||||||
|
if (failed.length) {
|
||||||
|
sendSuccessMessage(
|
||||||
|
pluginData,
|
||||||
|
msg.channel,
|
||||||
|
`Saved ${savedCount} messages. The following messages could not be saved: ${failed.join(", ")}`,
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
sendSuccessMessage(pluginData, msg.channel, `Saved ${savedCount} messages!`);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
});
|
53
backend/src/plugins/MessageSaver/events/SaveMessagesEvts.ts
Normal file
53
backend/src/plugins/MessageSaver/events/SaveMessagesEvts.ts
Normal file
|
@ -0,0 +1,53 @@
|
||||||
|
import { messageSaverEvt } from "../types";
|
||||||
|
import { Message } from "eris";
|
||||||
|
|
||||||
|
export const MessageCreateEvt = messageSaverEvt({
|
||||||
|
event: "messageCreate",
|
||||||
|
allowOutsideOfGuild: false,
|
||||||
|
|
||||||
|
async listener(meta) {
|
||||||
|
// Only save regular chat messages
|
||||||
|
if (meta.args.message.type !== 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
await meta.pluginData.state.savedMessages.createFromMsg(meta.args.message);
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
export const MessageUpdateEvt = messageSaverEvt({
|
||||||
|
event: "messageUpdate",
|
||||||
|
allowOutsideOfGuild: false,
|
||||||
|
|
||||||
|
async listener(meta) {
|
||||||
|
if (meta.args.message.type !== 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
await meta.pluginData.state.savedMessages.saveEditFromMsg(meta.args.message);
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
export const MessageDeleteEvt = messageSaverEvt({
|
||||||
|
event: "messageDelete",
|
||||||
|
allowOutsideOfGuild: false,
|
||||||
|
|
||||||
|
async listener(meta) {
|
||||||
|
const msg = meta.args.message as Message;
|
||||||
|
if (msg.type != null && msg.type !== 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
await meta.pluginData.state.savedMessages.markAsDeleted(msg.id);
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
export const MessageDeleteBulkEvt = messageSaverEvt({
|
||||||
|
event: "messageDeleteBulk",
|
||||||
|
allowOutsideOfGuild: false,
|
||||||
|
|
||||||
|
async listener(meta) {
|
||||||
|
const ids = meta.args.messages.map(m => m.id);
|
||||||
|
await meta.pluginData.state.savedMessages.markBulkAsDeleted(ids);
|
||||||
|
},
|
||||||
|
});
|
35
backend/src/plugins/MessageSaver/saveMessagesToDB.ts
Normal file
35
backend/src/plugins/MessageSaver/saveMessagesToDB.ts
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
import { MessageSaverPluginType } from "./types";
|
||||||
|
import { PluginData } from "knub";
|
||||||
|
import { TextChannel, Message } from "eris";
|
||||||
|
|
||||||
|
export async function saveMessagesToDB(
|
||||||
|
pluginData: PluginData<MessageSaverPluginType>,
|
||||||
|
channel: TextChannel,
|
||||||
|
ids: string[],
|
||||||
|
) {
|
||||||
|
const failed = [];
|
||||||
|
for (const id of ids) {
|
||||||
|
const savedMessage = await pluginData.state.savedMessages.find(id);
|
||||||
|
if (savedMessage) continue;
|
||||||
|
|
||||||
|
let thisMsg: Message;
|
||||||
|
|
||||||
|
try {
|
||||||
|
thisMsg = await channel.getMessage(id);
|
||||||
|
|
||||||
|
if (!thisMsg) {
|
||||||
|
failed.push(id);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
await pluginData.state.savedMessages.createFromMsg(thisMsg, { is_permanent: true });
|
||||||
|
} catch (e) {
|
||||||
|
failed.push(id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
savedCount: ids.length - failed.length,
|
||||||
|
failed,
|
||||||
|
};
|
||||||
|
}
|
18
backend/src/plugins/MessageSaver/types.ts
Normal file
18
backend/src/plugins/MessageSaver/types.ts
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
import * as t from "io-ts";
|
||||||
|
import { BasePluginType, command, eventListener } from "knub";
|
||||||
|
import { GuildSavedMessages } from "../../data/GuildSavedMessages";
|
||||||
|
|
||||||
|
export const ConfigSchema = t.type({
|
||||||
|
can_manage: t.boolean,
|
||||||
|
});
|
||||||
|
export type TConfigSchema = t.TypeOf<typeof ConfigSchema>;
|
||||||
|
|
||||||
|
export interface MessageSaverPluginType extends BasePluginType {
|
||||||
|
config: TConfigSchema;
|
||||||
|
state: {
|
||||||
|
savedMessages: GuildSavedMessages;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export const messageSaverCmd = command<MessageSaverPluginType>();
|
||||||
|
export const messageSaverEvt = eventListener<MessageSaverPluginType>();
|
|
@ -1,10 +1,12 @@
|
||||||
import { UtilityPlugin } from "./Utility/UtilityPlugin";
|
import { UtilityPlugin } from "./Utility/UtilityPlugin";
|
||||||
import { LocateUserPlugin } from "./LocateUser/LocateUserPlugin";
|
import { LocateUserPlugin } from "./LocateUser/LocateUserPlugin";
|
||||||
import { ZeppelinPluginBlueprint } from "./ZeppelinPluginBlueprint";
|
import { ZeppelinPluginBlueprint } from "./ZeppelinPluginBlueprint";
|
||||||
|
import { MessageSaverPlugin } from "./MessageSaver/MessageSaverPlugin";
|
||||||
|
|
||||||
// prettier-ignore
|
// prettier-ignore
|
||||||
export const guildPlugins: Array<ZeppelinPluginBlueprint<any>> = [
|
export const guildPlugins: Array<ZeppelinPluginBlueprint<any>> = [
|
||||||
LocateUserPlugin,
|
LocateUserPlugin,
|
||||||
|
MessageSaverPlugin,
|
||||||
UtilityPlugin,
|
UtilityPlugin,
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue