mrmBot-Matrix/commands/meme.js

34 lines
1.4 KiB
JavaScript
Raw Normal View History

2019-09-13 20:02:41 +00:00
const { spawn } = require("child_process");
exports.run = async (message, args) => {
message.channel.sendTyping();
2019-09-13 20:02:41 +00:00
const image = await require("../utils/imagedetect.js")(message);
if (image === undefined) return `${message.author.mention}, you need to provide an image to generate a meme!`;
if (image.type === "gif") return `${message.author.mention}, this command doesn't work with GIFs!`;
2019-09-13 20:02:41 +00:00
if (args.length === 0) return `${message.author.mention}, you need to provide some text to generate a meme!`;
const [topText, bottomText] = args.join(" ").split(",").map(elem => elem.trim());
const child = spawn("./utils/meme.sh", [topText.toUpperCase().replace(/\\/g, "\\\\"), bottomText ? bottomText.toUpperCase().replace(/\\/g, "\\\\") : ""]);
child.stdin.write(image.data);
2019-09-13 20:02:41 +00:00
child.stdin.end();
const chunks = [];
child.stdout.on("data", (data) => {
chunks.push(data);
});
child.once("error", (error) => {
if (error) throw error;
2019-09-13 20:02:41 +00:00
});
child.stderr.once("data", (error) => {
2020-01-06 00:02:24 +00:00
if (error) throw new Error(error.toString());
2019-09-13 20:02:41 +00:00
});
child.stdout.once("close", () => {
const data = Buffer.concat(chunks);
return message.channel.createMessage("", {
file: data,
name: "meme.png"
});
});
};
exports.category = 5;
exports.help = "Generates a meme from an image (separate top/bottom text with a comma)";
exports.params = "[top text], {bottom text}";