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

23 lines
No EOL
935 B
JavaScript

import jsqr from "jsqr";
import fetch from "node-fetch";
import sharp from "sharp";
import { clean } from "../../utils/misc.js";
import Command from "../../classes/command.js";
import imageDetect from "../../utils/imagedetect.js";
class QrReadCommand extends Command {
async run() {
const image = await imageDetect(this.client, this.message);
if (image === undefined) return "You need to provide an image with a QR code to read!";
this.client.sendChannelTyping(this.message.channel.id);
const data = await (await fetch(image.path)).buffer();
const rawData = await sharp(data).ensureAlpha().raw().toBuffer({ resolveWithObject: true });
const qrBuffer = jsqr(rawData.data, rawData.info.width, rawData.info.height);
if (!qrBuffer) return "I couldn't find a QR code!";
return `\`\`\`\n${await clean(qrBuffer.data)}\n\`\`\``;
}
static description = "Reads a QR code";
}
export default QrReadCommand;