laracord / framework

A Discord bot micro-framework powered by Laravel.
https://laracord.com
MIT License
33 stars 10 forks source link

✨ Add persistent interaction routing support to commands (Fixes #79) #80

Closed Log1x closed 5 months ago

Log1x commented 5 months ago

This PR adds persistent interaction routing allowing for easy persistence and handling of interactions such as button clicks.

It is based on Laravel routing and allows for passing parameters such as {id} and making them optional using a ? like {id?}. All parameters should be passed using : instead of /.

All routes are prefixed with the command name behind the scenes to remain unique.

It is hopefully pretty straight forward to use, see the following example:

/**
 * Handle the command.
 */
public function handle($message, $args)
{
    return $this
        ->message()
        ->title('Hello')
        ->content('Hello world!')
        ->button('👋', route: 'wave')
        ->button('Test', route: 'wave:12345')
        ->send($message);
}

/**
 * The command interaction routes.
 */
public function interactions(): array
{
    return [
        'wave' => fn (Interaction $interaction) => $this->message('👋')->reply($interaction, true),
        'wave:{id?}' => fn (Interaction $interaction, string $id = 'default') => $this->message("Hello from {$id}")->reply($interaction, true),
    ];
}