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

Migrate Post to new Plugin structure

This commit is contained in:
Dark 2020-07-23 21:26:22 +02:00
parent ebcb28261b
commit 5c070643a3
15 changed files with 752 additions and 0 deletions

View file

@ -0,0 +1,30 @@
import { postCmd } from "../types";
import { commandTypeHelpers as ct } from "../../../commandTypes";
import { sendErrorMessage, sendSuccessMessage } from "src/pluginUtils";
import { formatContent } from "../util/formatContent";
export const EditCmd = postCmd({
trigger: "edit",
permission: "can_post",
signature: {
messageId: ct.string(),
content: ct.string({ catchAll: true }),
},
async run({ message: msg, args, pluginData }) {
const savedMessage = await pluginData.state.savedMessages.find(args.messageId);
if (!savedMessage) {
sendErrorMessage(pluginData, msg.channel, "Unknown message");
return;
}
if (savedMessage.user_id !== pluginData.client.user.id) {
sendErrorMessage(pluginData, msg.channel, "Message wasn't posted by me");
return;
}
await pluginData.client.editMessage(savedMessage.channel_id, savedMessage.id, formatContent(args.content));
sendSuccessMessage(pluginData, msg.channel, "Message edited");
},
});

View file

@ -0,0 +1,63 @@
import { postCmd } from "../types";
import { commandTypeHelpers as ct } from "../../../commandTypes";
import { sendErrorMessage, sendSuccessMessage } from "src/pluginUtils";
import { Embed } from "eris";
import { trimLines } from "src/utils";
import { formatContent } from "../util/formatContent";
const COLOR_MATCH_REGEX = /^#?([0-9a-f]{6})$/;
export const EditEmbedCmd = postCmd({
trigger: "edit_embed",
permission: "can_post",
signature: {
messageId: ct.string(),
maincontent: ct.string({ catchAll: true }),
title: ct.string({ option: true }),
content: ct.string({ option: true }),
color: ct.string({ option: true }),
},
async run({ message: msg, args, pluginData }) {
const savedMessage = await pluginData.state.savedMessages.find(args.messageId);
if (!savedMessage) {
sendErrorMessage(pluginData, msg.channel, "Unknown message");
return;
}
const content = args.content || args.maincontent;
let color = null;
if (args.color) {
const colorMatch = args.color.match(COLOR_MATCH_REGEX);
if (!colorMatch) {
sendErrorMessage(pluginData, msg.channel, "Invalid color specified, use hex colors");
return;
}
color = parseInt(colorMatch[1], 16);
}
const embed: Embed = savedMessage.data.embeds[0] as Embed;
embed.type = "rich";
if (args.title) embed.title = args.title;
if (content) embed.description = formatContent(content);
if (color) embed.color = color;
await pluginData.client.editMessage(savedMessage.channel_id, savedMessage.id, { embed });
await sendSuccessMessage(pluginData, msg.channel, "Embed edited");
if (args.content) {
const prefix = pluginData.guildConfig.prefix || "!";
msg.channel.createMessage(
trimLines(`
<@!${msg.author.id}> You can now specify an embed's content directly at the end of the command:
\`${prefix}edit_embed -title "Some title" content goes here\`
The \`-content\` option will soon be removed in favor of this.
`),
);
}
},
});

View file

@ -0,0 +1,23 @@
import { postCmd } from "../types";
import { commandTypeHelpers as ct } from "../../../commandTypes";
import { actualPostCmd } from "../util/actualPostCmd";
export const PostCmd = postCmd({
trigger: "post",
permission: "can_post",
signature: {
channel: ct.textChannel(),
content: ct.string({ catchAll: true }),
"enable-mentions": ct.bool({ option: true, isSwitch: true }),
schedule: ct.string({ option: true }),
repeat: ct.delay({ option: true }),
"repeat-until": ct.string({ option: true }),
"repeat-times": ct.number({ option: true }),
},
async run({ message: msg, args, pluginData }) {
actualPostCmd(pluginData, msg, args.channel, { content: args.content }, args);
},
});

View file

@ -0,0 +1,76 @@
import { postCmd } from "../types";
import { commandTypeHelpers as ct } from "../../../commandTypes";
import { actualPostCmd } from "../util/actualPostCmd";
import { sendErrorMessage } from "src/pluginUtils";
import { Embed } from "eris";
import { isValidEmbed } from "src/utils";
import { formatContent } from "../util/formatContent";
const COLOR_MATCH_REGEX = /^#?([0-9a-f]{6})$/;
export const PostEmbedCmd = postCmd({
trigger: "post_embed",
permission: "can_post",
signature: {
channel: ct.textChannel(),
maincontent: ct.string({ catchAll: true }),
title: ct.string({ option: true }),
content: ct.string({ option: true }),
color: ct.string({ option: true }),
raw: ct.bool({ option: true, isSwitch: true, shortcut: "r" }),
schedule: ct.string({ option: true }),
repeat: ct.delay({ option: true }),
"repeat-until": ct.string({ option: true }),
"repeat-times": ct.number({ option: true }),
},
async run({ message: msg, args, pluginData }) {
const content = args.content || args.maincontent;
if (!args.title && !content) {
sendErrorMessage(pluginData, msg.channel, "Title or content required");
return;
}
let color = null;
if (args.color) {
const colorMatch = args.color.toLowerCase().match(COLOR_MATCH_REGEX);
if (!colorMatch) {
sendErrorMessage(pluginData, msg.channel, "Invalid color specified, use hex colors");
return;
}
color = parseInt(colorMatch[1], 16);
}
let embed: Embed = { type: "rich" };
if (args.title) embed.title = args.title;
if (color) embed.color = color;
if (content) {
if (args.raw) {
let parsed;
try {
parsed = JSON.parse(content);
} catch (e) {
sendErrorMessage(pluginData, msg.channel, "Syntax error in embed JSON");
return;
}
if (!isValidEmbed(parsed)) {
sendErrorMessage(pluginData, msg.channel, "Embed is not valid");
return;
}
embed = Object.assign({}, embed, parsed);
} else {
embed.description = formatContent(content);
}
}
actualPostCmd(pluginData, msg, args.channel, { embed }, args);
},
});

