mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-05-10 20:35:02 +00:00
Migrate Post to new Plugin structure
This commit is contained in:
parent
ebcb28261b
commit
5c070643a3
15 changed files with 752 additions and 0 deletions
67
backend/src/plugins/Post/util/postMessage.ts
Normal file
67
backend/src/plugins/Post/util/postMessage.ts
Normal file
|
@ -0,0 +1,67 @@
|
|||
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;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue