Last active
June 29, 2024 19:26
-
-
Save Log1x/427c764ce310bb10bb4acfcb992b3a2d to your computer and use it in GitHub Desktop.
Laracord Pagination Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Commands; | |
use App\Models\Quote; | |
use Discord\Parts\Interactions\Interaction; | |
use Laracord\Commands\Command; | |
class QuotesCommand extends Command | |
{ | |
/** | |
* The command name. | |
* | |
* @var string | |
*/ | |
protected $name = 'quotes'; | |
/** | |
* The command description. | |
* | |
* @var string | |
*/ | |
protected $description = 'Manage the quotes.'; | |
/** | |
* Handle the command. | |
* | |
* @param \Discord\Parts\Channel\Message $message | |
* @param array $args | |
* @return void | |
*/ | |
public function handle($message, $args) | |
{ | |
$this->quote($message, $args[0] ?? null); | |
} | |
/** | |
* Retrieve a quote. | |
* | |
* @param \Discord\Parts\Channel\Message $message | |
* @param int $id | |
* @return void | |
*/ | |
protected function quote($message, $id = null) | |
{ | |
$quote = $id | |
? Quote::find($id) | |
: Quote::inRandomOrder()->first(); | |
if (! $quote) { | |
return $this | |
->message('Quote not found.') | |
->title('Quotes') | |
->error() | |
->reply($message); | |
} | |
$previous = Quote::where('id', '<', $quote->id)->max('id') ?? null; | |
$next = Quote::where('id', '>', $quote->id)->min('id') ?? null; | |
return $this | |
->message() | |
->button('Previous Quote', route: "quote:{$previous}", disabled: ! $previous) | |
->button('Next Quote', route: "quote:{$next}", disabled: ! $next) | |
->title("Quote #{$id}") | |
->content($quote->content) | |
->timestamp($quote->created_at) | |
->editOrReply($message); | |
} | |
/** | |
* The command interaction routes. | |
*/ | |
public function interactions(): array | |
{ | |
return [ | |
'quote:{id?}' => fn (Interaction $interaction, ?string $id = null) => $interaction->acknowledge() && $this->quote($interaction->message, $id), | |
]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment