You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Feb 18, 2023. It is now read-only.
-`Discord\Slash\RegisterClient` used for registering commands with Discord.
20
+
-`Discord\Slash\Client` used for listening for HTTP requests and responding.
21
+
22
+
### `Discord\Slash\RegisterClient`
23
+
24
+
You should read up on how commands are registered in the [Discord Developer Documentation](https://discord.com/developers/docs/interactions/slash-commands#registering-a-command), specifically the `options` array when creating and updating commands.
25
+
26
+
```php
27
+
<?php
28
+
29
+
include 'vendor/autoload.php';
30
+
31
+
use Discord\Slash\RegisterClient;
32
+
33
+
$client = new RegisterClient('your-bot-token-here');
34
+
35
+
/// GETTING COMMANDS
36
+
37
+
// gets a list of all GLOBAL comamnds (not guild-specific)
38
+
$commands = $client->getCommands();
39
+
// gets a list of all guild-specific commands to the given guild
Now that you have registered commands, you can set up an HTTP server to listen for requests from Discord.
70
+
71
+
There are a few ways to set up an HTTP server to listen for requests:
72
+
- The built-in ReactPHP HTTP server.
73
+
- Using an external HTTP server such as Apache or nginx (documentation to come).
74
+
- Using the built-in ReactPHP HTTP server without HTTPS and using Apache or nginx as a reverse proxy.
75
+
76
+
Whatever path you choose, the server **must** be protected with HTTPS - Discord will not accept regular HTTP.
77
+
78
+
At the moment for testing, I am running the built-in ReactPHP HTTP server on port `8080` with no HTTPS. I then have an Apache2 web server *with HTTPS* that acts as a reverse proxy to the ReactPHP server. An example of setting this up on Linux is below.
This library only handles slash commands, and there is no support for any other interactions with Discord such as creating channels, sending other messages etc. You can easily combine the DiscordPHP library with this library to have a much larger collection of tools. All you must do is ensure both clients share the same ReactPHP event loop. In the future, there will be more integration between the libraries. Here is an example:
126
+
127
+
```php
128
+
<?php
129
+
130
+
include 'vendor/autoload.php';
131
+
132
+
// make sure you have included DiscordPHP into your project - `composer require team-reflex/discord-php`
133
+
134
+
use Discord\Discord;
135
+
use Discord\Slash\Client;
136
+
use Discord\Slash\Parts\Interaction;
137
+
use Discord\Slash\Parts\Choices;
138
+
139
+
$discord = new Discord([
140
+
'token' => '##################',
141
+
]);
142
+
143
+
$client = new Client([
144
+
'public_key' => '???????????????',
145
+
'loop' => $discord->getLoop(),
146
+
]);
147
+
148
+
$discord->on('ready', function (Discord $discord) {
149
+
// DiscordPHP is ready
150
+
});
151
+
152
+
$client->registerCommand('my_cool_command', function (Interaction $interaction, Choices $choices) use ($discord) {
153
+
$discord->guilds->get('id', 'coolguild')->members->ban(); // do something ?
154
+
$interaction->acknowledge();
155
+
});
156
+
157
+
$discord->run();
158
+
```
159
+
160
+
### Setting up Apache2 as a reverse proxy
161
+
162
+
Assuming you already have Apache2 installed and the SSL certificates on your server:
163
+
164
+
1. Enable the required Apache mods:
165
+
```shell
166
+
$ sudo a2enmod proxy
167
+
$ sudo a2enmod proxy_http
168
+
$ sudo a2enmod ssl
169
+
```
170
+
2. Create a new site or modify the existing default site to listen on port `443`:
171
+
```sh
172
+
$ vim /etc/apache2/sites-available/000-default.conf # default site
173
+
174
+
# change contents to the following
175
+
<VirtualHost *:443># listen on 443
176
+
ProxyPreserveHost On # preserve the host header from Discord
177
+
ProxyPass / http://127.0.0.1:8080/ # pass-through to the HTTP server on port 8080
178
+
ProxyPassReverse / http://127.0.0.1:8080/
179
+
180
+
SSLEngine On # enable SSL
181
+
SSLCertificateFile /path/to/ssl/cert.crt # change to your cert path
182
+
SSLCertificateKeyFile /path/to/ssl/cert.key # change to your key path
183
+
</VirtualHost>
184
+
```
185
+
3. Restart apache - the code below works on Debian-based systems:
0 commit comments