-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.js
46 lines (44 loc) · 1.81 KB
/
api.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
const initSqlJs = require('sql.js')
const fs = require('fs')
const { Buffer } = require('buffer')
const makeApi = async path => {
const SQL = await initSqlJs()
const db = fs.existsSync(path)
? new SQL.Database(fs.readFileSync(path))
: new SQL.Database().run('CREATE TABLE roles (guildid TEXT, roleid TEXT, userid TEXT, UNIQUE(roleid, userid))')
return {
commit: path => {
const data = db.export()
const buffer = Buffer.from(data)
fs.writeFileSync(path, buffer)
},
addRoles: (guildid, roleid, userid) => db.run('INSERT INTO roles VALUES(?, ?, ?)', [guildid, roleid, userid]),
listUsers: (guildid, roleid) => {
try {
return db.exec('SELECT userid FROM roles WHERE guildid = ? AND roleid = ?', [guildid, roleid])[0].values.flat()
} catch (err) {
return []
}
},
removeUserFromRole: (guildid, roleid, userid) => db.run('DELETE FROM roles WHERE guildid = ? AND roleid = ? AND userid = ?', [guildid, roleid, userid]),
listRoles: (guildid, userid) => {
try {
return db.exec('SELECT roleid FROM roles WHERE guildid = ? AND userid = ?', [guildid, userid])[0].values.flat()
} catch (err) {
return []
}
},
listAllRoles: guildid => {
try {
return db.exec('SELECT roleid FROM roles WHERE guildid = ?', [guildid])[0].values.flat()
} catch (err) {
return []
}
},
removeRole: (guildid, roleid) => db.run('DELETE FROM roles WHERE guildid = ? AND roleid = ?', [guildid, roleid]),
removeGuild: guildid => db.run('DELETE FROM roles WHERE guildid = ?', [guildid])
}
}
module.exports = {
makeApi
}