View file

@ -0,0 +1,25 @@
import { postCmd } from "../types";
import { sorter } from "src/utils";
import { sendErrorMessage, sendSuccessMessage } from "src/pluginUtils";
import { commandTypeHelpers as ct } from "../../../commandTypes";
export const ScheduledPostsDeleteCmd = postCmd({
trigger: ["scheduled_posts delete", "scheduled_posts d"],
permission: "can_post",
signature: {
num: ct.number(),
},
async run({ message: msg, args, pluginData }) {
const scheduledPosts = await pluginData.state.scheduledPosts.all();
scheduledPosts.sort(sorter("post_at"));
const post = scheduledPosts[args.num - 1];
if (!post) {
return sendErrorMessage(pluginData, msg.channel, "Scheduled post not found");
}
await pluginData.state.scheduledPosts.delete(post.id);
sendSuccessMessage(pluginData, msg.channel, "Scheduled post deleted!");
},
});

View file

@ -0,0 +1,57 @@
import { postCmd } from "../types";
import { trimLines, sorter, disableCodeBlocks, deactivateMentions, createChunkedMessage } from "src/utils";
import humanizeDuration from "humanize-duration";
const SCHEDULED_POST_PREVIEW_TEXT_LENGTH = 50;
export const ScheduledPostsListCmd = postCmd({
trigger: ["scheduled_posts", "scheduled_posts list"],
permission: "can_post",
async run({ message: msg, pluginData }) {
const scheduledPosts = await pluginData.state.scheduledPosts.all();
if (scheduledPosts.length === 0) {
msg.channel.createMessage("No scheduled posts");
return;
}
scheduledPosts.sort(sorter("post_at"));
let i = 1;
const postLines = scheduledPosts.map(p => {
let previewText =
p.content.content || (p.content.embed && (p.content.embed.description || p.content.embed.title)) || "";
const isTruncated = previewText.length > SCHEDULED_POST_PREVIEW_TEXT_LENGTH;
previewText = disableCodeBlocks(deactivateMentions(previewText))
.replace(/\s+/g, " ")
.slice(0, SCHEDULED_POST_PREVIEW_TEXT_LENGTH);
const parts = [`\`#${i++}\` \`[${p.post_at}]\` ${previewText}${isTruncated ? "..." : ""}`];
if (p.attachments.length) parts.push("*(with attachment)*");
if (p.content.embed) parts.push("*(embed)*");
if (p.repeat_until) {
parts.push(`*(repeated every ${humanizeDuration(p.repeat_interval)} until ${p.repeat_until})*`);
}
if (p.repeat_times) {
parts.push(
`*(repeated every ${humanizeDuration(p.repeat_interval)}, ${p.repeat_times} more ${
p.repeat_times === 1 ? "time" : "times"
})*`,
);
}
parts.push(`*(${p.author_name})*`);
return parts.join(" ");
});
const finalMessage = trimLines(`
${postLines.join("\n")}
Use \`scheduled_posts <num>\` to view a scheduled post in full
Use \`scheduled_posts delete <num>\` to delete a scheduled post
`);
createChunkedMessage(msg.channel, finalMessage);
},
});

View file

@ -0,0 +1,26 @@
import { postCmd } from "../types";
import { sorter } from "src/utils";
import { sendErrorMessage } from "src/pluginUtils";
import { commandTypeHelpers as ct } from "../../../commandTypes";
import { postMessage } from "../util/postMessage";
import { TextChannel } from "eris";
export const ScheduledPostsShowCmd = postCmd({
trigger: ["scheduled_posts", "scheduled_posts show"],
permission: "can_post",
signature: {
num: ct.number(),
},
async run({ message: msg, args, pluginData }) {
const scheduledPosts = await pluginData.state.scheduledPosts.all();
scheduledPosts.sort(sorter("post_at"));
const post = scheduledPosts[args.num - 1];
if (!post) {
return sendErrorMessage(pluginData, msg.channel, "Scheduled post not found");
}
postMessage(pluginData, msg.channel as TextChannel, post.content, post.attachments, post.enable_mentions);
},
});