-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathawesome.js
103 lines (92 loc) · 3.36 KB
/
awesome.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
const pronouns = require("./pronouns.js");
const chatters = new Map();
const SECONDS = 1000;
const MINUTES = 60 * SECONDS;
async function add(apiClient, channel, user) {
const u = await apiClient.users.getUserByName(user);
if (chatters.has(channel)) {
chatters.get(channel).set(u.displayName, Date.now());
} else {
chatters.set(channel, new Map([[u.displayName, Date.now()]]));
}
}
function prune() {
for (const [channel, users] of chatters) {
for (const [user, ts] of users) {
if (Date.now() - ts > 30 * MINUTES) {
console.log(`Pruning ${user} from active chatters in ${channel}`)
users.delete(user);
}
}
}
}
async function third_person(user) {
return pronouns.get_pronoun_id(user).then(id => {
switch (id) {
case "aeaer": return "aer";
case "any": return ["him", "her", "them"][Math.floor(Math.random() * 3)];
case "eem": return "em";
case "faefaer": return "faer";
case "hehim": return "him";
case "heshe": return ["him", "her"][Math.floor(Math.random() * 2)];
case "hethem": return ["him", "them"][Math.floor(Math.random() * 2)];
case "itits": return "it";
case "other": return "this one";
case "perper": return "per";
case "sheher": return "her";
case "shethem": return ["her", "them"][Math.floor(Math.random() * 2)];
case "theythem": return "them";
case "vever": return "ver";
case "xexem": return "xem";
case "ziehir": return "hir";
default: return "them";
}
}).catch(() => "them");
}
function get(channel, self) {
prune();
let users = [];
if (chatters.has(channel)) {
users = Array.from(chatters.get(channel).keys());
}
if (users.length === 0) {
return;
}
users.push(self);
const i = Math.floor(Math.random() * users.length);
return users[i];
}
async function command(chatClient, channel, self, db) {
let user = get(channel, self);
if (user) {
const them = await third_person(user);
chatClient.say(channel, `You know who's awesome? ${user} is awesome! We love ${them}.`);
const { rows } = await db.query('SELECT message FROM catchphrases WHERE user_name = $1', [user]);
if (rows.length > 0) {
chatClient.say(channel, rows[0].message);
}
}
}
async function setCatchphrase(chatClient, apiClient, channel, db, user, catchphrase) {
const u = await apiClient.users.getUserByName(user);
const { rows } = await db.query('SELECT * FROM catchphrases WHERE user_name = $1', [u.displayName]);
if (rows.length === 0) {
await db.query('INSERT INTO catchphrases (user_name, message) VALUES ($1, $2)', [u.displayName, catchphrase]);
} else {
await db.query('UPDATE catchphrases SET message = $1 WHERE user_name = $2', [catchphrase, u.displayName]);
}
chatClient.say(channel, 'Catchphrase for ' + u.displayName + ' set to: ' + catchphrase);
}
function dumpChatters(chatClient, channel) {
prune();
for (const [user, ts] of chatters.get(channel)) {
chatClient.say(channel, user + ' last spoke at ' + new Date(ts).toString());
}
}
module.exports = {
add,
get,
command,
dumpChatters,
setCatchphrase
};