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.
Controlling who gets pinged
Mention syntax placed in a message's content notifies by default. Use an AllowedMentions object to restrict (or fully suppress) those notifications:
Delivery options
These are available on Message, WebhookMessage, and ReplyWithMessage.
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
The auto-populated select menus (user, role, mentionable, and channel) can be pre-populated with default selections:
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.
Component Interactions
When a user clicks a button or makes a selection in a select menu, Discord sends a ComponentInteracted interaction. You can identify which component was used via its custom_id, and read any selected values.
Updating the original message
Instead of sending a new message, you can edit the message the component is attached to (for example, to disable a button after it's clicked) by responding with an UpdateMessage:
Deferring a response
Discord requires a response within 3 seconds. If you need longer, acknowledge the interaction first and follow up later: