@@ -150,12 +150,8 @@ protected function clearOptions(): void
150
150
151
151
/**
152
152
* Retrieve the parsed command options.
153
- *
154
- * @param string|null $key
155
- * @param mixed $default
156
- * @return mixed
157
153
*/
158
- protected function option ($ key = null , $ default = null )
154
+ protected function option (? string $ key = null , mixed $ default = null ): mixed
159
155
{
160
156
if (is_null ($ key ) || ! $ this ->getOptions ()) {
161
157
return $ this ->getOptions ();
@@ -164,12 +160,24 @@ protected function option($key = null, $default = null)
164
160
return Arr::get ($ this ->getOptions (), $ key , $ default );
165
161
}
166
162
163
+ /**
164
+ * Retrieve the option value.
165
+ */
166
+ public function value (?string $ option = null , mixed $ default = null ): mixed
167
+ {
168
+ $ options = $ this ->flattenOptions ($ this ->getOptions ());
169
+
170
+ if (is_null ($ option )) {
171
+ return $ options ;
172
+ }
173
+
174
+ return $ options [$ option ] ?? $ default ;
175
+ }
176
+
167
177
/**
168
178
* Set the command options.
169
- *
170
- * @return array
171
179
*/
172
- public function options ()
180
+ public function options (): array
173
181
{
174
182
return [];
175
183
}
@@ -210,10 +218,8 @@ public function getPermissions(): ?string
210
218
211
219
/**
212
220
* Retrieve the slash command options.
213
- *
214
- * @return array
215
221
*/
216
- public function getRegisteredOptions ()
222
+ public function getRegisteredOptions (): ? array
217
223
{
218
224
if ($ this ->registeredOptions ) {
219
225
return $ this ->registeredOptions ;
@@ -230,4 +236,31 @@ public function getRegisteredOptions()
230
236
: new DiscordOption ($ this ->discord (), $ option )
231
237
)->map (fn ($ option ) => $ option ->setName (Str::slug ($ option ->name )))->all ();
232
238
}
239
+
240
+ /**
241
+ * Flatten the options into dot notated keys.
242
+ */
243
+ protected function flattenOptions (array $ options , ?string $ parent = null ): array
244
+ {
245
+ return collect ($ options )->flatMap (function ($ option ) use ($ parent ) {
246
+ $ key = $ parent ? "{$ parent }. {$ option ['name ' ]}" : $ option ['name ' ];
247
+
248
+ if (is_array ($ option ) && isset ($ option ['options ' ])) {
249
+ $ options = $ this ->flattenOptions ($ option ['options ' ], $ key );
250
+
251
+ if (array_key_exists ('value ' , $ option )) {
252
+ return [
253
+ ...[$ key => $ option ['value ' ]],
254
+ ...$ options ,
255
+ ];
256
+ }
257
+
258
+ return $ options ;
259
+ }
260
+
261
+ return isset ($ option ['value ' ])
262
+ ? [$ key => $ option ['value ' ]]
263
+ : [];
264
+ })->all ();
265
+ }
233
266
}
0 commit comments