-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathquote.js
121 lines (108 loc) · 4.81 KB
/
quote.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
const quiz = require('./quiz.js');
async function command(chatClient, apiClient, db, channel, channelId, user, args) {
if (quiz.quiz_active()) {
chatClient.say(channel, `NO CHEATING, ${user}! SHAME ON YOU!`);
return;
}
if (args.length == 1 && args[0] == "add") {
chatClient.say(channel, "You have to... actually say the quote...");
} else if (args.length > 1 && args[0] == "add" ) {
args.shift();
const quote = args.join(' ');
const broadcaster = await apiClient.users.getUserByName(channel);
const ch = await apiClient.channels.getChannelInfoById(broadcaster);
const { rows } = await db.query('SELECT MAX(number) FROM quotes WHERE channel=$1', [channel]);
const number = rows[0].max + 1;
const result = await db.query('INSERT INTO quotes(message, quoted_by, game, channel, number) VALUES($1, $2, $3, $4, $5)', [quote, user, ch.gameName, channel, number]);
chatClient.say(channel, `Successfully added quote #${number}: "${quote}" [${ch.gameName}]`);
} else if (args.length == 1 && args[0].match(/^\d+$/)) {
const id = parseInt(args[0], 10);
db.query('SELECT number, message, game, created_at FROM quotes WHERE number = $1 AND channel = $2;', [id, channel], (err, res) => {
if (err) console.log(err);
if (res.rows.length > 0) {
const q = res.rows[0]
const date = new Date(q.created_at);
chatClient.say(channel, `#${q.number}: "${q.message}" [${q.game}] ${date.toDateString()}`);
} else {
chatClient.say(channel, `Quote #${id} not found`);
}
});
} else {
db.query('SELECT number, message, game, quoted_by, created_at FROM quotes WHERE channel = $1;', [channel], (err, res) => {
if (err) console.log(err);
let quotes = [];
if (args.length == 0) {
quotes = res.rows;
} else {
const search = args.join(' ');
for (const quote of res.rows) {
if (
search.toLowerCase() == String(quote.game).toLowerCase() ||
search.toLowerCase() == String(quote.quoted_by).toLowerCase() ||
quote.message.toLowerCase().includes(search.toLowerCase())
) {
quotes.push(quote);
}
}
}
if (quotes.length == 0) {
chatClient.say(channel, "Sorry, I can't find any quotes about that");
return;
}
const i = Math.floor(Math.random() * quotes.length);
const q = quotes[i]
const date = new Date(q.created_at);
chatClient.say(channel, `#${q.number}: "${q.message}" [${q.game}] ${date.toDateString()}`);
});
}
}
async function quote(db, channel) {
const { rows } = await db.query('SELECT id, message, game, quoted_by, created_at FROM quotes WHERE channel = $1;', [channel]);
const i = Math.floor(Math.random() * rows.length);
return rows[i];
}
async function lookup(db, channel, user, args) {
if (quiz.quiz_active()) {
return `NO CHEATING, ${user}! SHAME ON YOU!`;
}
if (args.length == 1 && args[0].match(/^\d+$/)) {
const id = parseInt(args[0], 10);
const { rows } = await db.query('SELECT number, message, game, created_at FROM quotes WHERE number = $1 AND channel = $2;', [id, channel]);
if (rows.length > 0) {
const q = rows[0]
const date = new Date(q.created_at);
return `#${q.number}: "${q.message}" [${q.game}] ${date.toDateString()}`;
} else {
return`Quote #${id} not found`;
}
} else {
const { rows } = await db.query('SELECT number, message, game, quoted_by, created_at FROM quotes WHERE channel = $1;', [channel]);
let quotes = [];
if (args.length == 0) {
quotes = rows;
} else {
const search = args.join(' ');
for (const quote of rows) {
if (
search.toLowerCase() == String(quote.game).toLowerCase() ||
search.toLowerCase() == String(quote.quoted_by).toLowerCase() ||
quote.message.toLowerCase().includes(search.toLowerCase())
) {
quotes.push(quote);
}
}
}
if (quotes.length == 0) {
return "Sorry, I can't find any quotes about that";
}
const i = Math.floor(Math.random() * quotes.length);
const q = quotes[i]
const date = new Date(q.created_at);
return `#${q.number}: "${q.message}" [${q.game}] ${date.toDateString()}`;
}
}
module.exports = {
command,
quote,
lookup,
};