-
-
Notifications
You must be signed in to change notification settings - Fork 250
Expand file tree
/
Copy pathRegisteredCommand.php
More file actions
217 lines (189 loc) · 5.54 KB
/
Copy pathRegisteredCommand.php
File metadata and controls
217 lines (189 loc) · 5.54 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?php
declare(strict_types=1);
/*
* This file is a part of the DiscordPHP project.
*
* Copyright (c) 2015-present David Cole <david.cole1340@gmail.com>
*
* This file is subject to the MIT license that is bundled
* with this source code in the LICENSE.md file.
*/
namespace Discord\Helpers;
use Discord\Discord;
use Discord\Parts\Interactions\Interaction;
use Discord\Parts\Interactions\Request\Option;
/**
* RegisteredCommand represents a command that has been registered with the
* Discord servers and has a handler to handle when the command is triggered.
*
* https://discord.com/developers/docs/interactions/application-commands
*
* @since 7.0.0
*
* @author David Cole <david.cole1340@gmail.com>
*/
class RegisteredCommand
{
/**
* The Discord client.
*
* @var Discord Client.
*/
protected $discord;
/**
* The name of the command.
*
* @var string
*/
private $name;
/**
* The callback to be called when the command is triggered.
*
* @var callable
*/
private $callback;
/**
* The callback to be called when the auto complete is triggered.
*
* @var callable
*/
private $autocomplete_callback;
/**
* Array of sub-commands.
*
* @var RegisteredCommand[]
*/
private $subCommands;
/**
* RegisteredCommand represents a command that has been registered with the
* Discord servers and has a handler to handle when the command is triggered.
*
* @param Discord $discord
* @param string $name
* @param callable|null $callback
* @param callable|null $autocomplete_callback Callback returning list of auto complete suggestions
*/
public function __construct(Discord $discord, string $name, ?callable $callback = null, ?callable $autocomplete_callback = null)
{
$this->discord = $discord;
$this->name = $name;
$this->callback = $callback;
$this->autocomplete_callback = $autocomplete_callback;
}
/**
* Executes the command. Will search for a sub-command if given, otherwise
* executes the callback, if given.
*
* @param ExCollectionInterface<Option>|Option[] $options
* @param Interaction $interaction
*
* @return bool Whether the command successfully executed.
*/
public function execute($options, Interaction $interaction): bool
{
$params = Collection::for(Option::class, 'name');
foreach ($options as $option) {
if (isset($this->subCommands[$option->name])) {
if ($this->subCommands[$option->name]->execute($option->options ?? [], $interaction)) {
return true;
}
}
$params->pushItem($this->discord->getFactory()->part(Option::class, (array) $option, true));
}
if (isset($this->callback)) {
($this->callback)($interaction, $params);
return true;
}
return false;
}
/**
* Executes the command. Will search for a sub-command if given, otherwise
* executes the callback, if given.
*
* @param ApplicationCommand|ApplicationCommandAutocomplete $interaction
*
* @return bool Whether the command successfully executed.
*/
public function suggest(Interaction $interaction): bool
{
if (is_callable($this->autocomplete_callback)) {
$choice = ($this->autocomplete_callback)($interaction);
if (is_array($choice)) {
$interaction->autoCompleteResult($choice);
}
return true;
}
return false;
}
/**
* Sets the callback for the command.
*
* @param callable $callback
*/
public function setCallback(callable $callback): void
{
$this->callback = $callback;
}
/**
* Sets the callback for the auto complete suggestion.
*
* @param callable $autocomplete_callback
*/
public function setAutoCompleteCallback(callable $autocomplete_callback): void
{
$this->autocomplete_callback = $autocomplete_callback;
}
/**
* Tries to get a sub-command if exists.
*
* @param string $name
*
* @return RegisteredCommand|null
*/
public function getSubCommand(string $name): ?RegisteredCommand
{
return $this->subCommands[$name] ?? null;
}
/**
* Adds a sub-command to the command.
*
* @param string[]|string $names
* @param callable|null $callback
* @param callable|null $autocomplete_callback
*
* @throws \LogicException
*
* @return static
*/
public function addSubCommand(array|string $names, ?callable $callback = null, ?callable $autocomplete_callback = null): RegisteredCommand
{
if (is_string($names)) {
$names = [$names];
}
foreach($names as $subCommand){
if (isset($this->subCommands[$subCommand])) {
throw new \LogicException("The command `{$subCommand}` already exists.");
}
$this->subCommands[$subCommand] = new static($this->discord, $subCommand, $callback, $autocomplete_callback);
}
return $this;
}
/**
* Get command name.
*
* @return string
*/
public function getName(): string
{
return $this->name;
}
/**
* Get sub commands.
*
* @return RegisteredCommand[]|null
*/
public function getSubCommands(): ?array
{
return $this->subCommands;
}
}