zappyzep/src/plugins/Post.ts

215 lines
6.2 KiB
TypeScript
Raw Normal View History

import { decorators as d, IPluginOptions } from "knub";
import { Channel, EmbedBase, Message, Role, TextChannel } from "eris";
import { errorMessage, downloadFile, getRoleMentions } from "../utils";
2018-11-24 14:19:47 +02:00
import { GuildSavedMessages } from "../data/GuildSavedMessages";
import { ZeppelinPlugin } from "./ZeppelinPlugin";
2019-01-15 03:04:47 +02:00
import fs from "fs";
const fsp = fs.promises;
2019-01-15 03:58:58 +02:00
const COLOR_MATCH_REGEX = /^#?([0-9a-f]{6})$/;
interface IPostPluginConfig {
can_post: boolean;
}
export class PostPlugin extends ZeppelinPlugin<IPostPluginConfig> {
2019-01-15 03:04:47 +02:00
public static pluginName = "post";
2018-11-24 14:19:47 +02:00
protected savedMessages: GuildSavedMessages;
onLoad() {
this.savedMessages = GuildSavedMessages.getInstance(this.guildId);
}
getDefaultOptions(): IPluginOptions<IPostPluginConfig> {
return {
config: {
can_post: false,
},
overrides: [
{
level: ">=100",
config: {
can_post: true,
2019-02-09 14:47:50 +02:00
},
},
],
};
}
2019-02-09 14:47:50 +02:00
protected formatContent(str) {
return str.replace(/\\n/g, "\n");
}
/**
2019-01-15 03:04:47 +02:00
* COMMAND: Post a message as the bot to the specified channel
*/
@d.command("post", "<channel:channel> [content:string$]", {
options: [
{
name: "enable-mentions",
type: "bool",
},
],
})
@d.permission("can_post")
async postCmd(msg: Message, args: { channel: Channel; content?: string; "enable-mentions": boolean }) {
if (!(args.channel instanceof TextChannel)) {
msg.channel.createMessage(errorMessage("Channel is not a text channel"));
return;
}
const content: string = (args.content && this.formatContent(args.content)) || undefined;
2019-01-15 03:04:47 +02:00
let downloadedAttachment;
let file;
if (msg.attachments.length) {
downloadedAttachment = await downloadFile(msg.attachments[0].url);
file = {
name: msg.attachments[0].filename,
2019-02-09 14:47:50 +02:00
file: await fsp.readFile(downloadedAttachment.path),
2019-01-15 03:04:47 +02:00
};
}
if (content == null && file == null) {
msg.channel.createMessage(errorMessage("Text content or attachment required"));
return;
}
const rolesMadeMentionable: Role[] = [];
if (args["enable-mentions"] && content) {
const mentionedRoleIds = getRoleMentions(content);
if (mentionedRoleIds != null) {
for (const roleId of mentionedRoleIds) {
const role = this.guild.roles.get(roleId);
if (role && !role.mentionable) {
await role.edit({
mentionable: true,
});
rolesMadeMentionable.push(role);
}
}
}
}
2019-01-15 03:04:47 +02:00
const createdMsg = await args.channel.createMessage(content, file);
await this.savedMessages.setPermanent(createdMsg.id);
2019-01-15 03:04:47 +02:00
for (const role of rolesMadeMentionable) {
role.edit({
mentionable: false,
});
}
2019-01-15 03:04:47 +02:00
if (downloadedAttachment) {
downloadedAttachment.deleteFn();
}
}
2019-01-15 03:58:58 +02:00
/**
* COMMAND: Post a message with an embed as the bot to the specified channel
*/
2019-01-15 03:39:39 +02:00
@d.command("post_embed", "<channel:channel>", {
2019-02-09 14:47:50 +02:00
options: [
{ name: "title", type: "string" },
{ name: "content", type: "string" },
{ name: "color", type: "string" },
],
2019-01-15 03:39:39 +02:00
})
@d.permission("can_post")
2019-01-15 03:39:39 +02:00
async postEmbedCmd(msg: Message, args: { channel: Channel; title?: string; content?: string; color?: string }) {
if (!(args.channel instanceof TextChannel)) {
msg.channel.createMessage(errorMessage("Channel is not a text channel"));
return;
}
if (!args.title && !args.content) {
msg.channel.createMessage(errorMessage("Title or content required"));
return;
}
let color = null;
if (args.color) {
2019-01-15 03:58:58 +02:00
const colorMatch = args.color.match(COLOR_MATCH_REGEX);
2019-01-15 03:39:39 +02:00
if (!colorMatch) {
msg.channel.createMessage(errorMessage("Invalid color specified, use hex colors"));
return;
}
color = parseInt(colorMatch[1], 16);
}
const embed: EmbedBase = {};
if (args.title) embed.title = args.title;
2019-02-09 14:47:50 +02:00
if (args.content) embed.description = this.formatContent(args.content);
2019-01-15 03:39:39 +02:00
if (color) embed.color = color;
const createdMsg = await args.channel.createMessage({ embed });
await this.savedMessages.setPermanent(createdMsg.id);
}
/**
2019-01-15 03:58:58 +02:00
* COMMAND: Edit the specified message posted by the bot
*/
@d.command("edit", "<messageId:string> <content:string$>")
@d.permission("can_post")
async editCmd(msg, args: { messageId: string; content: string }) {
const savedMessage = await this.savedMessages.find(args.messageId);
if (!savedMessage) {
msg.channel.createMessage(errorMessage("Unknown message"));
return;
}
if (savedMessage.user_id !== this.bot.user.id) {
msg.channel.createMessage(errorMessage("Message wasn't posted by me"));
return;
}
2019-02-09 14:47:50 +02:00
await this.bot.editMessage(savedMessage.channel_id, savedMessage.id, this.formatContent(args.content));
}
2019-01-15 03:58:58 +02:00
/**
* COMMAND: Edit the specified message with an embed posted by the bot
*/
@d.command("edit_embed", "<messageId:string>", {
2019-02-09 14:47:50 +02:00
options: [
{ name: "title", type: "string" },
{ name: "content", type: "string" },
{ name: "color", type: "string" },
],
2019-01-15 03:58:58 +02:00
})
@d.permission("can_post")
2019-01-15 03:58:58 +02:00
async editEmbedCmd(msg: Message, args: { messageId: string; title?: string; content?: string; color?: string }) {
const savedMessage = await this.savedMessages.find(args.messageId);
if (!savedMessage) {
msg.channel.createMessage(errorMessage("Unknown message"));
return;
}
if (!args.title && !args.content) {
msg.channel.createMessage(errorMessage("Title or content required"));
return;
}
let color = null;
if (args.color) {
const colorMatch = args.color.match(COLOR_MATCH_REGEX);
if (!colorMatch) {
msg.channel.createMessage(errorMessage("Invalid color specified, use hex colors"));
return;
}
color = parseInt(colorMatch[1], 16);
}
const embed: EmbedBase = savedMessage.data.embeds[0];
if (args.title) embed.title = args.title;
2019-02-09 14:47:50 +02:00
if (args.content) embed.description = this.formatContent(args.content);
2019-01-15 03:58:58 +02:00
if (color) embed.color = color;
await this.bot.editMessage(savedMessage.channel_id, savedMessage.id, { embed });
}
}