forked from joepie91/barcodebot
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
83 lines
1.7 KiB
JavaScript
83 lines
1.7 KiB
JavaScript
4 years ago
|
"use strict";
|
||
|
|
||
|
const unhandledError = require("unhandled-error");
|
||
|
|
||
|
const config = require("./config.json");
|
||
|
const shortenUrl = require("./shorten-url");
|
||
|
|
||
|
unhandledError((error) => {
|
||
|
console.error(error.stack);
|
||
|
});
|
||
|
|
||
|
const Promise = require("bluebird");
|
||
|
const IRC = require("irc-framework");
|
||
|
|
||
|
const getBarcode = require("./get-barcode");
|
||
|
|
||
|
function cleanBarcode(barcode) {
|
||
|
return barcode.replace(/[^0-9]+/g, "");
|
||
|
}
|
||
|
|
||
|
let eanRegex = /^([0-9]{8}|[0-9]{13})$/;
|
||
|
|
||
|
let bot = new IRC.Client();
|
||
|
|
||
|
bot.on("message", (event) => {
|
||
|
let match, barcode, explicit;
|
||
|
|
||
|
if (eanRegex.test(event.message)) {
|
||
|
barcode = event.message;
|
||
|
explicit = false;
|
||
|
} else if ((match = /^!barcode (.+)$/.exec(event.message)) != null) {
|
||
|
if (true || eanRegex.test(match[1])) {
|
||
|
barcode = match[1];
|
||
|
|
||
|
explicit = true;
|
||
|
} else {
|
||
|
event.reply("Ongeldige barcode!");
|
||
|
return;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (barcode != null) {
|
||
|
return Promise.try(() => {
|
||
|
console.log(`Looking up: ${barcode}`);
|
||
|
|
||
|
return getBarcode(cleanBarcode(barcode));
|
||
|
}).then((result) => {
|
||
|
console.log(result);
|
||
|
|
||
|
if (result == null) {
|
||
|
if (explicit === true) {
|
||
|
event.reply("Barcode niet gevonden!")
|
||
|
}
|
||
|
} else {
|
||
|
let price = (result.price != null)
|
||
|
? result.price
|
||
|
: "onbekend";
|
||
|
|
||
|
return Promise.try(() => {
|
||
|
if (result.image != null) {
|
||
|
return shortenUrl(result.image);
|
||
|
} else {
|
||
|
return "niet beschikbaar";
|
||
|
}
|
||
|
}).then((image) => {
|
||
|
event.reply(`${result.name} -- Prijsindicatie: ${price} -- Productfoto: ${image}`);
|
||
|
});
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
})
|
||
|
|
||
|
bot.on("registered", () => {
|
||
|
console.log("Connected!");
|
||
|
bot.join(config.irc.channel);
|
||
|
});
|
||
|
|
||
|
bot.connect({
|
||
|
host: config.irc.host,
|
||
|
port: config.irc.port,
|
||
|
nick: config.irc.nick
|
||
|
});
|