2022-01-13 01:47:36 +00:00
|
|
|
import jsqr from "jsqr";
|
2022-08-11 16:46:56 +00:00
|
|
|
import { request } from "undici";
|
2022-01-13 01:47:36 +00:00
|
|
|
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() {
|
2022-03-31 05:42:03 +00:00
|
|
|
const image = await imageDetect(this.client, this.message, this.interaction, this.options);
|
2022-01-26 18:53:20 +00:00
|
|
|
if (image === undefined) return "You need to provide an image/GIF with a QR code to read!";
|
2022-04-05 03:05:28 +00:00
|
|
|
await this.acknowledge();
|
2022-08-11 16:46:56 +00:00
|
|
|
const data = Buffer.from(await (await request(image.path)).body.arrayBuffer());
|
2022-01-13 01:47:36 +00:00
|
|
|
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";
|
2022-03-31 05:42:03 +00:00
|
|
|
static flags = [{
|
|
|
|
name: "image",
|
|
|
|
type: 11,
|
|
|
|
description: "An image/GIF attachment"
|
|
|
|
}, {
|
|
|
|
name: "link",
|
|
|
|
type: 3,
|
|
|
|
description: "An image/GIF URL"
|
|
|
|
}];
|
2022-01-13 01:47:36 +00:00
|
|
|
}
|
|
|
|
|
2022-01-26 18:53:20 +00:00
|
|
|
export default QrReadCommand;
|