From d012ce71858be8d8f89e62a4f90a10d160d1c29f Mon Sep 17 00:00:00 2001 From: lilyissillyyy Date: Sat, 6 Sep 2025 00:16:00 -0400 Subject: [PATCH] Fix readqr --- commands/analyze/generation.js | 2 +- commands/analyze/image-size.js | 2 +- commands/analyze/name-gender.js | 2 +- commands/analyze/read-qr-code.js | 38 ++++++++++++++++++++------------ commands/analyze/zodiac-sign.js | 2 +- package.json | 1 + 6 files changed, 29 insertions(+), 18 deletions(-) diff --git a/commands/analyze/generation.js b/commands/analyze/generation.js index 193eee2d..909b10d2 100644 --- a/commands/analyze/generation.js +++ b/commands/analyze/generation.js @@ -21,6 +21,6 @@ module.exports = class GenerationCommand extends Command { run(msg, { year }) { const generation = generations.find(gen => gen.start <= year && (gen.end ? gen.end >= year : true)); - return msg.say(`Someone born in ${year} is part of: **${generation.name}**`); + return msg.say(`Someone born in ${year} is part of **${generation.name}**.`); } }; diff --git a/commands/analyze/image-size.js b/commands/analyze/image-size.js index 7bf97c38..96c24228 100644 --- a/commands/analyze/image-size.js +++ b/commands/analyze/image-size.js @@ -25,6 +25,6 @@ module.exports = class ImageSizeCommand extends Command { async run(msg, { image }) { const { body } = await request.get(image); const data = await loadImage(body); - return msg.reply(`This image is ${data.width}x${data.height}.`); + return msg.reply(`This image is **${data.width}x${data.height}**.`); } }; diff --git a/commands/analyze/name-gender.js b/commands/analyze/name-gender.js index 9b8d8aae..e4870b8b 100644 --- a/commands/analyze/name-gender.js +++ b/commands/analyze/name-gender.js @@ -35,6 +35,6 @@ module.exports = class NameGenderCommand extends Command { .query({ name }); if (!body.gender) return msg.say(`I have no idea what gender ${body.name} is.`); const prob = Math.round(body.probability * 100); - return msg.say(`I'm ${prob}% sure ${body.name} is a ${genders[body.gender]} name.`); + return msg.say(`I'm ${prob}% sure ${body.name} is a **${genders[body.gender]}** name.`); } }; diff --git a/commands/analyze/read-qr-code.js b/commands/analyze/read-qr-code.js index c4128aa1..6262a3cd 100644 --- a/commands/analyze/read-qr-code.js +++ b/commands/analyze/read-qr-code.js @@ -1,5 +1,7 @@ const Command = require('../../framework/Command'); const request = require('node-superfetch'); +const QrCode = require('qrcode-reader'); +const { createCanvas, loadImage } = require('@napi-rs/canvas'); const { shorten } = require('../../util/Util'); module.exports = class ReadQRCodeCommand extends Command { @@ -9,14 +11,6 @@ module.exports = class ReadQRCodeCommand extends Command { aliases: ['scan-qr-code', 'scan-qr', 'read-qr'], group: 'analyze', description: 'Reads a QR Code.', - credit: [ - { - name: 'goQR.me', - url: 'http://goqr.me/', - reason: 'QR code API', - reasonURL: 'http://goqr.me/api/' - } - ], args: [ { key: 'image', @@ -28,11 +22,27 @@ module.exports = class ReadQRCodeCommand extends Command { } async run(msg, { image }) { - const { body } = await request - .get('https://api.qrserver.com/v1/read-qr-code/') - .query({ fileurl: image }); - const data = body[0].symbol[0]; - if (!data.data) return msg.reply(`Could not read QR Code: ${data.error}.`); - return msg.reply(shorten(data.data, 2000 - (msg.author.toString().length + 2))); + const { body } = await request.get(image); + const img = await loadImage(body); + const canvas = createCanvas(img); + const ctx = canvas.getContext('2d'); + const imgData = ctx.getImageData(0, 0, img.width, img.height); + try { + const result = await this.readQrCode(imgData); + return msg.reply(shorten(result, 2000)); + } catch (err) { + return msg.reply(`Could not read QR Code: \`${err.message}\`.`); + } + } + + readQrCode(imgData) { + const qr = new QrCode(); + return new Promise((res, rej) => { + qr.callback = (err, value) => { + if (err) return rej(err); + return res(value); + } + qr.decode(imgData); + }); } }; diff --git a/commands/analyze/zodiac-sign.js b/commands/analyze/zodiac-sign.js index 0f0e994d..1a5b9cca 100644 --- a/commands/analyze/zodiac-sign.js +++ b/commands/analyze/zodiac-sign.js @@ -27,7 +27,7 @@ module.exports = class ZodiacSignCommand extends Command { run(msg, { month, day }) { const sign = this.determineSign(month, day); if (!sign) return msg.reply('Invalid day.'); - return msg.say(`The Zodiac Sign for ${month}/${day} is ${sign.name}.`); + return msg.say(`The Zodiac Sign for ${month}/${day} is **${sign.name}**.`); } determineSign(month, day) { diff --git a/package.json b/package.json index d81e5b94..c577d4a7 100644 --- a/package.json +++ b/package.json @@ -70,6 +70,7 @@ "ntcjs": "^1.1.3", "parse-domain": "^8.2.2", "pokersolver": "^2.1.4", + "qrcode-reader": "^1.0.4", "random-js": "^2.1.0", "sagiri": "^4.3.0", "semver": "^7.7.2",