1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Livia |
4
|
|
|
* Copyright 2017-2019 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\Types; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* {@inheritdoc} |
14
|
|
|
* @internal |
15
|
|
|
*/ |
16
|
|
|
class CommandOrGroupArgumentType extends ArgumentType { |
17
|
|
|
/** |
18
|
|
|
* @internal |
19
|
|
|
*/ |
20
|
|
|
function __construct(\CharlotteDunois\Livia\Client $client) { |
21
|
|
|
parent::__construct($client, 'command-or-group'); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* {@inheritdoc} |
26
|
|
|
* @return bool|string|\React\Promise\ExtendedPromiseInterface |
27
|
|
|
*/ |
28
|
|
|
function validate(string $value, \CharlotteDunois\Livia\Commands\Context $context, ?\CharlotteDunois\Livia\Arguments\Argument $arg = null) { |
29
|
|
|
$groups = $this->client->registry->findGroups($value); |
30
|
|
|
if(\count($groups) === 1) { |
31
|
|
|
return true; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
$commands = $this->client->registry->findCommands($value); |
35
|
|
|
if(\count($commands) === 1) { |
36
|
|
|
return true; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
if(\count($groups) === 0 && \count($commands) === 0) { |
40
|
|
|
return false; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$cmds = (!empty($commands) ? \CharlotteDunois\Livia\Utils\DataHelpers::disambiguation($commands, 'commands', 'name') : ''); |
44
|
|
|
$grps = (!empty($groups) ? \CharlotteDunois\Livia\Utils\DataHelpers::disambiguation($groups, 'groups', 'name') : ''); |
45
|
|
|
|
46
|
|
|
return $cmds.(!empty($cmds) && !empty($grps) ? \PHP_EOL : '').$grps.\PHP_EOL; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
* @return mixed|null|\React\Promise\ExtendedPromiseInterface |
52
|
|
|
*/ |
53
|
|
|
function parse(string $value, \CharlotteDunois\Livia\Commands\Context $context, ?\CharlotteDunois\Livia\Arguments\Argument $arg = null) { |
54
|
|
|
$groups = $this->client->registry->findGroups($value); |
55
|
|
|
if(\count($groups) > 0) { |
56
|
|
|
return $groups[0]; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$commands = $this->client->registry->findCommands($value); |
60
|
|
|
if(\count($commands) > 0) { |
61
|
|
|
return $commands[0]; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return null; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|