-
Notifications
You must be signed in to change notification settings - Fork 4
/
money.js
35 lines (30 loc) · 1.14 KB
/
money.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
async function earn(chatClient, db, channel, user, amount = 1) {
const { rows } = await db.query('SELECT money FROM money WHERE user_name = $1', [user]);
let money = 0;
if (rows.length > 0) {
money = rows[0].money;
}
money += amount;
chatClient.say(channel, `${user} earns ${amount} PepperMoney and now has ${money}!`);
if (rows.length == 0) {
await db.query('INSERT INTO money (user_name, money) VALUES($1, $2)', [user, money]);
} else {
await db.query('UPDATE money SET money = $1 WHERE user_name = $2', [money, user]);
}
}
async function report(chatClient, db, channel, user) {
const { rows } = await db.query('SELECT money FROM money WHERE user_name = $1', [user]);
let money = 0;
if (rows.length > 0) {
money = rows[0].money;
}
chatClient.say(channel, `${user} has ${money} PepperMoney`);
}
async function leaders(db) {
const { rows } = await db.query('SELECT user_name, money FROM money ORDER BY money DESC LIMIT 3');
return rows.map(row => ({
'name': row.user_name,
'money': row.money
}));
}
module.exports = { earn, report, leaders };