Skip to content

Commit

Permalink
Update donger.js
Browse files Browse the repository at this point in the history
  • Loading branch information
LeonAlvarez authored Oct 6, 2019
1 parent a0b3d3f commit 495b516
Showing 1 changed file with 26 additions and 18 deletions.
44 changes: 26 additions & 18 deletions features/donger.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,32 @@

const donger = require('cool-ascii-faces');

const selectDonger = (dongerIndex) => {

if (dongerIndex === undefined) {
const value = donger();
return {value, index: donger.faces.indexOf(value)}
}

if (dongerIndex >= donger.faces.length) {
throw "Index bigger than dongers length";
}

return {value: donger.faces[dongerIndex], index: dongerIndex}
};

module.exports = ({message, reply}) => {
const commandHasIndex = /\/donger \d+$/.exec(message.text);
let selectedDonger;
let dongerIndex;
if (commandHasIndex) {
dongerIndex = parseInt(/\d+$/.exec(message.text)[0], 10);
if (dongerIndex >= donger.faces.length) {
reply(`Solo hay ${donger.faces.length - 1} dongers :c`);
return;
}
selectedDonger = donger.faces[dongerIndex];
} else {
selectedDonger = donger();
dongerIndex = donger.faces.indexOf(selectedDonger);
try {
const {groups: {index}} = /\/donger\s?(?<index>\d+)?$/.exec(message.text);
const selectedDonger = selectDonger(index);

return reply(`
Donger ${selectedDonger.index}:
${selectedDonger.value}
`);

} catch(err) {
console.log(err);
return reply(`Solo hay ${donger.faces.length - 1} dongers :c`);
}
const msg = `
Donger ${dongerIndex}:
${selectedDonger}
`;
reply(msg);
};

0 comments on commit 495b516

Please sign in to comment.