PHP code example of plytas / laravel-discord-interactions
1. Go to this page and download the library: Download plytas/laravel-discord-interactions library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
plytas / laravel-discord-interactions example snippets
use Plytas\Discord\Contracts\DiscordCommand;
use Plytas\Discord\Data\DiscordInteraction;
use Plytas\Discord\Data\DiscordMessage;
use Plytas\Discord\Data\DiscordResponse;
class PingCommand implements DiscordCommand
{
public function description(): string
{
return 'Replies with pong';
}
public function handle(DiscordInteraction $interaction): DiscordResponse
{
return $interaction->respondWithMessage(
DiscordMessage::new()
->setContent('Pong!')
->ephemeral()
);
}
}
use Illuminate\Support\Facades\App;
use Plytas\Discord\Contracts\DiscordCommand;
use Plytas\Discord\Data\DiscordInteraction;
use Plytas\Discord\Data\DiscordMessage;
use Plytas\Discord\Data\DiscordResponse;
use Plytas\Discord\Facades\Discord;
class PingCommand implements DiscordCommand
{
public function description(): string
{
return 'Replies with pong';
}
public function handle(DiscordInteraction $interaction): DiscordResponse
{
// Register a terminating callback to update the message after processing is done
App::terminating(function () use ($interaction) {
// Simulate a long-running process
sleep(5);
Discord::updateInteractionMessage(
interaction: $interaction,
message: DiscordMessage::new()
->setContent('Pong!')
->ephemeral()
);
});
// Respond with a message that indicates that the command is still processing
return $interaction->respondWithMessage(
DiscordMessage::new()
->setContent('Calculating...')
->ephemeral() // Only the user who invoked the command can see the message
);
}
}
use Plytas\Discord\Components\ActionRow;
use Plytas\Discord\Components\Button;
use Plytas\Discord\Contracts\DiscordCommand;
use Plytas\Discord\Data\DiscordInteraction;
use Plytas\Discord\Data\DiscordMessage;
use Plytas\Discord\Data\DiscordResponse;
use Plytas\Discord\Enums\ButtonStyle;
class RockPaperScissorsCommand implements DiscordCommand
{
public function description(): string
{
return 'Play rock-paper-scissors';
}
public function handle(DiscordInteraction $interaction): DiscordResponse
{
return $interaction->respondWithMessage(
DiscordMessage::new()
->setContent('Select your move')
->addComponent(
component: ActionRow::new()
->addComponent(
component: Button::new()
->setCustomId('rock')
->setLabel('Rock')
->setEmoji('🪨')
->setStyle(ButtonStyle::Primary)
)
->addComponent(
component: Button::new()
->setCustomId('paper')
->setLabel('Paper')
->setEmoji('📄')
->setStyle(ButtonStyle::Primary)
)
->addComponent(
component: Button::new()
->setCustomId('scissors')
->setLabel('Scissors')
->setEmoji('✂️')
->setStyle(ButtonStyle::Primary)
)
)
->ephemeral() // Only the user who invoked the command can see the message
);
}
}
use Illuminate\Support\Arr;
use Plytas\Discord\Contracts\DiscordComponentHandler;
use Plytas\Discord\Data\DiscordInteraction;
use Plytas\Discord\Data\DiscordMessage;
use Plytas\Discord\Data\DiscordResponse;
class RockPaperScissorsComponentHandler implements DiscordComponentHandler
{
public function handle(DiscordInteraction $interaction): DiscordResponse
{
$playerSelection = $interaction->getMessageComponent()->custom_id;
$botSelection = Arr::random(['rock', 'paper', 'scissors']);
if ($playerSelection === $botSelection) {
$message = 'It\'s a tie!';
}
$winningMoves = [
'rock' => 'scissors',
'paper' => 'rock',
'scissors' => 'paper',
];
if ($winningMoves[$playerSelection] === $botSelection) {
$message = 'You win!';
} else {
$message = 'You lose!';
}
return $interaction->updateMessage(
DiscordMessage::new()
->setContent($message)
->ephemeral()
);
}
}