Skip to content

Commit b98d4e1

Browse files
authored
Ditch Discordbot (#32)
* checking in changes for a bit * fixed sf gg * fixed timezone for dev mode * happy enough with changes * let handlers stack
1 parent f6c37e3 commit b98d4e1

File tree

13 files changed

+484
-315
lines changed

13 files changed

+484
-315
lines changed

.eslintrc.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"env": {
3+
"es6": true,
4+
"node": true
5+
},
6+
"extends": "eslint:recommended",
7+
"parserOptions": {
8+
"ecmaVersion": 2015
9+
},
10+
"rules": {
11+
"indent": ["error", 2],
12+
"linebreak-style": ["error", "unix"],
13+
"quotes": ["error", "double"],
14+
"semi": ["error", "always"]
15+
}
16+
}

Dockerfile.dev

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
FROM mhart/alpine-node:10.7.0
2-
RUN apk update && apk upgrade && apk add git file imagemagick && npm i -g nodemon > /dev/null
2+
RUN apk update && apk upgrade && apk add tzdata git file imagemagick curl && npm i -g nodemon > /dev/null
3+
ENV TZ America/Chicago
4+
RUN ln -snf /usr/share/zoneinfo/$TZ /etclocaltime && echo $TZ > /etc/timezone
35
WORKDIR /code/
46
COPY package*.json ./
57
RUN npm i > /dev/null
Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,39 @@
1-
module.exports = (searchTitle, searchUrl, x, isEbay = false) => {
2-
const MAX_RESULTS = 50;
3-
const RESULTS_LIMIT = 7;
4-
/**
5-
* slack doesn't need full results listed, just first three
6-
* @type {*}
7-
*/
8-
const sharedResults = x.slice(0, RESULTS_LIMIT);
9-
/**
10-
* counting unshown results here
11-
* @type {string}
12-
*/
13-
const hiddenResultsIdenifier = x.length >= MAX_RESULTS ? 'many' : x.length - RESULTS_LIMIT > 0 ? 'some' : 'no';
1+
module.exports = (searchTitle, searchUrl, x = [], isEbay = false) => {
2+
const MAX_RESULTS = 50;
3+
const RESULTS_LIMIT = 7;
4+
/**
5+
* slack doesn't need full results listed, just first three
6+
* @type {*}
7+
*/
8+
const sharedResults = x.slice(0, RESULTS_LIMIT);
9+
/**
10+
* counting unshown results here
11+
* @type {string}
12+
*/
13+
// prettier-ignore
14+
const hiddenResultsIdenifier =
15+
x.length >= MAX_RESULTS ? "many" :
16+
x.length - RESULTS_LIMIT > 0 ? "some" : "no";
1417

15-
return [
16-
`Searched for \`${searchTitle}\``,
17-
''
18-
]
19-
.concat(sharedResults.map(r => {
20-
const shipping = !!r.shipping && !isNaN(r.shipping) && `+ $${r.shipping}s/h`;
21-
const year = r.year && `(${r.year})`;
18+
return [`Searched for \`${searchTitle}\``, ""]
19+
.concat(
20+
sharedResults.map(r => {
21+
const shipping =
22+
!!r.shipping && !isNaN(r.shipping) && `+ $${r.shipping}s/h`;
23+
const year = r.year && `(${r.year})`;
2224

23-
return [
24-
[`**$${r.price}**`, shipping, year].filter(s => !!s).join(' '),
25-
r.about.length > 120 ? r.about.substr(0, 120) + '...' : r.about,
26-
''
27-
]
28-
.join('\n');
29-
}))
30-
.concat([
31-
!isEbay ? searchUrl : undefined,
32-
!isEbay ? `There are ${hiddenResultsIdenifier} more results in the search above :point_up:` : undefined
33-
])
34-
.join('\n');
25+
return [
26+
[`**$${r.price}**`, shipping, year].filter(s => !!s).join(" "),
27+
r.about.length > 120 ? r.about.substr(0, 120) + "..." : r.about,
28+
""
29+
].join("\n");
30+
})
31+
)
32+
.concat([
33+
!isEbay ? searchUrl : undefined,
34+
!isEbay
35+
? `There are ${hiddenResultsIdenifier} more results in the search above :point_up:`
36+
: undefined
37+
])
38+
.join("\n");
3539
};

handlers/bookmancy/index.js

Lines changed: 65 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,72 @@
1-
const {bookmancy} = require('@dillonchr/funhouse');
2-
const {trackError} = require('../../utils');
3-
const funhouseResponseTransformer = require('./funhouse-response-transformer');
4-
const confirmMessage = require('./confirmation-messages');
1+
const { bookmancy } = require("@dillonchr/funhouse");
2+
const { trackError } = require("../../utils");
3+
const funhouseResponseTransformer = require("./funhouse-response-transformer");
4+
const confirmMessage = require("./confirmation-messages");
55
const is = {
6-
abe: s => /^abe /i.test(s),
7-
ebay: s => /^ebay /i.test(s),
8-
sold: s => /^sold /i.test(s),
9-
live: s => /^live /i.test(s)
6+
abe: s => /^abe /i.test(s),
7+
ebay: s => /^ebay /i.test(s),
8+
sold: s => /^sold /i.test(s),
9+
live: s => /^live /i.test(s)
1010
};
1111

1212
module.exports = bot => {
13-
bot.hears(['ebay', 'abe'], ({reply, content}) => {
14-
if (is.abe(content)) {
15-
reply(confirmMessage());
16-
const messagePieces = content.substr(4).split(',').map(s => s.trim());
17-
const [author, title, publisher, year, format] = messagePieces;
18-
const query = {
19-
author,
20-
title,
21-
publisher,
22-
year: format && !isNaN(format) ? format : !isNaN(year) && year,
23-
format: isNaN(year) ? year : format,
24-
source: 'abe',
25-
includeUrl: true
26-
};
27-
bookmancy(query, (err, response) => {
28-
if (err) {
29-
trackError(err);
30-
return reply(`Search error: ${err.message}`);
31-
}
13+
bot.hears(["ebay", "abe"], ({ reply, content }) => {
14+
if (is.abe(content)) {
15+
reply(confirmMessage());
16+
const messagePieces = content
17+
.substr(4)
18+
.split(",")
19+
.map(s => s.trim());
20+
const [author, title, publisher, year, format] = messagePieces;
21+
const query = {
22+
author,
23+
title,
24+
publisher,
25+
year: format && !isNaN(format) ? format : !isNaN(year) && year,
26+
format: isNaN(year) ? year : format,
27+
source: "abe",
28+
includeUrl: true
29+
};
30+
bookmancy(query, (err, response) => {
31+
if (err) {
32+
trackError(err);
33+
return reply(`Search error: ${err.message}`);
34+
}
3235

33-
const searchTitle = messagePieces.join(' - ');
34-
reply(funhouseResponseTransformer(searchTitle, response.url, response.results));
35-
});
36-
} else if (is.ebay(content)) {
37-
reply(confirmMessage());
38-
const searchSansEbay = content.substr(5);
39-
const isLive = is.live(searchSansEbay);
40-
const isSold = is.sold(searchSansEbay);
41-
const searchTitle = searchSansEbay.replace(/^live|sold/i, '').trim();
42-
const query = {
43-
author: searchTitle,
44-
source: 'ebay'
45-
};
46-
if (isLive) {
47-
query.live = true;
48-
}
49-
if (isSold) {
50-
query.sold = true;
51-
}
52-
bookmancy(query, (err, response) => {
53-
if (err) {
54-
trackError(err);
55-
return reply(`Search error: ${err.message}`);
56-
}
57-
reply(funhouseResponseTransformer(searchTitle, '', response.results, true));
58-
});
36+
const searchTitle = messagePieces.join(" - ");
37+
reply(
38+
funhouseResponseTransformer(
39+
searchTitle,
40+
response.url,
41+
response.results
42+
)
43+
);
44+
});
45+
} else if (is.ebay(content)) {
46+
reply(confirmMessage());
47+
const searchSansEbay = content.substr(5);
48+
const isLive = is.live(searchSansEbay);
49+
const isSold = is.sold(searchSansEbay);
50+
const searchTitle = searchSansEbay.replace(/^live|sold/i, "").trim();
51+
const query = {
52+
author: searchTitle,
53+
source: "ebay"
54+
};
55+
if (isLive) {
56+
query.live = true;
57+
}
58+
if (isSold) {
59+
query.sold = true;
60+
}
61+
bookmancy(query, (err, response) => {
62+
if (err) {
63+
trackError(err);
64+
return reply(`Search error: ${err.message}`);
5965
}
60-
});
66+
reply(
67+
funhouseResponseTransformer(searchTitle, "", response.results, true)
68+
);
69+
});
70+
}
71+
});
6172
};

handlers/cryptonics.js

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
1-
const {cryptonics} = require('@dillonchr/funhouse');
2-
const {trackError} = require('../utils');
1+
const { cryptonics } = require("@dillonchr/funhouse");
2+
const { trackError } = require("../utils");
33
const is = {
4-
encrypt: s => /^encrypt \d+;/i.test(s),
5-
decrypt: s => /^decrypt \d+;/i.test(s)
4+
encrypt: s => /^encrypt \d+;/i.test(s),
5+
decrypt: s => /^decrypt \d+;/i.test(s)
66
};
77

88
module.exports = bot => {
9-
bot.hears(['encrypt', 'decrypt'], ({reply, content}) => {
10-
if (is.encrypt(content) || is.decrypt(content)) {
11-
const firstSemi = content.indexOf(';');
12-
const offset = +content.substr(0, firstSemi).match(/\d+/)[0];
13-
const message = content.substr(firstSemi + 1);
14-
const isEncrypt = is.encrypt(content);
15-
const call = isEncrypt ? cryptonics.encrypt : cryptonics.decrypt;
9+
bot.hears(["encrypt", "decrypt"], ({ reply, content }) => {
10+
if (is.encrypt(content) || is.decrypt(content)) {
11+
const firstSemi = content.indexOf(";");
12+
const offset = +content.substr(0, firstSemi).match(/\d+/)[0];
13+
const message = content.substr(firstSemi + 1);
14+
const isEncrypt = is.encrypt(content);
15+
const call = isEncrypt ? cryptonics.encrypt : cryptonics.decrypt;
1616

17-
call(offset, message, (err, response) => {
18-
if (err) {
19-
trackError(err);
20-
return reply(`Error translating: ${err.message}`);
21-
}
22-
23-
const emoji = isEncrypt ? ':lock:' : ':unlock:';
24-
reply(`${emoji}\n\`\`\`${response.body}\`\`\``);
25-
});
17+
call(offset, message, (err, response) => {
18+
if (err) {
19+
trackError(err);
20+
return reply(`Error translating: ${err.message}`);
2621
}
27-
});
22+
23+
const emoji = isEncrypt ? ":lock:" : ":unlock:";
24+
reply(`${emoji}\n\`\`\`${response.body}\`\`\``);
25+
});
26+
}
27+
});
2828
};

handlers/dailytext.js

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
1-
const {dailytext} = require('@dillonchr/funhouse');
2-
const {trackError} = require('../utils/index');
1+
const { dailytext } = require("@dillonchr/funhouse");
2+
const { trackError } = require("../utils/index");
33

4+
module.exports = controller => {
5+
controller.hears(["daily text", "dailytext"], ({ reply }) =>
6+
dailytext((err, t) => {
7+
if (err) {
8+
reply(`Text problem: ${err.message}`);
9+
return trackError(err);
10+
}
411

5-
module.exports = (controller) => {
6-
controller.hears(['daily text', 'dailytext'], ({reply}) => dailytext((err, t) => {
7-
if (err) {
8-
reply(`Text problem: ${err.message}`);
9-
return trackError(err);
10-
}
11-
12-
reply(
13-
[
14-
`**${t.date}** - **${t.themeScriptureLocation}**`,
15-
`\`\`\`${t.themeScripture}\`\`\``,
16-
t.comments
17-
].join('\n'));
18-
}));
12+
reply(
13+
[
14+
`**${t.date}** - **${t.themeScriptureLocation}**`,
15+
`\`\`\`${t.themeScripture}\`\`\``,
16+
t.comments
17+
].join("\n")
18+
);
19+
})
20+
);
1921
};

handlers/gdq.js

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,36 @@
1-
const {gdq} = require('@dillonchr/funhouse');
2-
const {trackError} = require('../utils');
3-
const moment = require('moment');
1+
const { gdq } = require("@dillonchr/funhouse");
2+
const { trackError } = require("../utils");
3+
const moment = require("moment");
44

5-
const timestampToDisplayTime = (timestamp) => {
6-
return moment(timestamp).utcOffset('-05:00').format('h:mm A');
5+
const timestampToDisplayTime = timestamp => {
6+
return moment(timestamp).utcOffset("-05:00").format("h:mm A");
77
};
88

99
module.exports = bot => {
10-
bot.hears(['gdq', ':video_game:'], ({reply}) => {
11-
gdq((err, g) => {
12-
if (g && g.length) {
13-
const response = g
14-
.filter(g => !g.done)
15-
.slice(0, 5)
16-
.map(({runners, title, start, ends, estimate}) => {
17-
return [
18-
`**${title}**`,
19-
`Starts: **${timestampToDisplayTime(start)}**`,
20-
`Estimate: _${estimate}_`,
21-
`Ends: **${timestampToDisplayTime(ends)}**`,
22-
`${runners}`,
23-
''
24-
].join('\n');
25-
})
26-
.join('\n');
27-
reply(response || 'No upcoming runs :video_game:');
28-
} else {
29-
if (err) {
30-
trackError(err);
31-
}
32-
reply('Sorry, I got nothin\'.');
33-
}
34-
});
10+
bot.hears(["gdq", ":video_game:"], ({ reply }) => {
11+
gdq((err, g) => {
12+
if (Array.isArray(g) && g.length) {
13+
const response = g
14+
.filter(g => !g.done)
15+
.slice(0, 5)
16+
.map(({ runners, title, start, ends, estimate }) => {
17+
return [
18+
`**${title}**`,
19+
`Starts: **${timestampToDisplayTime(start)}**`,
20+
`Estimate: _${estimate}_`,
21+
`Ends: **${timestampToDisplayTime(ends)}**`,
22+
`${runners}`,
23+
""
24+
].join("\n");
25+
})
26+
.join("\n");
27+
reply(response || "No upcoming runs :video_game:");
28+
} else {
29+
if (err) {
30+
trackError(err);
31+
}
32+
reply("Sorry, I got nothin'.");
33+
}
3534
});
35+
});
3636
};

0 commit comments

Comments
 (0)