Download the PHP package more-cores/discord-commands without Composer
On this page you can find all versions of the php package more-cores/discord-commands. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package discord-commands
Discord Commands
Using this library you can build Discord bot commands, process inbound interactions from Discord and respond with messages.
Table of contents
- Installation
- Creating Messages
- Mentioning
- Enriching
- Author
- Fields
- Image
- Thumbnail
- Footer
- Components
- Buttons
- SelectMenus
- Role Select Menus
- Mentionable Select Menus
- Mentionable Select Menus
- Channel Select Menus
- Text Input
- Creating Commands
- Chat commands
- Handling Command Interactions
- Request verification
- Laravel middleware
- Responding to Interactions
- Modals
Installation
Creating Messages
Creating webhook messages (they have higher rate limits):
Or using standard messaging:
Mentioning
Both Message
and WebhookMessage
offer the ability to mention roles.
Enriching
Certain information can be enriched on the Discord side, such as linking to channels and timestamps in the user's local time.
Linking to a Channel
Linking to a Special Channel
Embedding Timestamps
Author
Fields
Image
Thumbnail
Footer
Components
Buttons
Use dedicated classes for each button type. Because buttons are non-interactive, you'll need to wrap them with an ActionRow
when adding them to a message. You can use the shorthand actionRow()
to accomplish this.
SelectMenus
Role Select Menus
Mentionable Select Menus
Mentionable Select Menus
Channel Select Menus
You can also limit which channel types can be selected
Text Input
Creating Commands
Chat commands
To create a chat command, create a class like this:
You can then simply run (new MyCommand())->jsonSerialize()
to generate the json payload needed to sync your command with Discord.
Handling Command Interactions
You can use our factory to hydrate objects that represent incoming interactions within Discord. Here's an example using a Laravel request object:
Request verification
Discord requires that you verify the signature of inbound requests from their system. You'll need to have this in place in order to even configure your interactions endpoint.
You'll need to run composer require simplito/elliptic-php
to add the necessary dependency, then you can verify the request like this:
Laravel middleware
For Laravel applications, we've included a middleware out of the box to assist with this. You can configure a route in routes/api.php
and wrap it with our verification middleware:
Then in your controller, make sure you respond to Ping
with a Pong
...
Responding to Interactions
You can respond to interactions by sending messages, showing modals, etc. Make sure to give the Discord docs a read (e.g. your app has 3 seconds to respond to an interaction).
Modals
To show a modal, you can simply respond to the interaction http request with your modal object:
Showing the modal
Processing modal feedback
To process processing modal feedback, make sure you're handling ModalSubmitted
events.
In the below example, $commandFactory
is not provided by this package, but relates to your domain. You can customize that process however you'd like.
In my case, I like for a single class to contain all the details about what happens on a Command. So I added a whenSubmitted
method that looks something like this:
The key takeaway here is that Discord requires an interaction in response to the modal submission. So you'll need to respond with something.