3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-05-10 20:35:02 +00:00

Post: add support for !posting files

This commit is contained in:
Dragory 2019-01-15 03:04:47 +02:00
parent ee3a64cafa
commit d3c3b65db6
4 changed files with 93 additions and 8 deletions

View file

@ -1,11 +1,13 @@
import { Plugin, decorators as d } from "knub";
import { Channel, Message, TextChannel } from "eris";
import { errorMessage } from "../utils";
import { errorMessage, downloadFile } from "../utils";
import { GuildSavedMessages } from "../data/GuildSavedMessages";
import { ISavedMessageData } from "../data/entities/SavedMessage";
import fs from "fs";
const fsp = fs.promises;
export class PostPlugin extends Plugin {
public static pluginName = 'post';
public static pluginName = "post";
protected savedMessages: GuildSavedMessages;
@ -33,18 +35,39 @@ export class PostPlugin extends Plugin {
}
/**
* Post a message as the bot to the specified channel
* COMMAND: Post a message as the bot to the specified channel
*/
@d.command("post", "<channel:channel> <content:string$>")
@d.command("post", "<channel:channel> [content:string$]")
@d.permission("post")
async postCmd(msg: Message, args: { channel: Channel; content: string }) {
async postCmd(msg: Message, args: { channel: Channel; content?: string }) {
if (!(args.channel instanceof TextChannel)) {
msg.channel.createMessage(errorMessage("Channel is not a text channel"));
return;
}
const createdMsg = await args.channel.createMessage(args.content);
const content = args.content || undefined;
let downloadedAttachment;
let file;
if (msg.attachments.length) {
downloadedAttachment = await downloadFile(msg.attachments[0].url);
file = {
name: msg.attachments[0].filename,
file: await fsp.readFile(downloadedAttachment.path)
};
}
if (content == null && file == null) {
msg.channel.createMessage(errorMessage("Text content or attachment required"));
return;
}
const createdMsg = await args.channel.createMessage(content, file);
await this.savedMessages.setPermanent(createdMsg.id);
if (downloadedAttachment) {
downloadedAttachment.deleteFn();
}
}
/**

View file

@ -1,9 +1,15 @@
import at = require("lodash.at");
import at from "lodash.at";
import { Emoji, Guild, GuildAuditLogEntry, TextableChannel } from "eris";
import url from "url";
import tlds from "tlds";
import emojiRegex from "emoji-regex";
import fs from "fs";
const fsp = fs.promises;
import https from "https";
import tmp from "tmp";
/**
* Turns a "delay string" such as "1h30m" to milliseconds
* @param {String} str
@ -287,6 +293,41 @@ export async function createChunkedMessage(channel: TextableChannel, messageText
}
}
/**
* Downloads the file from the given URL to a temporary file, with retry support
*/
export function downloadFile(attachmentUrl: string, retries = 3): Promise<{ path: string; deleteFn: () => void }> {
return new Promise(resolve => {
tmp.file((err, path, fd, deleteFn) => {
if (err) throw err;
const writeStream = fs.createWriteStream(path);
https
.get(attachmentUrl, res => {
res.pipe(writeStream);
writeStream.on("finish", () => {
writeStream.end();
resolve({
path,
deleteFn
});
});
})
.on("error", httpsErr => {
fsp.unlink(path);
if (retries === 0) {
throw httpsErr;
} else {
console.warn("File download failed, retrying. Error given:", httpsErr.message);
resolve(downloadFile(attachmentUrl, retries - 1));
}
});
});
});
}
export function noop() {
// IT'S LITERALLY NOTHING
}