This repository has been archived by the owner on Nov 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
db.js
97 lines (87 loc) · 2.08 KB
/
db.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
const Sequelize = require('sequelize');
const DB = (() => {
const sequelize = new Sequelize('yukariopts', 'yukari', 'yukari', {
host: 'localhost',
dialect: 'sqlite',
logging: false,
storage: 'db.sqlite',
});
const GuildOpts = sequelize.define('tags', {
guild: {
type: Sequelize.STRING,
unique: true,
},
use_nhk: Sequelize.BOOLEAN,
use_nied: Sequelize.BOOLEAN,
use_tsunami: Sequelize.BOOLEAN,
min_shindo: Sequelize.STRING,
ignore_unknown: Sequelize.BOOLEAN,
use_text: Sequelize.BOOLEAN,
alert_room: Sequelize.STRING,
use_voice: Sequelize.BOOLEAN,
alert_voice: Sequelize.STRING,
admin_only: {
type: Sequelize.BOOLEAN,
defaultValue: true,
},
mention: {
type: Sequelize.STRING,
defaultValue: 'nobody',
},
});
// publics
return {
init: function() {
GuildOpts.sync();
},
get: async function(guild) {
const data = await GuildOpts.findOne({ where: { guild } });
if (data) {
return data;
}
// Guild wasn't yet indexed, so we need to make a new one
const newdata = await GuildOpts.create({
guild,
use_nhk: true,
use_nied: true,
use_tsunami: true,
min_shindo: '3',
ignore_unknown: false,
use_text: true,
alert_room: 'auto',
use_voice: true,
alert_voice: 'auto',
admin_only: true,
mention: 'nobody',
});
return newdata;
},
set: async function(guild, key, value) {
const updator = {};
updator[key] = value;
const affectedRows = await GuildOpts.update(updator, { where: { guild } });
if (affectedRows > 0) {
return;
}
// Guild wasn't yet indexed, so we need to make a new one
const creator = {
guild,
use_nhk: true,
use_nied: true,
use_tsunami: true,
min_shindo: '3',
ignore_unknown: false,
use_text: true,
alert_room: 'auto',
use_voice: true,
alert_voice: 'auto',
admin_only: true,
mention: 'nobody',
};
creator[key] = value;
await GuildOpts.create(creator);
return;
},
};
})();
module.exports = DB;