Skip to content
This repository has been archived by the owner on Jan 19, 2025. It is now read-only.

Commit

Permalink
Merge pull request #862 from SwitchbladeBot/new-qrcode
Browse files Browse the repository at this point in the history
QR Code command and imageParameter improvements
  • Loading branch information
perronosaurio authored Jun 5, 2019
2 parents b77fabd + 8b15d1c commit d054078
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 11 deletions.
22 changes: 14 additions & 8 deletions src/commands/misc/qrcode.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
const { Command, SwitchbladeEmbed } = require('../../')

const types = ['generate', 'create', 'g', 'read', 'r']

module.exports = class QRCode extends Command {
constructor (client) {
super(client, {
name: 'qrcode',
aliases: ['qr'],
parameters: [{
type: 'string', full: true, missingError: 'commands:qrcode.noText'
type: 'string',
full: false,
whitelist: types,
missingError: ({ t, prefix }) => {
return new SwitchbladeEmbed().setTitle(t('commons:search.noType'))
.setDescription([
this.usage(t, prefix),
'',
`__**${t('commons:search.types')}:**__`,
`\`${['generate', 'read'].join('`, `')}\``
].join('\n'))
}
}]
})
}

async run ({ t, author, channel }, text) {
channel.send(
new SwitchbladeEmbed(author)
.setImage(`https://api.qrserver.com/v1/create-qr-code/?data=${encodeURIComponent(text)}`)
)
}
}
21 changes: 21 additions & 0 deletions src/commands/misc/qrcode/generate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const { Command, SwitchbladeEmbed } = require('../../../')

module.exports = class QRCodeGenerate extends Command {
constructor (client) {
super(client, {
name: 'generate',
aliases: ['create', 'g'],
parentCommand: 'qrcode',
parameters: [{
type: 'string', full: true, missingError: 'commands:qrcode.subcommands.generate.noText'
}]
})
}

async run ({ t, author, channel, language }, text) {
channel.send(
new SwitchbladeEmbed(author)
.setImage(`https://api.qrserver.com/v1/create-qr-code/?data=${encodeURIComponent(text)}`)
)
}
}
30 changes: 30 additions & 0 deletions src/commands/misc/qrcode/read.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const { Command, CommandError, SwitchbladeEmbed } = require('../../../')
const fetch = require('node-fetch')

module.exports = class QRCodeRead extends Command {
constructor (client) {
super(client, {
name: 'read',
aliases: ['r'],
parentCommand: 'qrcode',
parameters: [{
type: 'image',
authorAvatar: false,
url: true,
missingError: 'commands:qrcode.subcommands.read.noImage'
}]
})
}

async run ({ t, channel, author, message }, image) {
const body = await fetch(`http://api.qrserver.com/v1/read-qr-code/?fileurl=${encodeURIComponent(image)}`).then(res => res.json())
if (body[0].symbol[0].data !== null) {
channel.send(
new SwitchbladeEmbed(author)
.setDescription(body[0].symbol[0].data)
)
} else {
throw new CommandError(t('commands:qrcode.subcommands.read.unknownImage'))
}
}
}
17 changes: 14 additions & 3 deletions src/locales/en-US/commands.json
Original file line number Diff line number Diff line change
Expand Up @@ -457,9 +457,20 @@
"time": "Time"
},
"qrcode": {
"commandDescription": "Generates a QR Code.",
"commandUsage": "<text>",
"noText": " You have to give me some text!"
"commandDescription": "Generates or reads a QR code.",
"subcommands": {
"generate": {
"commandDescription": "Generates a QR code.",
"commandUsage": "<text>",
"noText": " You have to give me some text!"
},
"read": {
"commandDescription": "Reads a QR code.",
"commandUsage": "<image/link>",
"noImage": "You have to give me an image to read.",
"unknownImage": "You have to give me a valid QR code to read."
}
}
},
"strawpoll": {
"commandDescription": "Creates a Straw Poll.",
Expand Down
7 changes: 7 additions & 0 deletions src/structures/command/parameters/types/ImageParameter.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ module.exports = class ImageParameter extends Parameter {
return {
...super.parseOptions(options),
user,
url: defVal(options, 'url', false),
attachment: defVal(options, 'attachment', true),
link: defVal(options, 'link', true),
userOptions: user ? UserParameter.parseOptions(options.userOptions) : null,
Expand Down Expand Up @@ -70,6 +71,7 @@ module.exports = class ImageParameter extends Parameter {
parseState.argIndex--
const attachment = message.attachments.first()
try {
if (this.url) return attachment.url
const buffer = await imageRequest(attachment.url, client)
return buffer
} catch (e) {
Expand All @@ -82,6 +84,7 @@ module.exports = class ImageParameter extends Parameter {
// Link
if (this.link && isValidURL(arg)) {
try {
if (this.url) return arg
const buffer = await imageRequest(arg, client)
return buffer
} catch (e) {
Expand All @@ -96,6 +99,7 @@ module.exports = class ImageParameter extends Parameter {
const user = UserParameter._parse(arg, this.userOptions, context)
if (user) {
try {
if (this.url) return user.displayAvatarURL
const buffer = await imageRequest(user.displayAvatarURL, client)
return buffer
} catch (e) {
Expand All @@ -117,6 +121,7 @@ module.exports = class ImageParameter extends Parameter {
parseState.argIndex--
const attachment = msg.attachments.first()
try {
if (this.url) return attachment.url
const buffer = await imageRequest(attachment.url, client)
return buffer
} catch (e) {
Expand All @@ -136,6 +141,7 @@ module.exports = class ImageParameter extends Parameter {
if (url) {
parseState.argIndex--
try {
if (this.url) return url
const buffer = await imageRequest(url, client)
return buffer
} catch (e) {
Expand All @@ -152,6 +158,7 @@ module.exports = class ImageParameter extends Parameter {
if (this.authorAvatar) {
try {
parseState.argIndex--
if (this.url) return author.displayAvatarURL
const buffer = await imageRequest(author.displayAvatarURL, client)
return buffer
} catch (e) {
Expand Down

0 comments on commit d054078

Please sign in to comment.