Skip to content

Commit

Permalink
πŸ§‘β€πŸ’» Pass the resolved target to context menus (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
Log1x authored Jul 18, 2024
2 parents 92b1527 + e3a68ff commit fc11623
Show file tree
Hide file tree
Showing 14 changed files with 49 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/Commands/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function maybeHandle($message, $args)
*
* @param \Discord\Parts\Channel\Message $message
* @param array $args
* @return void
* @return mixed
*/
abstract public function handle($message, $args);

Expand Down
20 changes: 13 additions & 7 deletions src/Commands/ContextMenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

namespace Laracord\Commands;

use Discord\Parts\Channel\Message;
use Discord\Parts\Interactions\Command\Command as DiscordCommand;
use Discord\Parts\Interactions\Interaction;
use Discord\Parts\User\User;
use Laracord\Commands\Contracts\ContextMenu as ContextMenuContract;

abstract class ContextMenu extends ApplicationCommand implements ContextMenuContract
Expand Down Expand Up @@ -32,22 +35,25 @@ public function create(): DiscordCommand

/**
* Handle the context menu interaction.
*
* @param \Discord\Parts\Interactions\Interaction $interaction
* @return void
*/
abstract public function handle($interaction);
abstract public function handle(Interaction $interaction, Message|User|null $target): mixed;

/**
* Maybe handle the context menu interaction.
*
* @param \Discord\Parts\Interactions\Interaction $interaction
* @return void
* @return mixed
*/
public function maybeHandle($interaction)
{
$target = match ($this->getType()) {
DiscordCommand::USER => $interaction->data->resolved->users?->first(),
DiscordCommand::MESSAGE => $interaction->data->resolved->messages?->first(),
default => null,
};

if (! $this->isAdminCommand()) {
$this->handle($interaction);
$this->handle($interaction, $target);

return;
}
Expand All @@ -63,7 +69,7 @@ public function maybeHandle($interaction)
);
}

$this->handle($interaction);
$this->handle($interaction, $target);
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/Commands/Contracts/ContextMenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

namespace Laracord\Commands\Contracts;

use Discord\Parts\Channel\Message;
use Discord\Parts\Interactions\Interaction;
use Discord\Parts\User\User;

interface ContextMenu
{
/**
* Handle the context menu interaction.
*/
public function handle(Interaction $interaction);
public function handle(Interaction $interaction, Message|User|null $target): mixed;
}
4 changes: 2 additions & 2 deletions src/Commands/SlashCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,15 @@ public function create(): DiscordCommand
* Handle the slash command.
*
* @param \Discord\Parts\Interactions\Interaction $interaction
* @return void
* @return mixed
*/
abstract public function handle($interaction);

/**
* Maybe handle the slash command.
*
* @param \Discord\Parts\Interactions\Interaction $interaction
* @return void
* @return mixed
*/
public function maybeHandle($interaction)
{
Expand Down
6 changes: 5 additions & 1 deletion src/Console/Commands/MakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ protected function replaceClass($stub, $name)

$command = $this->option('command') ?: Str::of($name)->classBasename()->kebab()->value();

return str_replace(['dummy:command', '{{ command }}'], $command, $stub);
$title = Str::of($command)->replace('-', ' ')->apa();

$stub = str_replace(['dummy:command', '{{ command }}', '{{ title }}'], [$command, $command, $title], $stub);

return $stub;
}

