mrmBot-Matrix/commands/general/qrcreate.js
Essem 64462f2202
Revert qrcreate/qrread removal
The impact wasn't too bad in retrospect
2022-01-12 19:47:36 -06:00

37 lines
No EOL
998 B
JavaScript

import qrcode from "qrcode";
import { PassThrough } from "stream";
import Command from "../../classes/command.js";
class QrCreateCommand extends Command {
async run() {
if (this.args.length === 0) return "You need to provide some text to generate a QR code!";
this.client.sendChannelTyping(this.message.channel.id);
const writable = new PassThrough();
qrcode.toFileStream(writable, this.content, { margin: 1 });
const file = await this.streamToBuf(writable);
return {
file: file,
name: "qr.png"
};
}
streamToBuf(stream) {
return new Promise((resolve, reject) => {
const chunks = [];
stream.on("data", (chunk) => {
chunks.push(chunk);
});
stream.once("error", (error) => {
reject(error);
});
stream.once("end", () => {
resolve(Buffer.concat(chunks));
});
});
}
static description = "Generates a QR code";
static arguments = ["[text]"];
}
export default QrCreateCommand;