mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-05-11 04:45:02 +00:00
Finish preliminary rework, ready to test
This commit is contained in:
parent
57893e7f76
commit
d0a1beb809
177 changed files with 854 additions and 707 deletions
|
@ -2,6 +2,7 @@ import { postCmd } from "../types";
|
|||
import { commandTypeHelpers as ct } from "../../../commandTypes";
|
||||
import { sendErrorMessage, sendSuccessMessage } from "../../../pluginUtils";
|
||||
import { formatContent } from "../util/formatContent";
|
||||
import { TextChannel } from "discord.js";
|
||||
|
||||
export const EditCmd = postCmd({
|
||||
trigger: "edit",
|
||||
|
@ -24,7 +25,9 @@ export const EditCmd = postCmd({
|
|||
return;
|
||||
}
|
||||
|
||||
await pluginData.client.editMessage(savedMessage.channel_id, savedMessage.id, formatContent(args.content));
|
||||
(pluginData.guild.channels.cache.get(savedMessage.channel_id) as TextChannel).messages.edit(savedMessage.id, {
|
||||
content: formatContent(args.content),
|
||||
});
|
||||
sendSuccessMessage(pluginData, msg.channel, "Message edited");
|
||||
},
|
||||
});
|
||||
|
|
|
@ -6,6 +6,7 @@ import { trimLines } from "../../../utils";
|
|||
import { formatContent } from "../util/formatContent";
|
||||
import { parseColor } from "../../../utils/parseColor";
|
||||
import { rgbToInt } from "../../../utils/rgbToInt";
|
||||
import { MessageEmbed, TextChannel } from "discord.js";
|
||||
|
||||
const COLOR_MATCH_REGEX = /^#?([0-9a-f]{6})$/;
|
||||
|
||||
|
@ -42,17 +43,19 @@ export const EditEmbedCmd = postCmd({
|
|||
}
|
||||
}
|
||||
|
||||
const embed: Embed = savedMessage.data.embeds![0] as Embed;
|
||||
const embed: MessageEmbed = savedMessage.data.embeds![0] as MessageEmbed;
|
||||
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 });
|
||||
(pluginData.guild.channels.cache.get(savedMessage.channel_id) as TextChannel).messages.edit(savedMessage.id, {
|
||||
embed,
|
||||
});
|
||||
await sendSuccessMessage(pluginData, msg.channel, "Embed edited");
|
||||
|
||||
if (args.content) {
|
||||
const prefix = pluginData.fullConfig.prefix || "!";
|
||||
msg.channel.createMessage(
|
||||
msg.channel.send(
|
||||
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\`
|
||||
|
|
|
@ -7,6 +7,7 @@ import { isValidEmbed, trimLines } from "../../../utils";
|
|||
import { formatContent } from "../util/formatContent";
|
||||
import { parseColor } from "../../../utils/parseColor";
|
||||
import { rgbToInt } from "../../../utils/rgbToInt";
|
||||
import { MessageEmbed, MessageEmbedOptions } from "discord.js";
|
||||
|
||||
export const PostEmbedCmd = postCmd({
|
||||
trigger: "post_embed",
|
||||
|
@ -46,7 +47,7 @@ export const PostEmbedCmd = postCmd({
|
|||
}
|
||||
}
|
||||
|
||||
let embed: Embed = { type: "rich" };
|
||||
let embed: MessageEmbedOptions = {};
|
||||
if (args.title) embed.title = args.title;
|
||||
if (color) embed.color = color;
|
||||
|
||||
|
@ -73,7 +74,7 @@ export const PostEmbedCmd = postCmd({
|
|||
|
||||
if (args.content) {
|
||||
const prefix = pluginData.fullConfig.prefix || "!";
|
||||
msg.channel.createMessage(
|
||||
msg.channel.send(
|
||||
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\`
|
||||
|
|
|
@ -20,7 +20,7 @@ export const ScheduledPostsListCmd = postCmd({
|
|||
async run({ message: msg, pluginData }) {
|
||||
const scheduledPosts = await pluginData.state.scheduledPosts.all();
|
||||
if (scheduledPosts.length === 0) {
|
||||
msg.channel.createMessage("No scheduled posts");
|
||||
msg.channel.send("No scheduled posts");
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
|
@ -3,6 +3,7 @@ import { sorter } from "../../../utils";
|
|||
import { sendErrorMessage } from "../../../pluginUtils";
|
||||
import { commandTypeHelpers as ct } from "../../../commandTypes";
|
||||
import { postMessage } from "../util/postMessage";
|
||||
import { TextChannel } from "discord.js";
|
||||
|
||||
export const ScheduledPostsShowCmd = postCmd({
|
||||
trigger: ["scheduled_posts", "scheduled_posts show"],
|
||||
|
|
|
@ -8,6 +8,7 @@ import { PostPluginType } from "../types";
|
|||
import { parseScheduleTime } from "./parseScheduleTime";
|
||||
import { postMessage } from "./postMessage";
|
||||
import { TimeAndDatePlugin } from "../../TimeAndDate/TimeAndDatePlugin";
|
||||
import { Message, Channel, TextChannel } from "discord.js";
|
||||
|
||||
const MIN_REPEAT_TIME = 5 * MINUTES;
|
||||
const MAX_REPEAT_TIME = Math.pow(2, 32);
|
||||
|
@ -27,22 +28,30 @@ export async function actualPostCmd(
|
|||
} = {},
|
||||
) {
|
||||
if (!(targetChannel instanceof TextChannel)) {
|
||||
msg.channel.createMessage(errorMessage("Channel is not a text channel"));
|
||||
msg.channel.send(errorMessage("Channel is not a text channel"));
|
||||
return;
|
||||
}
|
||||
|
||||
if (content == null && msg.attachments.length === 0) {
|
||||
msg.channel.createMessage(errorMessage("Message content or attachment required"));
|
||||
if (content == null && msg.attachments.size === 0) {
|
||||
msg.channel.send(errorMessage("Message content or attachment required"));
|
||||
return;
|
||||
}
|
||||
|
||||
if (opts.repeat) {
|
||||
if (opts.repeat < MIN_REPEAT_TIME) {
|
||||
sendErrorMessage(pluginData, msg.channel, `Minimum time for -repeat is ${humanizeDuration(MIN_REPEAT_TIME)}`);
|
||||
sendErrorMessage(
|
||||
pluginData,
|
||||
msg.channel as TextChannel,
|
||||
`Minimum time for -repeat is ${humanizeDuration(MIN_REPEAT_TIME)}`,
|
||||
);
|
||||
return;
|
||||
}
|
||||
if (opts.repeat > MAX_REPEAT_TIME) {
|
||||
sendErrorMessage(pluginData, msg.channel, `Max time for -repeat is ${humanizeDuration(MAX_REPEAT_TIME)}`);
|
||||
sendErrorMessage(
|
||||
pluginData,
|
||||
msg.channel as TextChannel,
|
||||
`Max time for -repeat is ${humanizeDuration(MAX_REPEAT_TIME)}`,
|
||||
);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
@ -53,7 +62,7 @@ export async function actualPostCmd(
|
|||
// Schedule the post to be posted later
|
||||
postAt = await parseScheduleTime(pluginData, msg.author.id, opts.schedule);
|
||||
if (!postAt) {
|
||||
sendErrorMessage(pluginData, msg.channel, "Invalid schedule time");
|
||||
sendErrorMessage(pluginData, msg.channel as TextChannel, "Invalid schedule time");
|
||||
return;
|
||||
}
|
||||
} else if (opts.repeat) {
|
||||
|
@ -70,17 +79,17 @@ export async function actualPostCmd(
|
|||
|
||||
// Invalid time
|
||||
if (!repeatUntil) {
|
||||
sendErrorMessage(pluginData, msg.channel, "Invalid time specified for -repeat-until");
|
||||
sendErrorMessage(pluginData, msg.channel as TextChannel, "Invalid time specified for -repeat-until");
|
||||
return;
|
||||
}
|
||||
if (repeatUntil.isBefore(moment.utc())) {
|
||||
sendErrorMessage(pluginData, msg.channel, "You can't set -repeat-until in the past");
|
||||
sendErrorMessage(pluginData, msg.channel as TextChannel, "You can't set -repeat-until in the past");
|
||||
return;
|
||||
}
|
||||
if (repeatUntil.isAfter(MAX_REPEAT_UNTIL)) {
|
||||
sendErrorMessage(
|
||||
pluginData,
|
||||
msg.channel,
|
||||
msg.channel as TextChannel,
|
||||
"Unfortunately, -repeat-until can only be at most 100 years into the future. Maybe 99 years would be enough?",
|
||||
);
|
||||
return;
|
||||
|
@ -88,18 +97,26 @@ export async function actualPostCmd(
|
|||
} else if (opts["repeat-times"]) {
|
||||
repeatTimes = opts["repeat-times"];
|
||||
if (repeatTimes <= 0) {
|
||||
sendErrorMessage(pluginData, msg.channel, "-repeat-times must be 1 or more");
|
||||
sendErrorMessage(pluginData, msg.channel as TextChannel, "-repeat-times must be 1 or more");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (repeatUntil && repeatTimes) {
|
||||
sendErrorMessage(pluginData, msg.channel, "You can only use one of -repeat-until or -repeat-times at once");
|
||||
sendErrorMessage(
|
||||
pluginData,
|
||||
msg.channel as TextChannel,
|
||||
"You can only use one of -repeat-until or -repeat-times at once",
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
if (opts.repeat && !repeatUntil && !repeatTimes) {
|
||||
sendErrorMessage(pluginData, msg.channel, "You must specify -repeat-until or -repeat-times for repeated messages");
|
||||
sendErrorMessage(
|
||||
pluginData,
|
||||
msg.channel as TextChannel,
|
||||
"You must specify -repeat-until or -repeat-times for repeated messages",
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -114,7 +131,7 @@ export async function actualPostCmd(
|
|||
// Save schedule/repeat information in DB
|
||||
if (postAt) {
|
||||
if (postAt < moment.utc()) {
|
||||
sendErrorMessage(pluginData, msg.channel, "Post can't be scheduled to be posted in the past");
|
||||
sendErrorMessage(pluginData, msg.channel as TextChannel, "Post can't be scheduled to be posted in the past");
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -123,7 +140,7 @@ export async function actualPostCmd(
|
|||
author_name: `${msg.author.username}#${msg.author.discriminator}`,
|
||||
channel_id: targetChannel.id,
|
||||
content,
|
||||
attachments: msg.attachments,
|
||||
attachments: msg.attachments.array(),
|
||||
post_at: postAt
|
||||
.clone()
|
||||
.tz("Etc/UTC")
|
||||
|
@ -162,7 +179,7 @@ export async function actualPostCmd(
|
|||
|
||||
// When the message isn't scheduled for later, post it immediately
|
||||
if (!opts.schedule) {
|
||||
await postMessage(pluginData, targetChannel, content, msg.attachments, opts["enable-mentions"]);
|
||||
await postMessage(pluginData, targetChannel, content, msg.attachments.array(), opts["enable-mentions"]);
|
||||
}
|
||||
|
||||
if (opts.repeat) {
|
||||
|
@ -197,6 +214,6 @@ export async function actualPostCmd(
|
|||
}
|
||||
|
||||
if (targetChannel.id !== msg.channel.id || opts.schedule || opts.repeat) {
|
||||
sendSuccessMessage(pluginData, msg.channel, successMessage);
|
||||
sendSuccessMessage(pluginData, msg.channel as TextChannel, successMessage);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,14 +4,15 @@ import { PostPluginType } from "../types";
|
|||
import { downloadFile } from "../../../utils";
|
||||
import fs from "fs";
|
||||
import { formatContent } from "./formatContent";
|
||||
import { TextChannel, Message, MessageOptions, MessageAttachment } from "discord.js";
|
||||
|
||||
const fsp = fs.promises;
|
||||
|
||||
export async function postMessage(
|
||||
pluginData: GuildPluginData<PostPluginType>,
|
||||
channel: TextChannel,
|
||||
content: MessageContent,
|
||||
attachments: Attachment[] = [],
|
||||
content: MessageOptions,
|
||||
attachments: MessageAttachment[] = [],
|
||||
enableMentions: boolean = false,
|
||||
): Promise<Message> {
|
||||
if (typeof content === "string") {
|
||||
|
@ -27,20 +28,18 @@ export async function postMessage(
|
|||
if (attachments.length) {
|
||||
downloadedAttachment = await downloadFile(attachments[0].url);
|
||||
file = {
|
||||
name: attachments[0].filename,
|
||||
name: attachments[0].name,
|
||||
file: await fsp.readFile(downloadedAttachment.path),
|
||||
};
|
||||
}
|
||||
|
||||
if (enableMentions) {
|
||||
content.allowedMentions = {
|
||||
everyone: true,
|
||||
users: true,
|
||||
roles: true,
|
||||
parse: ["everyone", "roles", "users"],
|
||||
};
|
||||
}
|
||||
|
||||
const createdMsg = await channel.createMessage(content, file);
|
||||
const createdMsg = await channel.send(content, file);
|
||||
pluginData.state.savedMessages.setPermanent(createdMsg.id);
|
||||
|
||||
if (downloadedAttachment) {
|
||||
|
|
|
@ -6,6 +6,7 @@ import { LogType } from "../../../data/LogType";
|
|||
import moment from "moment-timezone";
|
||||
|
||||
import { postMessage } from "./postMessage";
|
||||
import { TextChannel, User } from "discord.js";
|
||||
|
||||
const SCHEDULED_POST_CHECK_INTERVAL = 5 * SECONDS;
|
||||
|
||||
|
@ -15,7 +16,7 @@ export async function scheduledPostLoop(pluginData: GuildPluginData<PostPluginTy
|
|||
const channel = pluginData.guild.channels.cache.get(post.channel_id);
|
||||
if (channel instanceof TextChannel) {
|
||||
const [username, discriminator] = post.author_name.split("#");
|
||||
const author: Partial<User> = pluginData.client.user!.get(post.author_id) || {
|
||||
const author: User = (await pluginData.client.users.fetch(post.author_id)) || {
|
||||
id: post.author_id,
|
||||
username,
|
||||
discriminator,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue