Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add barcode information command #1255

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@ OSU_API_KEY=934df9309099a59a9a9c920d
OSU_CLIENT_ID=95
OSU_CLIENT_SECRET=a483da02d2af24889f092d2
OWLBOT_KEY=73892rbg8egy6t574bytybg8ted6tg5g3e87
UPC_BARCODE_KEY=EDEF1440461CB6D6546A2597C31276F1
LANGUAGELAYER_API_KEY=1a5fdsc2e83dddd4ad1e12a59fa585
19 changes: 19 additions & 0 deletions src/apis/BarCode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const { APIWrapper } = require('../')
const axios = require('axios')

const API_URL = 'https://api.upcdatabase.org'

module.exports = class UPCBarcodeAPI extends APIWrapper {
constructor () {
super({
name: 'barcode',
envVars: [ 'UPC_BARCODE_KEY' ]
})
}

request (query) {
return axios({
url: `${API_URL}/product/${encodeURIComponent(query)}/?apikey=${process.env.UPC_BARCODE_KEY}`
})
}
}
53 changes: 53 additions & 0 deletions src/commands/misc/barcode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const { Command, CommandError, SwitchbladeEmbed, Constants } = require('../../')

module.exports = class UPCBarcode extends Command {
constructor (client) {
super({
name: 'barcode',
aliases: ['upc', 'upcbarcode', 'upcbarc'],
category: 'misc',
requirements: {
apis: [ 'barcode' ]
},
parameters: [{
type: 'string',
full: true,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

barcode has spaces?
image
image

missingError: 'commands:barcode.noCode'
}]
}, client)
}

async run ({ t, channel }, code) {
channel.startTyping()
try {
const { data } = await this.client.apis.barcode.request(code)
const embed = new SwitchbladeEmbed()
.setTitle(data.title)
.setColor(Constants.BARCODE_COLOR)
.setFooter(
t('commands:barcode.footer'),
'https://upcdatabase.org/images/logo.png'
)
if (data.description !== '') embed.setDescription(`/${data.description}/`)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the /?

if (data.images) embed.setThumbnail(data.images[0])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (data.images.length) embed.setThumbnail(data.images[0])

if (data.stores) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (data.stores.length) {

embed.addFields(
data.stores.map(d =>
({
name: d.store,
price: `${d.price ? `$${d.price}` : ''}`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

value: `${d.price ? `$${d.price}` : ''}`

})
)
)
}
channel.send(embed).then(() => channel.stopTyping())
} catch (err) {
channel.stopTyping()
if (err.response && err.response.status === 404) {
throw new CommandError(t('commands:barcode.notFound'))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It isn't throwing this when no information is found
image
data result:
image

} else {
throw new CommandError(t('commands:barcode.error'))
}
}
}
}
8 changes: 8 additions & 0 deletions src/locales/en-US/commands.json
Original file line number Diff line number Diff line change
Expand Up @@ -1855,6 +1855,14 @@
"notFound": "Word not found",
"error": "An error has occurred while requesting definitons. Please try again later."
},
"barcode": {
"commandDescription": "Shows information about a product for a given barcode",
"commandUsage": "<product barcode>",
"noCode": "You have to give me a product barcode",
"notFound": "Product not found",
"footer": "Provided by UPC database API: api.upcdatabase.org",
"error": "An error has occurred while requesting product information. Please try again later."
},
"packagist": {
"commandDescription": "Shows information about public PHP packages installable with Composer.",
"commandUsage": "<package name>",
Expand Down
1 change: 1 addition & 0 deletions src/utils/Constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ module.exports = {
FLATHUB_COLOR: '#4A86CF',
INSTAGRAM_COLOR: '#E1306C',
OWLBOT_COLOR: '#82d2d4',
BARCODE_COLOR: '#04649c',
PACKAGIST_COLOR: '#d8dff2',
WIKIPEDIA_COLOR: '#e6e6e8',
STEAM_COLOR: '#1b2838',
Expand Down