-
Notifications
You must be signed in to change notification settings - Fork 237
Option_commands
FoxWorn3365 edited this page Jan 24, 2023
·
8 revisions
Options are part of Components.
use Discord\Parts\Interactions\Command\Option;
Add options to Slash Commands
In this case, the example proposed in the link above is used.
use Discord\Builders\CommandBuilder;
//...
$discord->application->commands->save($discord->application->commands->create(CommandBuilder::new()
->setName('greet')
->setDescriptions('Greet an user')
->addOption((new Option($discord))
->setName('user')
->setDescription('User to greet')
->setType(Option::USER)
->setRequired(true)
)
->toArray()
));
⚠️ Warning!
Defining commands with the CommandBuilder no longer appears to be supported inPHP 8.2
asCreation of dynamic property is deprecated
Then:
// Handle
$discord->listenCommand('greet', function (Interaction $interaction) {
$user = $interaction->data->resolved->users->first();
$interaction->respondWithMessage(MessageBuilder::new()->setContent("Hello, {$user}"));
});
In this case, the example proposed in the link above is used.
use Discord\Builders\CommandBuilder;
//...
$discord->application->commands->save($discord->application->commands->create(CommandBuilder::new()
->setName('say')
->setDescriptions('Say a phrase')
->addOption((new Option($discord))
->setName('text')
->setDescription('Phrase')
->setType(Option::STRING)
->setRequired(true)
)
->toArray()
));
and then for manage the command when executed:
$discord->listenCommand('greet', function (Interaction $interaction) {
$args = $interaction->data->options->toArray();
$content = $interaction->data->options->offsetGet('text')->value;
$interaction->respondWithMessage(MessageBuilder::new()->setContent($content));
});
$discord->listenCommand('mycommand', function (Interaction $interaction) {
// ...
$interaction->respondWithMessage(MessageBuilder::new()->setContent($content), true);
});
Note
To retrieve the associative array directly from the command and turn it into an object without having to retrieve each argument first, you can do this. It turns out, however, that it is not recommended to use.$args = (object)$interaction->data->options->toArray();
Note: This wiki is currently Work In Progress. Consider reading the docs instead.
- Application Command (Slash based)
Command Client (Message based)
- Activity
- Application
- Guild
- Private Channel
- User
Components
-
ActionRow
- Buttons
- Option (commands)
- SelectMenu
- TextInput
Builders