Download the PHP package discord-php/slash without Composer
On this page you can find all versions of the php package discord-php/slash. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download discord-php/slash
More information about discord-php/slash
Files in discord-php/slash
Package slash
Short Description HTTP server for Discord slash commands
License MIT
Informations about the package slash
DiscordPHP-Slash
PHP server and client for Discord slash commands. Please read the Discord slash command documentation before using this library.
If you are already using DiscordPHP v7+ you DO NOT need this DiscordPHP-Slash library. Read more here: https://github.com/discord-php/DiscordPHP/wiki/Slash-Command
Warning
Discord slash commands are still in beta. Expect the way these commands work to change at any time without notice. Same goes for this library.
Requirements
- PHP >=7.3
- Composer
Installation
Usage
There are two "clients" in the library:
Discord\Slash\RegisterClient
used for registering commands with Discord.Discord\Slash\Client
used for listening for HTTP requests and responding.
Discord\Slash\RegisterClient
You should read up on how commands are registered in the Discord Developer Documentation, specifically the options
array when creating and updating commands.
Discord\Slash\Client
There are two ways to set up the slash client:
- Webhook method
- Gateway method (deprecated)
Please read both sections as both have important information and both have advantages/disadvantages.
Webhook method
Now that you have registered commands, you can set up an HTTP server to listen for requests from Discord.
There are a few ways to set up an HTTP server to listen for requests:
- The built-in ReactPHP HTTP server.
- Using the built-in ReactPHP HTTP server without HTTPS and using Apache or nginx as a reverse proxy (recommended).
- Using an external HTTP server such as Apache or nginx.
Whatever path you choose, the server must be protected with HTTPS - Discord will not accept regular HTTP.
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.
Setting up a basic Client
:
Please note that you must always acknowledge the interaction within 3 seconds, otherwise Discord will cancel the interaction.
If you are going to do something that takes time, call the acknowledge()
function and then add a follow up message using sendFollowUpMessage()
when ready.
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. Here is an example:
Running behing PHP-CGI/PHP-FPM
To run behind CGI/FPM and a webserver, the kambo/httpmessage
package is required:
The syntax is then exactly the same as if you were running with the ReactPHP http server, except for the last line:
Do note that the regular DiscordPHP client will not run on CGI or FPM, so your mileage may vary.
Setting up Apache2 as a reverse proxy
Assuming you already have Apache2 installed and the SSL certificates on your server:
-
Enable the required Apache mods:
-
Create a new site or modify the existing default site to listen on port
443
: - Restart apache - the code below works on Debian-based systems:
Gateway method (Deprecated)
Starting with DiscordPHP v7.0.0, slash commands are now integrated into the main library. You no longer need this DiscordPHP-Slash library anymore! Read more here: https://github.com/discord-php/DiscordPHP/blob/master/V7_CONVERSION.md#slash-commands
The client can connect with a regular DiscordPHP client to listen for interactions over gateway. To use this method, make sure there is no interactions endpoint set in your Discord developer application.
Make sure you have included DiscordPHP into your project (at the time of writing, only DiscordPHP 6.x is supported):
You can then create both clients and link them:
The gateway method is much easier to set up as you do not have to worry about SSL certificates.
License
This software is licensed under the MIT license which can be viewed in the LICENSE.md file.
Credits
- David Cole
All versions of slash with dependencies
react/http Version ^1.2
symfony/options-resolver Version ^5.2
monolog/monolog Version ^2.2
guzzlehttp/guzzle Version ^7.2
discord/interactions Version ^1|^2
simplito/elliptic-php Version ^1