/**
Expand Down
6 changes: 5 additions & 1 deletion src/Console/Commands/MakeMenuCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ protected function replaceClass($stub, $name)

$command = $this->option('command') ?: Str::of($name)->classBasename()->kebab()->value();

return str_replace(['dummy:command', '{{ command }}'], $command, $stub);
$title = Str::of($command)->replace('-', ' ')->apa();

$stub = str_replace(['dummy:command', '{{ command }}', '{{ title }}'], [$command, $command, $title], $stub);

return $stub;
}

/**
Expand Down
6 changes: 5 additions & 1 deletion src/Console/Commands/MakeSlashCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ protected function replaceClass($stub, $name)

$command = $this->option('command') ?: Str::of($name)->classBasename()->kebab()->value();

return str_replace(['dummy:command', '{{ command }}'], $command, $stub);
$title = Str::of($command)->replace('-', ' ')->apa();

$stub = str_replace(['dummy:command', '{{ command }}', '{{ title }}'], [$command, $command, $title], $stub);

return $stub;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Console/Commands/stubs/command.stub
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class {{ class }} extends Command
*
* @var string
*/
protected $description = 'The {{ command }} command.';
protected $description = 'The {{ title }} command.';

/**
* Determines whether the command requires admin permissions.
Expand All @@ -46,7 +46,7 @@ class {{ class }} extends Command
{
return $this
->message()
->title('{{ class }}')
->title('{{ title }}')
->content('Hello world!')
->button('πŸ‘‹', route: 'wave')
->send($message);
Expand Down
11 changes: 5 additions & 6 deletions src/Console/Commands/stubs/context-menu.stub
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace {{ namespace }};

use Discord\Parts\Channel\Message;
use Discord\Parts\Interactions\Interaction;
use Discord\Parts\User\User;
use Laracord\Commands\ContextMenu;

class {{ class }} extends ContextMenu
Expand All @@ -12,7 +14,7 @@ class {{ class }} extends ContextMenu
*
* @var string
*/
protected $name = '{{ command }}';
protected $name = '{{ title }}';

/**
* The context menu type.
Expand All @@ -35,16 +37,13 @@ class {{ class }} extends ContextMenu

/**
* Handle the context menu interaction.
*
* @param \Discord\Parts\Interactions\Interaction $interaction
* @return void
*/
public function handle($interaction)
public function handle(Interaction $interaction, Message|User|null $target): mixed
{
$interaction->respondWithMessage(
$this
->message()
->title('{{ class }}')
->title('{{ title }}')
->content('Hello world!')
->button('πŸ‘‹', route: 'wave')
->build()
Expand Down
2 changes: 1 addition & 1 deletion src/Console/Commands/stubs/event.stub
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class {{ class }} extends Event
/**
* Handle the event.
*/
public function handle({{ attributes }})
public function handle({{ attributes }}): mixed
{
$this->console()->log('The {{ eventName }} event has fired!');
}
Expand Down
2 changes: 1 addition & 1 deletion src/Console/Commands/stubs/service.stub
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class {{ class }} extends Service
/**
* Handle the service.
*/
public function handle(): void
public function handle(): mixed
{
$this->console()->log('Hello world.');
}
Expand Down
6 changes: 3 additions & 3 deletions src/Console/Commands/stubs/slash-command.stub
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class {{ class }} extends SlashCommand
*
* @var string
*/
protected $description = 'The {{ command }} slash command.';
protected $description = 'The {{ title }} slash command.';

/**
* The command options.
Expand Down Expand Up @@ -53,14 +53,14 @@ class {{ class }} extends SlashCommand
* Handle the slash command.
*
* @param \Discord\Parts\Interactions\Interaction $interaction
* @return void
* @return mixed
*/
public function handle($interaction)
{
$interaction->respondWithMessage(
$this
->message()
->title('{{ class }}')
->title('{{ title }}')
->content('Hello world!')
->button('πŸ‘‹', route: 'wave')
->build()
Expand Down
2 changes: 2 additions & 0 deletions src/Services/Contracts/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ interface Service
{
/**
* Handle the service.
*
* @return mixed
*/
public function handle();
}
2 changes: 1 addition & 1 deletion src/Services/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public static function make(Laracord $bot): self
/**
* Handle the service.
*
* @return void
* @return mixed
*/
abstract public function handle();

Expand Down

0 comments on commit fc11623

Please sign in to comment.