1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Livia |
4
|
|
|
* Copyright 2017-2018 Charlotte Dunois, All Rights Reserved |
5
|
|
|
* |
6
|
|
|
* Website: https://charuru.moe |
7
|
|
|
* License: https://github.com/CharlotteDunois/Livia/blob/master/LICENSE |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace CharlotteDunois\Livia\Providers; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Loads and stores settings associated with guilds. |
14
|
|
|
* Classes extending this class must assign the client received in the `init` method to the `client` property. |
15
|
|
|
* |
16
|
|
|
* @property \CharlotteDunois\Livia\LiviaClient $client The client this provider is for. This property is NOT accessible outside of the class and is only for documentation purpose here (for extending the class). |
17
|
|
|
*/ |
18
|
|
|
abstract class SettingProvider { |
19
|
|
|
/** |
20
|
|
|
* The Provider state, idling waiting to have a connection. |
21
|
|
|
* @var int |
22
|
|
|
* @source |
23
|
|
|
*/ |
24
|
|
|
const STATE_IDLE = 0; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The Provider state, ready to get work done. |
28
|
|
|
* @var int |
29
|
|
|
* @source |
30
|
|
|
*/ |
31
|
|
|
const STATE_READY = 1; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* The current provider state. |
35
|
|
|
* Implementations must set this state accordingly. |
36
|
|
|
* @var int |
37
|
|
|
*/ |
38
|
|
|
protected $providerState = 0; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* The client this provider is for. |
42
|
|
|
* @var \CharlotteDunois\Livia\LiviaClient |
43
|
|
|
*/ |
44
|
|
|
protected $client; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* An array of guilds getting set up. If in the array, events doing further setup should ignore the event. |
48
|
|
|
* @property array |
49
|
|
|
*/ |
50
|
|
|
protected $setup = array(); |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Returns the provider state. |
54
|
|
|
* @return int |
55
|
|
|
*/ |
56
|
|
|
function getState() { |
57
|
|
|
return $this->providerState; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Initializes the provider by connecting to databases and/or caching all data in memory. LiviaClient::setProvider will automatically call this once the client is ready. |
62
|
|
|
* @param \CharlotteDunois\Livia\LiviaClient $client |
63
|
|
|
* @return \React\Promise\ExtendedPromiseInterface |
64
|
|
|
*/ |
65
|
|
|
abstract function init(\CharlotteDunois\Livia\LiviaClient $client): \React\Promise\ExtendedPromiseInterface; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Destroys the provider, removing any event listeners. |
69
|
|
|
* @return mixed|void |
70
|
|
|
*/ |
71
|
|
|
abstract function destroy(); |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Creates a new table row in the db for the guild, if it doesn't exist already - otherwise loads the row. |
75
|
|
|
* @param string|\CharlotteDunois\Yasmin\Models\Guild $guild |
76
|
|
|
* @param array|\ArrayObject $settings |
77
|
|
|
* @return \React\Promise\ExtendedPromiseInterface |
78
|
|
|
* @throws \InvalidArgumentException |
79
|
|
|
*/ |
80
|
|
|
abstract function create($guild, &$settings = array()): \React\Promise\ExtendedPromiseInterface; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Gets a setting from a guild. |
84
|
|
|
* @param string|\CharlotteDunois\Yasmin\Models\Guild $guild |
85
|
|
|
* @param string $key |
86
|
|
|
* @param mixed $defaultValue |
87
|
|
|
* @return mixed |
88
|
|
|
* @throws \BadMethodCallException |
89
|
|
|
* @throws \InvalidArgumentException |
90
|
|
|
*/ |
91
|
|
|
abstract function get($guild, string $key, $defaultValue = null); |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Sets a setting for a guild. |
95
|
|
|
* @param string|\CharlotteDunois\Yasmin\Models\Guild $guild |
96
|
|
|
* @param string $key |
97
|
|
|
* @param mixed $value |
98
|
|
|
* @return mixed |
99
|
|
|
* @throws \BadMethodCallException |
100
|
|
|
* @throws \InvalidArgumentException |
101
|
|
|
*/ |
102
|
|
|
abstract function set($guild, string $key, $value); |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Removes a setting from a guild. |
106
|
|
|
* @param string|\CharlotteDunois\Yasmin\Models\Guild $guild |
107
|
|
|
* @param string $key |
108
|
|
|
* @return mixed |
109
|
|
|
* @throws \BadMethodCallException |
110
|
|
|
* @throws \InvalidArgumentException |
111
|
|
|
*/ |
112
|
|
|
abstract function remove($guild, string $key); |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Removes all settings in a guild. |
116
|
|
|
* @param string|\CharlotteDunois\Yasmin\Models\Guild $guild |
117
|
|
|
* @return mixed |
118
|
|
|
* @throws \InvalidArgumentException |
119
|
|
|
*/ |
120
|
|
|
abstract function clear($guild); |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Obtains the ID of the provided guild. |
124
|
|
|
* @param \CharlotteDunois\Yasmin\Models\Guild|string|int|null $guild |
125
|
|
|
* @return string |
126
|
|
|
* @throws \InvalidArgumentException |
127
|
|
|
*/ |
128
|
|
|
function getGuildID($guild) { |
129
|
|
|
if($guild === null || $guild === 'global') { |
130
|
|
|
return 'global'; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return $this->client->guilds->resolve($guild)->id; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* This method will attach all necessary event listeners to the client. |
138
|
|
|
* Providers extending this class must call this method when initializing the provider (in the `init` method). |
139
|
|
|
* @return void |
140
|
|
|
* @throws \BadMethodCallException |
141
|
|
|
*/ |
142
|
|
|
function attachListeners() { |
143
|
|
|
if(!($this->client instanceof \CharlotteDunois\Livia\LiviaClient)) { |
144
|
|
|
throw new \BadMethodCallException('The client property is not set or not a valid instance of LiviaClient'); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
$this->client->on('commandPrefixChange', array($this, 'callbackCommandPrefixChange')); |
148
|
|
|
$this->client->on('commandStatusChange', array($this, 'callbackCommandStatusChange')); |
149
|
|
|
$this->client->on('groupStatusChange', array($this, 'callbackGroupStatusChange')); |
150
|
|
|
$this->client->on('guildCreate', array($this, 'callbackGuildCreate')); |
151
|
|
|
$this->client->on('commandRegister', array($this, 'callbackCommandRegister')); |
152
|
|
|
$this->client->on('commandReregister', array($this, 'callbackCommandRegister')); |
153
|
|
|
$this->client->on('groupRegister', array($this, 'callbackGroupRegister')); |
154
|
|
|
$this->client->on('groupReregister', array($this, 'callbackGroupRegister')); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* This method will remove the attached event listeners from the client. |
159
|
|
|
* Providers extending this class must call this method when destroying the provider (in the `destroy` method). |
160
|
|
|
* @return void |
161
|
|
|
* @throws \BadMethodCallException |
162
|
|
|
*/ |
163
|
|
|
function removeListeners() { |
164
|
|
|
if(!($this->client instanceof \CharlotteDunois\Livia\LiviaClient)) { |
165
|
|
|
throw new \BadMethodCallException('The client property is not set or not a valid instance of LiviaClient'); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
$this->client->removeListener('commandPrefixChange', array($this, 'callbackCommandPrefixChange')); |
169
|
|
|
$this->client->removeListener('commandStatusChange', array($this, 'callbackCommandStatusChange')); |
170
|
|
|
$this->client->removeListener('groupStatusChange', array($this, 'callbackGroupStatusChange')); |
171
|
|
|
$this->client->removeListener('guildCreate', array($this, 'callbackGuildCreate')); |
172
|
|
|
$this->client->removeListener('commandRegister', array($this, 'callbackCommandRegister')); |
173
|
|
|
$this->client->removeListener('commandReregister', array($this, 'callbackCommandRegister')); |
174
|
|
|
$this->client->removeListener('groupRegister', array($this, 'callbackGroupRegister')); |
175
|
|
|
$this->client->removeListener('groupReregister', array($this, 'callbackGroupRegister')); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Loads all settings for a guild. Used in listener callbacks. |
180
|
|
|
* @param string|\CharlotteDunois\Yasmin\Models\Guild $guild |
181
|
|
|
* @return void |
182
|
|
|
*/ |
183
|
|
|
function setupGuild($guild) { |
184
|
|
|
$guild = $this->getGuildID($guild); |
185
|
|
|
|
186
|
|
|
$settings = $this->settings->get($guild); |
187
|
|
|
if(!$settings) { |
188
|
|
|
$this->create($guild)->done(null, array($this->client, 'handlePromiseRejection')); |
189
|
|
|
return; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
$this->setup[$guild] = true; |
193
|
|
|
|
194
|
|
|
if($guild === 'global' && \array_key_exists('commandPrefix', $settings)) { |
195
|
|
|
$this->client->setCommandPrefix($settings['commandPrefix'], true); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
foreach($this->client->registry->commands as $command) { |
199
|
|
|
$this->setupGuildCommand($guild, $command, $settings); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
foreach($this->client->registry->groups as $group) { |
203
|
|
|
$this->setupGuildGroup($guild, $group, $settings); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
unset($this->setup[$guild]); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Sets up a command's status in a guild from the guild's settings. Used in listener callbacks. |
211
|
|
|
* @param string|\CharlotteDunois\Yasmin\Models\Guild $guild |
212
|
|
|
* @param \CharlotteDunois\Livia\Commands\Command $command |
213
|
|
|
* @param array|\ArrayObject $settings |
214
|
|
|
* @return void |
215
|
|
|
*/ |
216
|
|
|
function setupGuildCommand($guild, \CharlotteDunois\Livia\Commands\Command $command, &$settings) { |
217
|
|
|
if(!isset($settings['command-'.$command->name])) { |
218
|
|
|
return; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
$command->setEnabledIn(($guild !== 'global' ? $guild : null), $settings['command-'.$command->name]); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Sets up a group's status in a guild from the guild's settings. Used in listener callbacks. |
226
|
|
|
* @param string|\CharlotteDunois\Yasmin\Models\Guild $guild |
227
|
|
|
* @param \CharlotteDunois\Livia\Commands\CommandGroup $group |
228
|
|
|
* @param array|\ArrayObject $settings |
229
|
|
|
* @return void |
230
|
|
|
*/ |
231
|
|
|
function setupGuildGroup($guild, \CharlotteDunois\Livia\Commands\CommandGroup $group, &$settings) { |
232
|
|
|
if(!isset($settings['group-'.$group->id])) { |
233
|
|
|
return; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
$group->setEnabledIn(($guild !== 'global' ? $guild : null), $settings['group-'.$group->id]); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* The callback for the command prefix change event. |
241
|
|
|
* @param \CharlotteDunois\Yasmin\Models\Guild|null $guild |
242
|
|
|
* @param string|null $prefix |
243
|
|
|
* @return void |
244
|
|
|
*/ |
245
|
|
|
function callbackCommandPrefixChange(?\CharlotteDunois\Yasmin\Models\Guild $guild, ?string $prefix) { |
246
|
|
|
if(!empty($this->setup[$this->getGuildID($guild)])) { |
247
|
|
|
return; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
$this->set($guild, 'commandPrefix', $prefix); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* The callback for the command status change event. |
255
|
|
|
* @param \CharlotteDunois\Yasmin\Models\Guild|null $guild |
256
|
|
|
* @param string|null $prefix |
257
|
|
|
* @param bool $enabled |
258
|
|
|
* @return void |
259
|
|
|
*/ |
260
|
|
|
function callbackCommandStatusChange(?\CharlotteDunois\Yasmin\Models\Guild $guild, \CharlotteDunois\Livia\Commands\Command $command, bool $enabled) { |
261
|
|
|
if(!empty($this->setup[$this->getGuildID($guild)])) { |
262
|
|
|
return; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
$this->set($guild, 'command-'.$command->name, $enabled); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* The callback for the group status change event. |
270
|
|
|
* @param \CharlotteDunois\Yasmin\Models\Guild|null $guild |
271
|
|
|
* @param string|null $prefix |
272
|
|
|
* @param bool $enabled |
273
|
|
|
* @return void |
274
|
|
|
*/ |
275
|
|
|
function callbackGroupStatusChange(?\CharlotteDunois\Yasmin\Models\Guild $guild, \CharlotteDunois\Livia\Commands\CommandGroup $group, bool $enabled) { |
276
|
|
|
if(!empty($this->setup[$this->getGuildID($guild)])) { |
277
|
|
|
return; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
$this->set($guild, 'group-'.$group->id, $enabled); |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* The callback for the guild create event. |
285
|
|
|
* @param \CharlotteDunois\Yasmin\Models\Guild $guild |
286
|
|
|
* @return void |
287
|
|
|
*/ |
288
|
|
|
function callbackGuildCreate(\CharlotteDunois\Yasmin\Models\Guild $guild) { |
289
|
|
|
$this->setupGuild($guild); |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* The callback for the command register and reregister event. |
294
|
|
|
* @param \CharlotteDunois\Livia\Commands\Command $copmmand |
295
|
|
|
* @return void |
296
|
|
|
*/ |
297
|
|
|
function callbackCommandRegister(\CharlotteDunois\Livia\Commands\Command $command) { |
298
|
|
|
foreach($this->settings as $guild => $settings) { |
299
|
|
|
if($guild !== 'global' && $this->client->guilds->has($guild) === false) { |
300
|
|
|
continue; |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
$this->setupGuildCommand($guild, $command, $settings); |
304
|
|
|
} |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
* The callback for the group register and reregister event. |
309
|
|
|
* @param \CharlotteDunois\Livia\Commands\CommandGroup $group |
310
|
|
|
* @return void |
311
|
|
|
*/ |
312
|
|
|
function callbackGroupRegister(\CharlotteDunois\Livia\Commands\CommandGroup $group) { |
313
|
|
|
foreach($this->settings as $guild => $settings) { |
314
|
|
|
if($guild !== 'global' && $this->client->guilds->has($guild) === false) { |
315
|
|
|
continue; |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
$this->setupGuildGroup($guild, $group, $settings); |
319
|
|
|
} |
320
|
|
|
} |
321
|
|
|
} |
322
|
|
|
|