2019-04-13 01:44:18 +03:00
|
|
|
import { Plugin, decorators as d, IPluginOptions } from "knub";
|
2018-12-22 13:57:55 +02:00
|
|
|
import { GuildChannel, Message, TextChannel } from "eris";
|
2018-11-24 14:01:06 +02:00
|
|
|
import { GuildSavedMessages } from "../data/GuildSavedMessages";
|
2018-12-22 13:57:55 +02:00
|
|
|
import { successMessage } from "../utils";
|
2019-07-11 12:23:57 +03:00
|
|
|
import { ZeppelinPlugin } from "./ZeppelinPlugin";
|
2019-07-21 21:15:52 +03:00
|
|
|
import * as t from "io-ts";
|
2018-11-24 14:01:06 +02:00
|
|
|
|
2019-07-21 21:15:52 +03:00
|
|
|
const ConfigSchema = t.type({
|
|
|
|
can_manage: t.boolean,
|
|
|
|
});
|
|
|
|
type TConfigSchema = t.TypeOf<typeof ConfigSchema>;
|
2019-03-04 21:44:04 +02:00
|
|
|
|
2019-07-21 21:15:52 +03:00
|
|
|
export class MessageSaverPlugin extends ZeppelinPlugin<TConfigSchema> {
|
2019-01-13 23:31:09 +02:00
|
|
|
public static pluginName = "message_saver";
|
2019-08-22 01:22:26 +03:00
|
|
|
public static showInDocs = false;
|
2019-08-22 02:58:32 +03:00
|
|
|
public static configSchema = ConfigSchema;
|
2019-01-03 06:15:28 +02:00
|
|
|
|
2018-11-24 14:18:48 +02:00
|
|
|
protected savedMessages: GuildSavedMessages;
|
2018-11-24 14:01:06 +02:00
|
|
|
|
2019-08-22 01:22:26 +03:00
|
|
|
public static getStaticDefaultOptions(): IPluginOptions<TConfigSchema> {
|
2018-12-22 13:57:55 +02:00
|
|
|
return {
|
2019-04-13 01:44:18 +03:00
|
|
|
config: {
|
|
|
|
can_manage: false,
|
2018-12-22 13:57:55 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
overrides: [
|
|
|
|
{
|
|
|
|
level: ">=100",
|
2019-04-13 01:44:18 +03:00
|
|
|
config: {
|
|
|
|
can_manage: true,
|
2019-02-17 15:19:55 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
2018-12-22 13:57:55 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-11-24 14:01:06 +02:00
|
|
|
onLoad() {
|
2019-05-25 21:25:34 +03:00
|
|
|
this.savedMessages = GuildSavedMessages.getGuildInstance(this.guildId);
|
2018-11-24 14:01:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@d.event("messageCreate", "guild", false)
|
|
|
|
async onMessageCreate(msg: Message) {
|
|
|
|
// Only save regular chat messages
|
|
|
|
if (msg.type !== 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-11-24 14:18:48 +02:00
|
|
|
await this.savedMessages.createFromMsg(msg);
|
2018-11-24 14:01:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@d.event("messageDelete", "guild", false)
|
|
|
|
async onMessageDelete(msg: Message) {
|
2018-11-24 14:18:48 +02:00
|
|
|
if (msg.type != null && msg.type !== 0) {
|
|
|
|
return;
|
|
|
|
}
|
2018-11-24 14:01:06 +02:00
|
|
|
|
2018-11-24 14:18:48 +02:00
|
|
|
await this.savedMessages.markAsDeleted(msg.id);
|
2018-11-24 14:01:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@d.event("messageUpdate", "guild", false)
|
|
|
|
async onMessageUpdate(msg: Message) {
|
2018-11-24 14:18:48 +02:00
|
|
|
if (msg.type !== 0) {
|
|
|
|
return;
|
|
|
|
}
|
2018-11-24 14:01:06 +02:00
|
|
|
|
2018-11-24 14:18:48 +02:00
|
|
|
await this.savedMessages.saveEditFromMsg(msg);
|
2018-11-24 14:01:06 +02:00
|
|
|
}
|
2018-11-24 18:39:17 +02:00
|
|
|
|
|
|
|
@d.event("messageDeleteBulk", "guild", false)
|
|
|
|
async onMessageBulkDelete(messages: Message[]) {
|
|
|
|
const ids = messages.map(m => m.id);
|
|
|
|
await this.savedMessages.markBulkAsDeleted(ids);
|
|
|
|
}
|
2018-12-22 13:57:55 +02:00
|
|
|
|
2018-12-22 14:10:38 +02:00
|
|
|
async saveMessagesToDB(channel: GuildChannel & TextChannel, ids: string[]) {
|
2018-12-22 13:57:55 +02:00
|
|
|
const failed = [];
|
2018-12-22 14:10:38 +02:00
|
|
|
for (const id of ids) {
|
2018-12-22 13:57:55 +02:00
|
|
|
const savedMessage = await this.savedMessages.find(id);
|
|
|
|
if (savedMessage) continue;
|
|
|
|
|
|
|
|
let thisMsg: Message;
|
|
|
|
|
|
|
|
try {
|
2018-12-22 14:10:38 +02:00
|
|
|
thisMsg = await channel.getMessage(id);
|
2018-12-22 13:57:55 +02:00
|
|
|
|
|
|
|
if (!thisMsg) {
|
|
|
|
failed.push(id);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
await this.savedMessages.createFromMsg(thisMsg, { is_permanent: true });
|
|
|
|
} catch (e) {
|
|
|
|
failed.push(id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-22 14:10:38 +02:00
|
|
|
return {
|
|
|
|
savedCount: ids.length - failed.length,
|
2019-02-17 15:19:55 +02:00
|
|
|
failed,
|
2018-12-22 14:10:38 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
@d.command("save_messages_to_db", "<channel:channel> <ids:string...>")
|
2019-06-11 10:26:50 +03:00
|
|
|
@d.permission("can_manage")
|
2018-12-22 14:10:38 +02:00
|
|
|
async saveMessageCmd(msg: Message, args: { channel: GuildChannel & TextChannel; ids: string[] }) {
|
|
|
|
await msg.channel.createMessage("Saving specified messages...");
|
|
|
|
|
|
|
|
const { savedCount, failed } = await this.saveMessagesToDB(args.channel, args.ids);
|
|
|
|
|
|
|
|
if (failed.length) {
|
2020-01-12 22:31:30 +11:00
|
|
|
this.sendSuccessMessage(
|
|
|
|
msg.channel,
|
|
|
|
`Saved ${savedCount} messages. The following messages could not be saved: ${failed.join(", ")}`,
|
|
|
|
);
|
2018-12-22 14:10:38 +02:00
|
|
|
} else {
|
2020-01-11 00:39:32 +11:00
|
|
|
this.sendSuccessMessage(msg.channel, `Saved ${savedCount} messages!`);
|
2018-12-22 14:10:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@d.command("save_pins_to_db", "<channel:channel>")
|
2019-06-11 10:26:50 +03:00
|
|
|
@d.permission("can_manage")
|
2018-12-22 14:10:38 +02:00
|
|
|
async savePinsCmd(msg: Message, args: { channel: GuildChannel & TextChannel }) {
|
|
|
|
await msg.channel.createMessage(`Saving pins from <#${args.channel.id}>...`);
|
|
|
|
|
|
|
|
const pins = await args.channel.getPins();
|
2020-01-12 11:47:54 +02:00
|
|
|
const { savedCount, failed } = await this.saveMessagesToDB(
|
|
|
|
args.channel,
|
|
|
|
pins.map(m => m.id),
|
|
|
|
);
|
2018-12-22 14:10:38 +02:00
|
|
|
|
2018-12-22 13:57:55 +02:00
|
|
|
if (failed.length) {
|
2020-01-12 22:31:30 +11:00
|
|
|
this.sendSuccessMessage(
|
|
|
|
msg.channel,
|
|
|
|
`Saved ${savedCount} messages. The following messages could not be saved: ${failed.join(", ")}`,
|
|
|
|
);
|
|
|
|
} else {
|
2020-01-11 00:39:32 +11:00
|
|
|
this.sendSuccessMessage(msg.channel, `Saved ${savedCount} messages!`);
|
2018-12-22 13:57:55 +02:00
|
|
|
}
|
|
|
|
}
|
2018-11-24 14:01:06 +02:00
|
|
|
}
|