-
Notifications
You must be signed in to change notification settings - Fork 4
/
advice.js
64 lines (55 loc) · 1.75 KB
/
advice.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
async function command(chatClient, db, channel, args) {
db.query('SELECT message FROM advice;', (err, res) => {
if (err) console.log(err);
let advices = [];
if (args.length == 0) {
advices = res.rows;
} else {
const search = args.join(' ');
for (const advice of res.rows) {
if (
advice.message.toLowerCase().includes(search.toLowerCase())
) {
advices.push(advice);
}
}
}
if (advices.length == 0) {
chatClient.say(channel, "You're on your own, buddy");
return;
}
const i = Math.floor(Math.random() * advices.length);
chatClient.say(channel, advices[i].message);
});
}
async function lookup(db, channel, args) {
const { rows } = await db.query('SELECT message FROM advice;');
let advices = [];
if (args.length == 0) {
advices = rows;
} else {
const search = args.join(' ');
for (const advice of rows) {
if (
advice.message.toLowerCase().includes(search.toLowerCase())
) {
advices.push(advice);
}
}
}
if (advices.length == 0) {
return "You're on your own, buddy";
}
const i = Math.floor(Math.random() * advices.length);
return advices[i].message;
}
async function add(chatClient, db, channel, user, message) {
await db.query('INSERT INTO advice (message, added_by) VALUES($1, $2)', [message, user]);
const uncap = message[0].toLowerCase() + message.substring(1)
chatClient.say(channel, `Thank you, ${user}, I will remember to ${uncap}.`);
}
module.exports = {
add,
command,
lookup
};