Skip to content
This repository was archived by the owner on Feb 18, 2023. It is now read-only.

Commit 5af4ad6

Browse files
committed
Added ability to create, edit and delete commands, moved HTTP client into seperate class
1 parent 7e0398c commit 5af4ad6

File tree

6 files changed

+380
-32
lines changed

6 files changed

+380
-32
lines changed

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,8 @@
1919
"psr-4": {
2020
"Discord\\Slash\\": "src/Discord"
2121
}
22+
},
23+
"require-dev": {
24+
"symfony/var-dumper": "^5.2"
2225
}
2326
}

src/Discord/Client.php

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
<?php
22

3-
namespace Discord\SlashCommands;
3+
namespace Discord\Slash;
44

55
use Monolog\Handler\StreamHandler;
66
use Monolog\Logger;
77
use Psr\Http\Message\ServerRequestInterface;
88
use React\EventLoop\Factory;
99
use React\EventLoop\LoopInterface;
10-
use GuzzleHttp\Client as HttpClient;
1110
use React\Http\Server as HttpServer;
1211
use React\Promise\Deferred;
1312
use React\Socket\Server as SocketServer;
1413
use Symfony\Component\OptionsResolver\OptionsResolver;
1514

15+
/**
16+
* The Client class acts as an HTTP web server to handle requests from Discord when a command
17+
* is triggered. The class can also be used as a request handler by mocking a ServerRequestInterface
18+
* to allow it to be used with another webserver such as Apache or nginx.
19+
*
20+
* @author David Cole <[email protected]>
21+
*/
1622
class Client
1723
{
1824
const API_BASE_URI = "https://discord.com/api/v8/";
@@ -31,13 +37,6 @@ class Client
3137
*/
3238
private $server;
3339

34-
/**
35-
* HTTP client.
36-
*
37-
* @var Http
38-
*/
39-
private $http;
40-
4140
/**
4241
* Socket listening for connections.
4342
*
@@ -67,7 +66,6 @@ private function resolveOptions(array $options): array
6766
'uri',
6867
'logger',
6968
'loop',
70-
'token',
7169
])
7270
->setDefaults([
7371
'uri' => '0.0.0.0:80',
@@ -80,18 +78,6 @@ private function resolveOptions(array $options): array
8078
$options['logger'] = (new Logger('DiscordPHP/Slash'))->pushHandler(new StreamHandler('php://stdout'));
8179
}
8280

83-
if (! isset($options['token'])) {
84-
$options['logger']->warning('no token given - unable to register commands');
85-
} else {
86-
$this->http = new HttpClient([
87-
'base_uri' => self::API_BASE_URI,
88-
'headers' => [
89-
'User-Agent' => $this->getUserAgent(),
90-
'Authorization' => 'Bot '.$options['token'],
91-
],
92-
]);
93-
}
94-
9581
return $options;
9682
}
9783

@@ -113,16 +99,6 @@ public function handleRequest(ServerRequestInterface $request)
11399
return $deferred->promise();
114100
}
115101

116-
/**
117-
* Returns the User-Agent of the application.
118-
*
119-
* @return string
120-
*/
121-
private function getUserAgent()
122-
{
123-
return "DiscordBot (https://github.com/davidcole1340/DiscordPHP-Slash, v0.0.1)";
124-
}
125-
126102
/**
127103
* Starts the ReactPHP event loop.
128104
*/
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Discord\Slash\Enums;
4+
5+
/**
6+
* @link https://discord.com/developers/docs/interactions/slash-commands#applicationcommandoptiontype
7+
* @author David Cole <[email protected]>
8+
*/
9+
final class ApplicationCommandOptionType
10+
{
11+
public const SUB_COMMAND = 1;
12+
public const SUB_COMMAND_GROUP = 2;
13+
public const STRING = 3;
14+
public const INTEGER = 4;
15+
public const BOOLEAN = 5;
16+
public const USER = 6;
17+
public const CHANNEL = 7;
18+
public const ROLE = 8;
19+
}

src/Discord/Parts/Command.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Discord\Slash\Parts;
4+
5+
class Command extends Part
6+
{
7+
8+
}

src/Discord/Parts/Part.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace Discord\Slash\Parts;
4+
5+
class Part
6+
{
7+
/**
8+
* Array of attributes.
9+
*
10+
* @var array
11+
*/
12+
protected $attributes;
13+
14+
/**
15+
* Part constructor.
16+
*
17+
* @param array $attributes
18+
*/
19+
public function __construct($attributes = [])
20+
{
21+
$this->attributes = (array) $attributes;
22+
}
23+
24+
/**
25+
* Returns the parts attributes.
26+
*
27+
* @return array
28+
*/
29+
public function getAttributes(): array
30+
{
31+
return $this->attributes;
32+
}
33+
34+
/**
35+
* Gets an attribute from the part.
36+
*
37+
* @param string $key
38+
*/
39+
public function __get(string $key)
40+
{
41+
return $this->attributes[$key] ?? null;
42+
}
43+
44+
/**
45+
* Sets an attribute in the part.
46+
*
47+
* @param string $key
48+
* @param mixed $value
49+
*/
50+
public function __set(string $key, $value)
51+
{
52+
$this->attributes[$key] = $value;
53+
}
54+
}

0 commit comments

Comments
 (0)