11<?php
22
3+ /*
4+ * This file is a part of the DiscordPHP-Slash project.
5+ *
6+ * Copyright (c) 2020-present David Cole <[email protected] > 7+ *
8+ * This source file is subject to the GNU General Public License v3.0
9+ * that is bundled with this source code in the LICENSE.md file.
10+ */
11+
312namespace Discord \Slash ;
413
514use Discord \Interaction as DiscordInteraction ;
3342 */
3443class Client
3544{
36- const API_BASE_URI = " https://discord.com/api/v8/ " ;
45+ const API_BASE_URI = ' https://discord.com/api/v8/ ' ;
3746
3847 /**
3948 * Array of options for the client.
@@ -59,13 +68,13 @@ class Client
5968 /**
6069 * An array of registered commands.
6170 *
62- * @var RegisteredCommand[]
71+ * @var RegisteredCommand[]
6372 */
6473 private $ commands ;
6574
6675 /**
6776 * Logger for client.
68- *
77+ *
6978 * @var LoggerInterface
7079 */
7180 private $ logger ;
@@ -150,6 +159,7 @@ public function handleRequest(ServerRequestInterface $request)
150159
151160 return $ this ->handleInteraction ($ interaction )->then (function ($ result ) {
152161 $ this ->logger ->info ('responding to interaction ' , $ result );
162+
153163 return new Response (200 , [], json_encode ($ result ));
154164 });
155165 }
@@ -158,19 +168,20 @@ public function handleRequest(ServerRequestInterface $request)
158168 * Handles an interaction from Discord.
159169 *
160170 * @param Interaction $interaction
161- *
171+ *
162172 * @return ExtendedPromiseInterface
163173 */
164174 private function handleInteraction (Interaction $ interaction ): ExtendedPromiseInterface
165175 {
166- return new Promise (function ($ resolve , $ reject ) use ($ interaction ) {
176+ return new Promise (function ($ resolve , $ reject ) use ($ interaction ) {
167177 switch ($ interaction ->type ) {
168178 case InteractionType::PING :
169179 return $ resolve ([
170180 'type ' => InteractionResponseType::PONG ,
171181 ]);
172182 case InteractionType::APPLICATION_COMMAND :
173183 $ interaction ->setResolve ($ resolve );
184+
174185 return $ this ->handleApplicationCommand ($ interaction );
175186 }
176187 });
@@ -185,11 +196,15 @@ private function handleApplicationCommand(Interaction $interaction): void
185196 {
186197 $ checkCommand = function ($ command ) use ($ interaction , &$ checkCommand ) {
187198 if (isset ($ this ->commands [$ command ['name ' ]])) {
188- if ($ this ->commands [$ command ['name ' ]]->execute ($ command ['options ' ] ?? [], $ interaction )) return true ;
199+ if ($ this ->commands [$ command ['name ' ]]->execute ($ command ['options ' ] ?? [], $ interaction )) {
200+ return true ;
201+ }
189202 }
190203
191204 foreach ($ command ['options ' ] ?? [] as $ option ) {
192- if ($ checkCommand ($ option )) return true ;
205+ if ($ checkCommand ($ option )) {
206+ return true ;
207+ }
193208 }
194209 };
195210
@@ -200,17 +215,21 @@ private function handleApplicationCommand(Interaction $interaction): void
200215 * Registeres a command with the client.
201216 *
202217 * @param string|array $name
203- * @param callable $callback
204- *
218+ * @param callable $callback
219+ *
205220 * @return RegisteredCommand
206221 */
207- public function registerCommand ($ name , callable $ callback = null ): RegisteredCommand
222+ public function registerCommand ($ name , callable $ callback = null ): RegisteredCommand
208223 {
209- if (is_array ($ name ) && count ($ name ) == 1 ) $ name = array_shift ($ name );
224+ if (is_array ($ name ) && count ($ name ) == 1 ) {
225+ $ name = array_shift ($ name );
226+ }
210227
211228 // registering base command
212229 if (! is_array ($ name ) || count ($ name ) == 1 ) {
213- if (isset ($ this ->commands [$ name ])) throw new InvalidArgumentException ("The command ` {$ name }` already exists. " );
230+ if (isset ($ this ->commands [$ name ])) {
231+ throw new InvalidArgumentException ("The command ` {$ name }` already exists. " );
232+ }
214233
215234 return $ this ->commands [$ name ] = new RegisteredCommand ($ name , $ callback );
216235 }
0 commit comments