Skip to content

Commit

Permalink
✨ Add persistent interaction routing support to commands (#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
Log1x authored Jun 5, 2024
2 parents 29e35d0 + 231f53c commit 47b83ae
Show file tree
Hide file tree
Showing 6 changed files with 195 additions and 26 deletions.
10 changes: 9 additions & 1 deletion src/Commands/AbstractCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,14 @@ public static function make(Laracord $bot): self
return new static($bot);
}

/**
* The command interaction routes.
*/
public function interactions(): array
{
return [];
}

/**
* Build an embed for use in a Discord message.
*
Expand All @@ -106,7 +114,7 @@ public static function make(Laracord $bot): self
*/
public function message($content = '')
{
return $this->bot()->message($content);
return $this->bot()->message($content)->routePrefix($this->getName());
}

/**
Expand Down
1 change: 0 additions & 1 deletion src/Commands/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Laracord\Commands;

use Laracord\Commands\Contracts\Command as CommandContract;
use Laracord\Discord\Message;

abstract class Command extends AbstractCommand implements CommandContract
{
Expand Down
12 changes: 12 additions & 0 deletions src/Console/Commands/stubs/command.stub
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace {{ namespace }};

use Discord\Parts\Interactions\Interaction;
use Laracord\Commands\Command;

class {{ class }} extends Command
Expand Down Expand Up @@ -47,6 +48,17 @@ class {{ class }} extends Command
->message()
->title('{{ class }}')
->content('Hello world!')
->button('👋', route: 'wave')
->send($message);
}

/**
* The command interaction routes.
*/
public function interactions(): array
{
return [
'wave' => fn (Interaction $interaction) => $this->message('👋')->reply($interaction),
];
}
}
12 changes: 12 additions & 0 deletions src/Console/Commands/stubs/slash-command.stub
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace {{ namespace }};

use Discord\Parts\Interactions\Interaction;
use Laracord\Commands\SlashCommand;

class {{ class }} extends SlashCommand
Expand Down Expand Up @@ -61,7 +62,18 @@ class {{ class }} extends SlashCommand
->message()
->title('{{ class }}')
->content('Hello world!')
->button('👋', route: 'wave')
->build()
);
}

/**
* The command interaction routes.
*/
public function interactions(): array
{
return [
'wave' => fn (Interaction $interaction) => $this->message('👋')->reply($interaction),
];
}
}
52 changes: 50 additions & 2 deletions src/Discord/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Discord\Builders\MessageBuilder;
use Discord\Parts\Channel\Channel;
use Discord\Parts\Channel\Message as ChannelMessage;
use Discord\Parts\Interactions\Interaction;
use Discord\Parts\User\User;
use Exception;
use Illuminate\Support\Carbon;
Expand Down Expand Up @@ -138,6 +139,11 @@ class Message
'info' => 3447003,
];

/**
* The interaction route prefix.
*/
protected ?string $routePrefix = null;

/**
* Create a new Discord message instance.
*
Expand Down Expand Up @@ -230,6 +236,18 @@ public function sendTo(mixed $user): ?ExtendedPromiseInterface
return $user->sendMessage($this->build());
}

/**
* Reply to a message or interaction.
*/
public function reply(Interaction|ChannelMessage $message, bool $ephemeral = false): ExtendedPromiseInterface
{
if ($message instanceof Interaction) {
return $message->respondWithMessage($this->build(), ephemeral: $ephemeral);
}

return $message->reply($this->build());
}

/**
* Get the embed.
*/
Expand Down Expand Up @@ -630,7 +648,7 @@ public function components(array $components): self
/**
* Add a URL button to the message.
*/
public function button(string $label, mixed $value, mixed $emoji = null, ?string $style = null, bool $disabled = false, ?string $id = null, array $options = []): self
public function button(string $label, mixed $value = null, mixed $emoji = null, ?string $style = null, bool $disabled = false, ?string $id = null, ?string $route = null, array $options = []): self
{
$style = match ($style) {
'link' => Button::STYLE_LINK,
Expand All @@ -652,6 +670,12 @@ public function button(string $label, mixed $value, mixed $emoji = null, ?string
$button = $button->setCustomId($id);
}

if ($route) {
$button = $this->getRoutePrefix()
? $button->setCustomId("{$this->getRoutePrefix()}@{$route}")
: $button->setCustomId($route);
}

if ($options) {
foreach ($options as $key => $option) {
$key = Str::of($key)->camel()->ucfirst()->start('set')->toString();
Expand All @@ -668,9 +692,15 @@ public function button(string $label, mixed $value, mixed $emoji = null, ?string

$button = match ($style) {
Button::STYLE_LINK => $button->setUrl($value),
default => $button->setListener($value, $this->bot->discord()),
default => $value ? $button->setListener($value, $this->bot->discord()) : $button,
};

if (! $value && ! $route && ! $id) {
throw new Exception('Message buttons must contain a valid `value`, `route`, or `id`.');

return $this;
}

$this->buttons[] = $button;

return $this;
Expand Down Expand Up @@ -701,4 +731,22 @@ public function clearButtons(): self

return $this;
}

/**
* Set the interaction route prefix.
*/
public function routePrefix(?string $routePrefix): self
{
$this->routePrefix = Str::slug($routePrefix);

return $this;
}

/**
* Retrieve the interaction route prefix.
*/
public function getRoutePrefix(): ?string
{
return $this->routePrefix;
}
}
Loading

0 comments on commit 47b83ae

Please sign in to comment.