-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
177 lines (152 loc) · 4.42 KB
/
index.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import { Plugin, command, permissions } from 'munar-core'
import Ultron from 'ultron'
const debug = require('debug')('munar:config')
const configCommands = [ 'set', 'get', 'add', 'remove' ]
class PluginConfigModel {
static collection = 'plugin_config'
static timestamps = true
static schema = {
_id: { type: String, index: true, unique: true },
enabled: { type: Boolean, default: false },
options: {
default: {}
}
}
}
export default class Config extends Plugin {
static description = 'Keeps plugin configuration.'
events = []
constructor (bot, options) {
super(bot, options)
this.models({
PluginConfig: PluginConfigModel
})
}
enable () {
const events = new Ultron(this.bot.plugins)
events.on('discovered', this.onPluginDiscovered)
events.on('load', this.onPluginLoad)
this.events.push(events)
}
disable () {
this.events.forEach((events) => events.remove())
this.events = []
}
set (message, ns, option, value) {
const plugin = this.bot.getPlugin(ns)
if (/^[0-9]+$/.test(value)) {
value = parseInt(value, 10)
}
if (/^true|false$/.test(value)) {
value = value === 'true'
}
debug('value', typeof value, value)
plugin.setOption(option, value)
this.save(ns, plugin.getOptions())
message.reply(`"${ns}.${option}" set to ${value}`)
}
get (message, ns, option) {
let plugin = this.bot.getPlugin(ns)
if (option) {
message.reply(`"${ns}.${option}": ${plugin.getOption(option)}`)
} else {
const options = plugin.getOptions()
debug('all options', options)
for (const option of Object.keys(options)) {
message.reply(`${ns}.${option}: ${options[option]}`)
}
}
}
add (message, ns, option, ...values) {
const plugin = this.bot.getPlugin(ns)
let arr = plugin.getOption(option)
if (arr == null) {
arr = values
} else if (Array.isArray(arr)) {
arr = arr.concat(values)
} else {
message.reply(`"${ns}.${option}" is not a list.`)
return
}
plugin.setOption(option, arr)
this.save(ns, plugin.getOptions())
message.reply(`added values to "${ns}.${option}".`)
}
remove (message, ns, option, ...values) {
const plugin = this.bot.getPlugin(ns)
let arr = plugin.getOption(option)
if (Array.isArray(arr)) {
arr = arr.filter((val) => values.indexOf(val) === -1)
} else {
message.reply(`"${ns}.${option}" is not a list.`)
return
}
plugin.setOption(option, arr)
this.save(ns, plugin.getOptions())
message.reply(`removed values from "${ns}.${option}".`)
}
@command('config', 'cf', { role: permissions.MANAGER })
config (message, command, ns, ...args) {
if (configCommands.indexOf(command) === -1) {
message.reply(`"${command}" is not a command.`)
return
}
if (!ns) {
message.reply('You should provide a plugin to configure.')
}
const plugin = this.bot.getPlugin(ns)
if (!plugin) {
message.reply(`Could not find plugin "${ns}".`)
return
}
this[command](message, ns, ...args)
}
onPluginDiscovered = async (name) => {
debug('load config', name)
const { options, enabled } = await this.load(name)
await this.bot.plugins.load(name, {
...options.toJSON(),
enable: enabled
})
}
onPluginLoad = (instance, name) => {
const onChange = () => this.onChange(name, instance)
this.events.push(
new Ultron(instance)
.on('option', onChange)
.on('enable', onChange)
.on('disable', onChange)
)
debug('attached load handlers', name)
}
async onChange (name, instance) {
const PluginConfig = this.model('PluginConfig')
const enabled = instance.enabled()
const options = instance.getOptions()
debug('save', name, enabled ? 'enabled' : 'disabled', options)
await PluginConfig.update(
{ _id: name },
{
_id: name,
enabled,
options
},
{ upsert: true }
)
}
async load (name) {
const PluginConfig = this.model('PluginConfig')
const config = await PluginConfig.findById(name)
const options = config && config.options || {}
const enabled = Boolean(config && config.enabled)
return { options, enabled }
}
async save (name, options) {
const PluginConfig = this.model('PluginConfig')
await PluginConfig.findByIdAndUpdate(
name,
{ $set: { options } },
{ upsert: true }
)
}
}