From 279a8fe7aeae9d15e726d23fb59e698f465879a3 Mon Sep 17 00:00:00 2001 From: Dragory <2606411+Dragory@users.noreply.github.com> Date: Wed, 27 Nov 2019 22:04:00 +0200 Subject: [PATCH] post: use content as raw embed source in !post_embed with --raw/-r switch --- backend/src/plugins/Post.ts | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/backend/src/plugins/Post.ts b/backend/src/plugins/Post.ts index 6717c084..348e6f33 100644 --- a/backend/src/plugins/Post.ts +++ b/backend/src/plugins/Post.ts @@ -13,6 +13,7 @@ import { deactivateMentions, createChunkedMessage, stripObjectToScalars, + isValidEmbed, } from "../utils"; import { GuildSavedMessages } from "../data/GuildSavedMessages"; import { ZeppelinPlugin } from "./ZeppelinPlugin"; @@ -278,6 +279,7 @@ export class PostPlugin extends ZeppelinPlugin { { name: "content", type: "string" }, { name: "color", type: "string" }, { name: "schedule", type: "string" }, + { name: "raw", isSwitch: true, shortcut: "r" }, ], }) @d.permission("can_post") @@ -290,6 +292,7 @@ export class PostPlugin extends ZeppelinPlugin { content?: string; color?: string; schedule?: string; + raw?: boolean; }, ) { if (!(args.channel instanceof TextChannel)) { @@ -315,11 +318,31 @@ export class PostPlugin extends ZeppelinPlugin { color = parseInt(colorMatch[1], 16); } - const embed: EmbedBase = {}; + let embed: EmbedBase = {}; if (args.title) embed.title = args.title; - if (content) embed.description = this.formatContent(content); if (color) embed.color = color; + if (content) { + if (args.raw) { + let parsed; + try { + parsed = JSON.parse(content); + } catch (e) { + this.sendErrorMessage(msg.channel, "Syntax error in embed JSON"); + return; + } + + if (!isValidEmbed(parsed)) { + this.sendErrorMessage(msg.channel, "Embed is not valid"); + return; + } + + embed = Object.assign({}, embed, parsed); + } else { + embed.description = this.formatContent(content); + } + } + if (args.schedule) { // Schedule the post to be posted later const postAt = this.parseScheduleTime(args.schedule);