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

Commit 18a2dfe

Browse files
committed
Initial commit
0 parents  commit 18a2dfe

File tree

4 files changed

+184
-0
lines changed

4 files changed

+184
-0
lines changed

.editorconfig

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
end_of_line = lf
6+
insert_final_newline = true
7+
indent_style = space
8+
indent_size = 4
9+
trim_trailing_whitespace = true
10+
11+
[*.yml]
12+
indent_size = 2
13+
14+
[*.js]
15+
indent_size = 2

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
vendor
2+
composer.lock
3+
test.php

composer.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "discordphp/slash",
3+
"description": "HTTP server for Discord slash commands",
4+
"type": "library",
5+
"license": "GPL-3.0-or-later",
6+
"authors": [
7+
{
8+
"name": "David Cole",
9+
"email": "[email protected]"
10+
}
11+
],
12+
"require": {
13+
"react/http": "^1.2",
14+
"symfony/options-resolver": "^5.2",
15+
"monolog/monolog": "^2.2",
16+
"guzzlehttp/guzzle": "^7.2"
17+
},
18+
"autoload": {
19+
"psr-4": {
20+
"Discord\\Slash\\": "src/Discord"
21+
}
22+
}
23+
}

src/Discord/Client.php

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
<?php
2+
3+
namespace Discord\SlashCommands;
4+
5+
use Monolog\Handler\StreamHandler;
6+
use Monolog\Logger;
7+
use Psr\Http\Message\ServerRequestInterface;
8+
use React\EventLoop\Factory;
9+
use React\EventLoop\LoopInterface;
10+
use GuzzleHttp\Client as HttpClient;
11+
use React\Http\Server as HttpServer;
12+
use React\Promise\Deferred;
13+
use React\Socket\Server as SocketServer;
14+
use Symfony\Component\OptionsResolver\OptionsResolver;
15+
16+
class Client
17+
{
18+
const API_BASE_URI = "https://discord.com/api/v8/";
19+
20+
/**
21+
* Array of options for the client.
22+
*
23+
* @var array
24+
*/
25+
private $options;
26+
27+
/**
28+
* HTTP server.
29+
*
30+
* @var HttpServer
31+
*/
32+
private $server;
33+
34+
/**
35+
* HTTP client.
36+
*
37+
* @var Http
38+
*/
39+
private $http;
40+
41+
/**
42+
* Socket listening for connections.
43+
*
44+
* @var SocketServer
45+
*/
46+
private $socket;
47+
48+
public function __construct(array $options = [])
49+
{
50+
$this->options = $this->resolveOptions($options);
51+
$this->registerServer();
52+
}
53+
54+
/**
55+
* Resolves the options for the client.
56+
*
57+
* @param array $options
58+
*
59+
* @return array
60+
*/
61+
private function resolveOptions(array $options): array
62+
{
63+
$resolver = new OptionsResolver();
64+
65+
$resolver
66+
->setDefined([
67+
'uri',
68+
'logger',
69+
'loop',
70+
'token',
71+
])
72+
->setDefaults([
73+
'uri' => '0.0.0.0:80',
74+
'loop' => Factory::create(),
75+
]);
76+
77+
$options = $resolver->resolve($options);
78+
79+
if (! isset($options['logger'])) {
80+
$options['logger'] = (new Logger('DiscordPHP/Slash'))->pushHandler(new StreamHandler('php://stdout'));
81+
}
82+
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+
95+
return $options;
96+
}
97+
98+
/**
99+
* Sets up the ReactPHP HTTP server.
100+
*/
101+
private function registerServer()
102+
{
103+
$this->server = new HttpServer($this->getLoop(), [$this, 'handleRequest']);
104+
105+
$this->socket = new SocketServer($this->options['uri'], $this->getLoop());
106+
$this->server->listen($this->socket);
107+
}
108+
109+
public function handleRequest(ServerRequestInterface $request)
110+
{
111+
$deferred = new Deferred();
112+
113+
return $deferred->promise();
114+
}
115+
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+
126+
/**
127+
* Starts the ReactPHP event loop.
128+
*/
129+
public function run()
130+
{
131+
$this->getLoop()->run();
132+
}
133+
134+
/**
135+
* Gets the ReactPHP event loop.
136+
*
137+
* @return LoopInterface
138+
*/
139+
public function getLoop(): LoopInterface
140+
{
141+
return $this->options['loop'];
142+
}
143+
}

0 commit comments

Comments
 (0)