-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqlFunctions.js
More file actions
56 lines (46 loc) · 2.27 KB
/
sqlFunctions.js
File metadata and controls
56 lines (46 loc) · 2.27 KB
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
const SQLite = require("better-sqlite3");
const scoresDb = new SQLite("./scores.sqlite");
const ticketsDb = new SQLite("./lottoTickets.sqlite");
//const { client } = require("./index");
function getScore(userId, guildId) {
const score = scoresDb.prepare("SELECT * FROM scores WHERE user = ? AND guild = ?");
return score.get(userId, guildId);
}
function setScore(newScore) {
const score = scoresDb.prepare("INSERT OR REPLACE INTO scores (id, user, guild, points, level) VALUES (@id, @user, @guild, @points, @level);");
return score.run(newScore);
}
// Returns an array of users in the lottery db
function getLottoEntrants(guildId) {
const statement = ticketsDb.prepare("SELECT * FROM lottoTickets WHERE tickets > 0 AND guild = ?;");
const lottoUsers = statement.all(guildId);
console.log(`Retrieving ${lottoUsers.length} users from the lottery database.`);
return lottoUsers;
}
function countLottoEntrants(guildId) {
const lottoUsers = ticketsDb.prepare("COUNT DISTINCT user FROM lottoTickets WHERE tickets > 0 AND guild = ?;");
return lottoUsers.get(guildId);
}
ticketsDb.aggregate('addAll', {
start: 0,
step: (total, nextValue) => total + nextValue,
});
function totalLottoTickets(guildId) {
//const stmt = ticketsDb.prepare("SELECT SUM(tickets) FROM lottoTickets WHERE tickets > 0 and guild = ?;");
const stmt = ticketsDb.prepare("SELECT addALL(tickets) FROM lottoTickets WHERE tickets > 0 and guild = ?;");
return stmt.pluck().get(guildId);
}
function getTickets(userId, guildId) {
const tickets = ticketsDb.prepare("SELECT * FROM lottoTickets WHERE user = ? AND guild = ?;");
return tickets.get(userId, guildId);
}
function setTickets(newTickets) {
const tickets = ticketsDb.prepare("INSERT OR REPLACE INTO lottoTickets (id, user, guild, tickets) VALUES (@id, @user, @guild, @tickets);");
return tickets.run(newTickets);
}
function clearCurrentLottery(guildId) {
console.log(`Removed ${getLottoEntrants.length} users from the lottery database. Database is now cleared.`)
const lotto = ticketsDb.prepare("DELETE FROM lottoTickets WHERE guild = ?;")
return lotto.run(guildId)
}
module.exports = { getScore, setScore, getTickets, setTickets, getLottoEntrants, countLottoEntrants, clearCurrentLottery, totalLottoTickets };