PHP code example of revolution / discord-manager

1. Go to this page and download the library: Download revolution/discord-manager 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/ */

    

revolution / discord-manager example snippets


return [
    // Guild-specific commands (only available in specified servers)
    'guild' => [
        [
            'name' => 'hello',
            'description' => 'Say hello to a user',
            'type' => CommandType::CHAT_INPUT,
            'guild_id' => env('DISCORD_GUILD'),
            'options' => [
                [
                    'name' => 'user',
                    'description' => 'User to greet',
                    'type' => CommandOptionType::USER,
                    '=> env('DISCORD_PUBLIC_KEY'),
    'path' => 'discord/webhook', // Webhook endpoint path
    'route' => 'discord.webhook', // Route name
    'middleware' => 'throttle', // Additional middleware
];



namespace App\Discord\Interactions;

use Illuminate\Http\Request;
use Revolution\DiscordManager\Concerns\WithInteraction;

class HelloCommand
{
    use WithInteraction;

    public string $command = 'hello';

    public function __invoke(Request $request): void
    {
        $user = $request->json('member.user.id', $request->json('user.id'));

        $data = [
            'content' => "<@$user> Hello from Laravel!",
            'allowed_mentions' => ['parse' => ['users']],
        ];

        $response = $this->followup(token: $request->json('token'), data: $data);
    }
}

return [
    // For guild-specific commands (only available in specified servers)
    'guild' => [
        [
            'name' => 'hello',
            'description' => 'Say hello from Laravel',
            'type' => CommandType::CHAT_INPUT,
            'guild_id' => env('DISCORD_GUILD'),
        ],
        // Add more guild commands here...
    ],

    // For global commands (available in all servers where your bot is installed)
    'global' => [
        [
            'name' => 'hello',
            'description' => 'Say hello from Laravel',
            'type' => CommandType::CHAT_INPUT,
        ],
        // Add more global commands here...
    ],

    // ... rest of configuration
];



namespace App\Listeners;

use Revolution\DiscordManager\Events\InteractionsWebhook;
use Revolution\DiscordManager\Facades\DiscordManager;

class InteractionsListener
{
    public function handle(InteractionsWebhook $event): void
    {
        DiscordManager::interaction($event->request);
    }
}



namespace App\Discord\Interactions;

use Illuminate\Http\Request;
use Revolution\DiscordManager\Concerns\WithInteraction;

class GreetCommand
{
    use WithInteraction;

    public string $command = 'greet';

    public function __invoke(Request $request): void
    {
        $targetUser = $request->json('data.options.0.value');
        $message = $request->json('data.options.1.value', 'Hello!');
        $user = $request->json('member.user.id', $request->json('user.id'));

        $data = [
            'content' => "<@$targetUser> $message (from <@$user>)",
            'allowed_mentions' => ['parse' => ['users']],
        ];

        $this->followup(token: $request->json('token'), data: $data);
    }
}



namespace App\Discord\Interactions;

use Illuminate\Http\Request;
use Revolution\DiscordManager\Concerns\WithInteraction;
use Revolution\DiscordManager\Support\ComponentType;
use Revolution\DiscordManager\Support\ButtonStyle;

class ButtonCommand
{
    use WithInteraction;

    public string $command = 'button-demo';

    public function __invoke(Request $request): void
    {
        $data = [
            'content' => 'Click a button below:',
            'components' => [
                [
                    'type' => ComponentType::ACTION_ROW->value,
                    'components' => [
                        [
                            'type' => ComponentType::BUTTON->value,
                            'style' => ButtonStyle::PRIMARY->value,
                            'label' => 'Primary',
                            'custom_id' => 'primary_button',
                        ],
                        [
                            'type' => ComponentType::BUTTON->value,
                            'style' => ButtonStyle::SUCCESS->value,
                            'label' => 'Success',
                            'custom_id' => 'success_button',
                        ],
                    ],
                ],
            ],
        ];

        $this->followup(token: $request->json('token'), data: $data);
    }
}



namespace App\Discord\Interactions;

use Illuminate\Http\Request;
use Revolution\DiscordManager\Concerns\WithInteraction;

class PrimaryButtonCommand
{
    use WithInteraction;

    public string $command = 'primary_button';

    public function __invoke(Request $request): void
    {
        $user = $request->json('member.user.id', $request->json('user.id'));

        $data = [
            'content' => "<@$user> You clicked the primary button!",
            'allowed_mentions' => ['parse' => ['users']],
        ];

        $this->followup(token: $request->json('token'), data: $data);
    }
}
shell
php artisan vendor:publish --tag=discord-interactions-config
shell
php artisan discord:make:interaction HelloCommand
shell
php artisan discord:interactions:register
shell
php artisan make:listener InteractionsListener
shell
php artisan serve