2019-05-04 18:41:50 +03:00
|
|
|
import { decorators as d, IPluginOptions, logger } from "knub";
|
|
|
|
import { Attachment, Channel, EmbedBase, Message, MessageContent, Role, TextChannel, User } from "eris";
|
|
|
|
import {
|
|
|
|
errorMessage,
|
|
|
|
downloadFile,
|
|
|
|
getRoleMentions,
|
|
|
|
trimLines,
|
|
|
|
DBDateFormat,
|
|
|
|
convertDelayStringToMS,
|
|
|
|
SECONDS,
|
|
|
|
sorter,
|
|
|
|
disableCodeBlocks,
|
|
|
|
deactivateMentions,
|
|
|
|
createChunkedMessage,
|
|
|
|
stripObjectToScalars,
|
|
|
|
} from "../utils";
|
2018-11-24 14:19:47 +02:00
|
|
|
import { GuildSavedMessages } from "../data/GuildSavedMessages";
|
2019-03-04 21:44:04 +02:00
|
|
|
import { ZeppelinPlugin } from "./ZeppelinPlugin";
|
2019-01-15 03:04:47 +02:00
|
|
|
|
|
|
|
import fs from "fs";
|
2019-05-04 18:41:50 +03:00
|
|
|
import { GuildScheduledPosts } from "../data/GuildScheduledPosts";
|
|
|
|
import moment, { Moment } from "moment-timezone";
|
|
|
|
import { GuildLogs } from "../data/GuildLogs";
|
|
|
|
import { LogType } from "../data/LogType";
|
2019-07-21 21:15:52 +03:00
|
|
|
import * as t from "io-ts";
|
|
|
|
|
|
|
|
const ConfigSchema = t.type({
|
|
|
|
can_post: t.boolean,
|
|
|
|
});
|
|
|
|
type TConfigSchema = t.TypeOf<typeof ConfigSchema>;
|
|
|
|
|
2019-01-15 03:04:47 +02:00
|
|
|
const fsp = fs.promises;
|
2018-07-14 20:54:48 +03:00
|
|
|
|
2019-01-15 03:58:58 +02:00
|
|
|
const COLOR_MATCH_REGEX = /^#?([0-9a-f]{6})$/;
|
|
|
|
|
2019-05-04 18:41:50 +03:00
|
|
|
const SCHEDULED_POST_CHECK_INTERVAL = 15 * SECONDS;
|
|
|
|
const SCHEDULED_POST_PREVIEW_TEXT_LENGTH = 50;
|
|
|
|
|
2019-07-21 21:15:52 +03:00
|
|
|
export class PostPlugin extends ZeppelinPlugin<TConfigSchema> {
|
2019-01-15 03:04:47 +02:00
|
|
|
public static pluginName = "post";
|
2019-07-21 21:15:52 +03:00
|
|
|
protected static configSchema = ConfigSchema;
|
2019-01-03 06:15:28 +02:00
|
|
|
|
2018-11-24 14:19:47 +02:00
|
|
|
protected savedMessages: GuildSavedMessages;
|
2019-05-04 18:41:50 +03:00
|
|
|
protected scheduledPosts: GuildScheduledPosts;
|
|
|
|
protected logs: GuildLogs;
|
2018-11-24 14:19:47 +02:00
|
|
|
|
2019-05-08 08:27:52 +03:00
|
|
|
private scheduledPostLoopTimeout;
|
|
|
|
|
2018-11-24 14:19:47 +02:00
|
|
|
onLoad() {
|
2019-05-25 21:25:34 +03:00
|
|
|
this.savedMessages = GuildSavedMessages.getGuildInstance(this.guildId);
|
|
|
|
this.scheduledPosts = GuildScheduledPosts.getGuildInstance(this.guildId);
|
2019-05-04 18:41:50 +03:00
|
|
|
this.logs = new GuildLogs(this.guildId);
|
|
|
|
|
|
|
|
this.scheduledPostLoop();
|
2018-11-24 14:19:47 +02:00
|
|
|
}
|
|
|
|
|
2019-05-08 08:27:52 +03:00
|
|
|
onUnload() {
|
|
|
|
clearTimeout(this.scheduledPostLoopTimeout);
|
|
|
|
}
|
|
|
|
|
2019-07-22 00:09:45 +03:00
|
|
|
protected static getStaticDefaultOptions(): IPluginOptions<TConfigSchema> {
|
2018-07-14 20:54:48 +03:00
|
|
|
return {
|
2019-04-13 01:44:18 +03:00
|
|
|
config: {
|
|
|
|
can_post: false,
|
2018-07-14 20:54:48 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
overrides: [
|
|
|
|
{
|
|
|
|
level: ">=100",
|
2019-04-13 01:44:18 +03:00
|
|
|
config: {
|
|
|
|
can_post: true,
|
2019-02-09 14:47:50 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
2018-07-14 20:54:48 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-02-09 14:47:50 +02:00
|
|
|
protected formatContent(str) {
|
|
|
|
return str.replace(/\\n/g, "\n");
|
|
|
|
}
|
|
|
|
|
2019-05-04 18:41:50 +03:00
|
|
|
protected async postMessage(
|
|
|
|
channel: TextChannel,
|
|
|
|
content: MessageContent,
|
|
|
|
attachments: Attachment[] = [],
|
|
|
|
enableMentions: boolean = false,
|
|
|
|
): Promise<Message> {
|
|
|
|
if (typeof content === "string") {
|
|
|
|
content = { content };
|
|
|
|
}
|
|
|
|
|
|
|
|
if (content && content.content) {
|
|
|
|
content.content = this.formatContent(content.content);
|
2018-07-14 20:54:48 +03:00
|
|
|
}
|
|
|
|
|
2019-01-15 03:04:47 +02:00
|
|
|
let downloadedAttachment;
|
|
|
|
let file;
|
2019-05-04 18:41:50 +03:00
|
|
|
if (attachments.length) {
|
|
|
|
downloadedAttachment = await downloadFile(attachments[0].url);
|
2019-01-15 03:04:47 +02:00
|
|
|
file = {
|
2019-05-04 18:41:50 +03:00
|
|
|
name: 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
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-02-17 16:02:16 +02:00
|
|
|
const rolesMadeMentionable: Role[] = [];
|
2019-05-04 18:41:50 +03:00
|
|
|
if (enableMentions && content.content) {
|
|
|
|
const mentionedRoleIds = getRoleMentions(content.content);
|
2019-02-17 16:02:16 +02:00
|
|
|
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-05-13 00:31:59 +03:00
|
|
|
|
|
|
|
content.disableEveryone = false;
|
2019-02-17 16:02:16 +02:00
|
|
|
}
|
|
|
|
|
2019-05-04 18:41:50 +03:00
|
|
|
const createdMsg = await channel.createMessage(content, file);
|
|
|
|
this.savedMessages.setPermanent(createdMsg.id);
|
2019-01-15 03:04:47 +02:00
|
|
|
|
2019-02-17 16:02:16 +02:00
|
|
|
for (const role of rolesMadeMentionable) {
|
|
|
|
role.edit({
|
|
|
|
mentionable: false,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-01-15 03:04:47 +02:00
|
|
|
if (downloadedAttachment) {
|
|
|
|
downloadedAttachment.deleteFn();
|
|
|
|
}
|
2019-05-04 18:41:50 +03:00
|
|
|
|
|
|
|
return createdMsg;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected parseScheduleTime(str): Moment {
|
|
|
|
const dtMatch = str.match(/^\d{4}-\d{2}-\d{2} \d{1,2}:\d{1,2}(:\d{1,2})?$/);
|
|
|
|
if (dtMatch) {
|
|
|
|
const dt = moment(str, dtMatch[1] ? "YYYY-MM-DD H:m:s" : "YYYY-MM-DD H:m");
|
|
|
|
return dt;
|
|
|
|
}
|
|
|
|
|
|
|
|
const tMatch = str.match(/^\d{1,2}:\d{1,2}(:\d{1,2})?$/);
|
|
|
|
if (tMatch) {
|
|
|
|
const dt = moment(str, tMatch[1] ? "H:m:s" : "H:m");
|
|
|
|
if (dt.isBefore(moment())) dt.add(1, "day");
|
|
|
|
return dt;
|
|
|
|
}
|
|
|
|
|
|
|
|
const delayStringMS = convertDelayStringToMS(str, "m");
|
|
|
|
if (delayStringMS) {
|
|
|
|
return moment().add(delayStringMS, "ms");
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected async scheduledPostLoop() {
|
|
|
|
const duePosts = await this.scheduledPosts.getDueScheduledPosts();
|
|
|
|
for (const post of duePosts) {
|
|
|
|
const channel = this.guild.channels.get(post.channel_id);
|
|
|
|
if (channel instanceof TextChannel) {
|
2019-05-04 19:18:16 +03:00
|
|
|
const [username, discriminator] = post.author_name.split("#");
|
|
|
|
const author: Partial<User> = this.bot.users.get(post.author_id) || {
|
|
|
|
id: post.author_id,
|
|
|
|
username,
|
|
|
|
discriminator,
|
|
|
|
};
|
|
|
|
|
2019-05-04 18:41:50 +03:00
|
|
|
try {
|
|
|
|
const postedMessage = await this.postMessage(channel, post.content, post.attachments, post.enable_mentions);
|
|
|
|
this.logs.log(LogType.POSTED_SCHEDULED_MESSAGE, {
|
2019-05-04 19:18:16 +03:00
|
|
|
author: stripObjectToScalars(author),
|
2019-05-04 18:41:50 +03:00
|
|
|
channel: stripObjectToScalars(channel),
|
|
|
|
messageId: postedMessage.id,
|
|
|
|
});
|
|
|
|
} catch (e) {
|
2019-05-04 19:18:16 +03:00
|
|
|
this.logs.log(LogType.BOT_ALERT, {
|
|
|
|
body: `Failed to post scheduled message by {userMention(author)} to {channelMention(channel)}`,
|
|
|
|
channel: stripObjectToScalars(channel),
|
|
|
|
author: stripObjectToScalars(author),
|
|
|
|
});
|
2019-05-04 18:41:50 +03:00
|
|
|
logger.warn(
|
|
|
|
`Failed to post scheduled message to #${channel.name} (${channel.id}) on ${this.guild.name} (${
|
|
|
|
this.guildId
|
|
|
|
})`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
await this.scheduledPosts.delete(post.id);
|
|
|
|
}
|
|
|
|
|
2019-05-08 08:27:52 +03:00
|
|
|
this.scheduledPostLoopTimeout = setTimeout(() => this.scheduledPostLoop(), SCHEDULED_POST_CHECK_INTERVAL);
|
2019-05-04 18:41:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* COMMAND: Post a regular text message as the bot to the specified channel
|
|
|
|
*/
|
|
|
|
@d.command("post", "<channel:channel> [content:string$]", {
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: "enable-mentions",
|
|
|
|
type: "bool",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "schedule",
|
|
|
|
type: "string",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
})
|
|
|
|
@d.permission("can_post")
|
|
|
|
async postCmd(
|
|
|
|
msg: Message,
|
|
|
|
args: { channel: Channel; content?: string; "enable-mentions": boolean; schedule?: string },
|
|
|
|
) {
|
|
|
|
if (!(args.channel instanceof TextChannel)) {
|
|
|
|
msg.channel.createMessage(errorMessage("Channel is not a text channel"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (args.content == null && msg.attachments.length === 0) {
|
|
|
|
msg.channel.createMessage(errorMessage("Text content or attachment required"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (args.schedule) {
|
|
|
|
// Schedule the post to be posted later
|
|
|
|
const postAt = this.parseScheduleTime(args.schedule);
|
|
|
|
if (!postAt) {
|
|
|
|
return this.sendErrorMessage(msg.channel, "Invalid schedule time");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (postAt < moment()) {
|
|
|
|
return this.sendErrorMessage(msg.channel, "Post can't be scheduled to be posted in the past");
|
|
|
|
}
|
|
|
|
|
|
|
|
await this.scheduledPosts.create({
|
|
|
|
author_id: msg.author.id,
|
|
|
|
author_name: `${msg.author.username}#${msg.author.discriminator}`,
|
|
|
|
channel_id: args.channel.id,
|
|
|
|
content: { content: args.content },
|
|
|
|
attachments: msg.attachments,
|
|
|
|
post_at: postAt.format(DBDateFormat),
|
|
|
|
enable_mentions: args["enable-mentions"],
|
|
|
|
});
|
|
|
|
this.sendSuccessMessage(
|
|
|
|
msg.channel,
|
|
|
|
`Message scheduled to be posted in <#${args.channel.id}> on ${postAt.format("YYYY-MM-DD [at] HH:mm:ss")} (UTC)`,
|
|
|
|
);
|
|
|
|
this.logs.log(LogType.SCHEDULED_MESSAGE, {
|
|
|
|
author: stripObjectToScalars(msg.author),
|
|
|
|
channel: stripObjectToScalars(args.channel),
|
|
|
|
date: postAt.format("YYYY-MM-DD"),
|
|
|
|
time: postAt.format("HH:mm:ss"),
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// Post the message immediately
|
|
|
|
await this.postMessage(args.channel, args.content, msg.attachments, args["enable-mentions"]);
|
2019-08-04 13:18:02 +03:00
|
|
|
if (args.channel.id !== msg.channel.id) {
|
|
|
|
this.sendSuccessMessage(msg.channel, `Message posted in <#${args.channel.id}>`);
|
|
|
|
}
|
2019-05-04 18:41:50 +03:00
|
|
|
}
|
2018-07-14 20:54:48 +03:00
|
|
|
}
|
|
|
|
|
2019-01-15 03:58:58 +02:00
|
|
|
/**
|
|
|
|
* COMMAND: Post a message with an embed as the bot to the specified channel
|
|
|
|
*/
|
2019-05-04 18:41:50 +03:00
|
|
|
@d.command("post_embed", "<channel:channel> [maincontent:string$]", {
|
2019-02-09 14:47:50 +02:00
|
|
|
options: [
|
|
|
|
{ name: "title", type: "string" },
|
|
|
|
{ name: "content", type: "string" },
|
|
|
|
{ name: "color", type: "string" },
|
2019-05-04 18:41:50 +03:00
|
|
|
{ name: "schedule", type: "string" },
|
2019-02-09 14:47:50 +02:00
|
|
|
],
|
2019-01-15 03:39:39 +02:00
|
|
|
})
|
2019-04-13 01:44:18 +03:00
|
|
|
@d.permission("can_post")
|
2019-05-04 18:41:50 +03:00
|
|
|
async postEmbedCmd(
|
|
|
|
msg: Message,
|
|
|
|
args: {
|
|
|
|
channel: Channel;
|
|
|
|
title?: string;
|
|
|
|
maincontent?: string;
|
|
|
|
content?: string;
|
|
|
|
color?: string;
|
|
|
|
schedule?: string;
|
|
|
|
},
|
|
|
|
) {
|
2019-01-15 03:39:39 +02:00
|
|
|
if (!(args.channel instanceof TextChannel)) {
|
|
|
|
msg.channel.createMessage(errorMessage("Channel is not a text channel"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-05-04 18:41:50 +03:00
|
|
|
const content = args.content || args.maincontent;
|
|
|
|
|
|
|
|
if (!args.title && !content) {
|
2019-01-15 03:39:39 +02:00
|
|
|
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-05-04 18:41:50 +03:00
|
|
|
if (content) embed.description = this.formatContent(content);
|
2019-01-15 03:39:39 +02:00
|
|
|
if (color) embed.color = color;
|
|
|
|
|
2019-05-04 18:41:50 +03:00
|
|
|
if (args.schedule) {
|
|
|
|
// Schedule the post to be posted later
|
|
|
|
const postAt = this.parseScheduleTime(args.schedule);
|
|
|
|
if (!postAt) {
|
|
|
|
return this.sendErrorMessage(msg.channel, "Invalid schedule time");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (postAt < moment()) {
|
|
|
|
return this.sendErrorMessage(msg.channel, "Post can't be scheduled to be posted in the past");
|
|
|
|
}
|
|
|
|
|
|
|
|
await this.scheduledPosts.create({
|
|
|
|
author_id: msg.author.id,
|
|
|
|
author_name: `${msg.author.username}#${msg.author.discriminator}`,
|
|
|
|
channel_id: args.channel.id,
|
|
|
|
content: { embed },
|
|
|
|
attachments: msg.attachments,
|
|
|
|
post_at: postAt.format(DBDateFormat),
|
|
|
|
});
|
|
|
|
await this.sendSuccessMessage(
|
|
|
|
msg.channel,
|
|
|
|
`Embed scheduled to be posted in <#${args.channel.id}> on ${postAt.format("YYYY-MM-DD [at] HH:mm:ss")} (UTC)`,
|
|
|
|
);
|
|
|
|
this.logs.log(LogType.SCHEDULED_MESSAGE, {
|
|
|
|
author: stripObjectToScalars(msg.author),
|
|
|
|
channel: stripObjectToScalars(args.channel),
|
|
|
|
date: postAt.format("YYYY-MM-DD"),
|
|
|
|
time: postAt.format("HH:mm:ss"),
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
const createdMsg = await args.channel.createMessage({ embed });
|
|
|
|
this.savedMessages.setPermanent(createdMsg.id);
|
|
|
|
|
2019-08-04 13:18:02 +03:00
|
|
|
if (msg.channel.id !== args.channel.id) {
|
|
|
|
await this.sendSuccessMessage(msg.channel, `Embed posted in <#${args.channel.id}>`);
|
|
|
|
}
|
2019-05-04 18:41:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (args.content) {
|
|
|
|
const prefix = this.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}post_embed --title="Some title" content goes here\`
|
|
|
|
The \`--content\` option will soon be removed in favor of this.
|
|
|
|
`),
|
|
|
|
);
|
|
|
|
}
|
2019-01-15 03:39:39 +02:00
|
|
|
}
|
|
|
|
|
2018-07-14 20:54:48 +03:00
|
|
|
/**
|
2019-01-15 03:58:58 +02:00
|
|
|
* COMMAND: Edit the specified message posted by the bot
|
2018-07-14 20:54:48 +03:00
|
|
|
*/
|
2018-11-24 14:34:05 +02:00
|
|
|
@d.command("edit", "<messageId:string> <content:string$>")
|
2019-04-13 01:44:18 +03:00
|
|
|
@d.permission("can_post")
|
2018-11-24 14:34:05 +02:00
|
|
|
async editCmd(msg, args: { messageId: string; content: string }) {
|
|
|
|
const savedMessage = await this.savedMessages.find(args.messageId);
|
|
|
|
if (!savedMessage) {
|
|
|
|
msg.channel.createMessage(errorMessage("Unknown message"));
|
2018-07-14 20:54:48 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-11-24 14:34:05 +02:00
|
|
|
if (savedMessage.user_id !== this.bot.user.id) {
|
|
|
|
msg.channel.createMessage(errorMessage("Message wasn't posted by me"));
|
2018-07-14 20:54:48 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-09 14:47:50 +02:00
|
|
|
await this.bot.editMessage(savedMessage.channel_id, savedMessage.id, this.formatContent(args.content));
|
2019-05-04 18:41:50 +03:00
|
|
|
this.sendSuccessMessage(msg.channel, "Message edited");
|
2018-07-14 20:54:48 +03:00
|
|
|
}
|
2019-01-15 03:58:58 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* COMMAND: Edit the specified message with an embed posted by the bot
|
|
|
|
*/
|
2019-05-04 18:41:50 +03:00
|
|
|
@d.command("edit_embed", "<messageId:string> [maincontent: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
|
|
|
})
|
2019-04-13 01:44:18 +03:00
|
|
|
@d.permission("can_post")
|
2019-05-04 18:41:50 +03:00
|
|
|
async editEmbedCmd(
|
|
|
|
msg: Message,
|
|
|
|
args: { messageId: string; title?: string; maincontent?: string; content?: string; color?: string },
|
|
|
|
) {
|
2019-01-15 03:58:58 +02:00
|
|
|
const savedMessage = await this.savedMessages.find(args.messageId);
|
|
|
|
if (!savedMessage) {
|
|
|
|
msg.channel.createMessage(errorMessage("Unknown message"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-05-04 18:41:50 +03:00
|
|
|
const content = args.content || args.maincontent;
|
2019-01-15 03:58:58 +02:00
|
|
|
|
|
|
|
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-05-04 18:41:50 +03:00
|
|
|
if (content) embed.description = this.formatContent(content);
|
2019-01-15 03:58:58 +02:00
|
|
|
if (color) embed.color = color;
|
|
|
|
|
|
|
|
await this.bot.editMessage(savedMessage.channel_id, savedMessage.id, { embed });
|
2019-05-04 18:41:50 +03:00
|
|
|
await this.sendSuccessMessage(msg.channel, "Embed edited");
|
|
|
|
|
|
|
|
if (args.content) {
|
|
|
|
const prefix = this.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.
|
|
|
|
`),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@d.command("scheduled_posts", [], {
|
|
|
|
aliases: ["scheduled_posts list"],
|
|
|
|
})
|
|
|
|
@d.permission("can_post")
|
|
|
|
async scheduledPostListCmd(msg: Message) {
|
|
|
|
const scheduledPosts = await this.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)*");
|
|
|
|
parts.push(`*(${p.author_name})*`);
|
|
|
|
|
|
|
|
return parts.join(" ");
|
|
|
|
});
|
|
|
|
|
|
|
|
const finalMessage = trimLines(`
|
|
|
|
${postLines.join("\n")}
|
|
|
|
|
|
|
|
Use \`scheduled_posts show <num>\` to view a scheduled post in full
|
|
|
|
Use \`scheduled_posts delete <num>\` to delete a scheduled post
|
|
|
|
`);
|
|
|
|
createChunkedMessage(msg.channel, finalMessage);
|
|
|
|
}
|
|
|
|
|
|
|
|
@d.command("scheduled_posts delete", "<num:number>", {
|
|
|
|
aliases: ["scheduled_posts d"],
|
|
|
|
})
|
|
|
|
@d.permission("can_post")
|
|
|
|
async scheduledPostDeleteCmd(msg: Message, args: { num: number }) {
|
|
|
|
const scheduledPosts = await this.scheduledPosts.all();
|
|
|
|
scheduledPosts.sort(sorter("post_at"));
|
|
|
|
const post = scheduledPosts[args.num - 1];
|
|
|
|
if (!post) {
|
|
|
|
return this.sendErrorMessage(msg.channel, "Scheduled post not found");
|
|
|
|
}
|
|
|
|
|
|
|
|
await this.scheduledPosts.delete(post.id);
|
|
|
|
this.sendSuccessMessage(msg.channel, "Scheduled post deleted!");
|
|
|
|
}
|
|
|
|
|
|
|
|
@d.command("scheduled_posts", "<num:number>", {
|
|
|
|
aliases: ["scheduled_posts show"],
|
|
|
|
})
|
|
|
|
@d.permission("can_post")
|
|
|
|
async scheduledPostShowCmd(msg: Message, args: { num: number }) {
|
|
|
|
const scheduledPosts = await this.scheduledPosts.all();
|
|
|
|
scheduledPosts.sort(sorter("post_at"));
|
|
|
|
const post = scheduledPosts[args.num - 1];
|
|
|
|
if (!post) {
|
|
|
|
return this.sendErrorMessage(msg.channel, "Scheduled post not found");
|
|
|
|
}
|
|
|
|
|
|
|
|
this.postMessage(msg.channel as TextChannel, post.content, post.attachments, post.enable_mentions);
|
2019-01-15 03:58:58 +02:00
|
|
|
}
|
2018-07-14 20:54:48 +03:00
|
|
|
}
|