mirror of
https://github.com/arthur-pbty/xiao.git
synced 2026-06-09 09:12:07 +02:00
Move create QR away from API
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
const Command = require('../../framework/Command');
|
||||
const request = require('node-superfetch');
|
||||
const { createCanvas } = require('@napi-rs/canvas');
|
||||
const { encodeQR } = require('qr');
|
||||
|
||||
module.exports = class CreateQRCodeCommand extends Command {
|
||||
constructor(client) {
|
||||
@@ -8,14 +9,6 @@ module.exports = class CreateQRCodeCommand extends Command {
|
||||
aliases: ['create-qr'],
|
||||
group: 'edit-image',
|
||||
description: 'Converts text to a QR Code.',
|
||||
credit: [
|
||||
{
|
||||
name: 'goQR.me',
|
||||
url: 'http://goqr.me/',
|
||||
reason: 'QR code API',
|
||||
reasonURL: 'http://goqr.me/api/'
|
||||
}
|
||||
],
|
||||
args: [
|
||||
{
|
||||
key: 'text',
|
||||
@@ -26,9 +19,26 @@ module.exports = class CreateQRCodeCommand extends Command {
|
||||
}
|
||||
|
||||
async run(msg, { text }) {
|
||||
const { body } = await request
|
||||
.get('https://api.qrserver.com/v1/create-qr-code/')
|
||||
.query({ data: text });
|
||||
return msg.say({ files: [{ attachment: body, name: 'qr-code.png' }] });
|
||||
const qr = this.createQRCode(text);
|
||||
return msg.say({ files: [{ attachment: qr, name: 'qr-code.png' }] });
|
||||
}
|
||||
|
||||
createQRCode(text) {
|
||||
const qr = encodeQR(text, 'raw');
|
||||
const size = qr.length;
|
||||
const scale = 10;
|
||||
const canvas = createCanvas(size * scale, size * scale);
|
||||
const ctx = canvas.getContext('2d');
|
||||
ctx.fillStyle = 'white';
|
||||
ctx.fillRect(0, 0, canvas.width, canvas.height);
|
||||
ctx.fillStyle = 'black';
|
||||
for (let y = 0; y < size; y++) {
|
||||
for (let x = 0; x < size; x++) {
|
||||
if (qr[y][x]) {
|
||||
ctx.fillRect(x * scale, y * scale, scale, scale);
|
||||
}
|
||||
}
|
||||
}
|
||||
return canvas.toBuffer('image/png');
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user