3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-05-10 20:35:02 +00:00
zeppelin/backend/src/plugins/Post/util/postMessage.ts
2020-07-23 21:26:22 +02:00

67 lines
1.8 KiB
TypeScript

import { PluginData } from "knub";
import { PostPluginType } from "../types";
import { TextChannel, MessageContent, Attachment, Message, Role } from "eris";
import { downloadFile, getRoleMentions } from "src/utils";
import fs from "fs";
import { formatContent } from "./formatContent";
const fsp = fs.promises;
export async function postMessage(
pluginData: PluginData<PostPluginType>,
channel: TextChannel,
content: MessageContent,
attachments: Attachment[] = [],
enableMentions: boolean = false,
): Promise<Message> {
if (typeof content === "string") {
content = { content };
}
if (content && content.content) {
content.content = formatContent(content.content);
}
let downloadedAttachment;
let file;
if (attachments.length) {
downloadedAttachment = await downloadFile(attachments[0].url);
file = {
name: attachments[0].filename,
file: await fsp.readFile(downloadedAttachment.path),
};
}
const rolesMadeMentionable: Role[] = [];
if (enableMentions && content.content) {
const mentionedRoleIds = getRoleMentions(content.content);
if (mentionedRoleIds != null) {
for (const roleId of mentionedRoleIds) {
const role = pluginData.guild.roles.get(roleId);
if (role && !role.mentionable) {
await role.edit({
mentionable: true,
});
rolesMadeMentionable.push(role);
}
}
}
content.allowedMentions.everyone = false;
}
const createdMsg = await channel.createMessage(content, file);
pluginData.state.savedMessages.setPermanent(createdMsg.id);
for (const role of rolesMadeMentionable) {
role.edit({
mentionable: false,
});
}
if (downloadedAttachment) {
downloadedAttachment.deleteFn();
}
return createdMsg;
}