post: use content as raw embed source in !post_embed with --raw/-r switch

This commit is contained in:
Dragory 2019-11-27 22:04:00 +02:00
parent cbcd2bd67d
commit 279a8fe7ae

View file

@ -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<TConfigSchema> {
{ 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<TConfigSchema> {
content?: string;
color?: string;
schedule?: string;
raw?: boolean;
},
) {
if (!(args.channel instanceof TextChannel)) {
@ -315,11 +318,31 @@ export class PostPlugin extends ZeppelinPlugin<TConfigSchema> {
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);