Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/Commands/AbstractCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
use Discord\Parts\Interactions\Command\Command;
use Discord\Parts\User\User;
use Illuminate\Support\Str;
use Laracord\Concerns\HasHandler;
use Laracord\Discord\Concerns\HasModal;
use Laracord\Laracord;

abstract class AbstractCommand
{
use HasModal;
use HasHandler, HasModal;

/**
* The bot instance.
Expand Down Expand Up @@ -185,7 +186,7 @@ public function getUser($user)
throw new Exception('The user model could not be found.');
}

return $this->user = $model::firstOrCreate(['discord_id' => $user->id], [
return $model::firstOrCreate(['discord_id' => $user->id], [
'discord_id' => $user->id,
'username' => $user->username,
]) ?? null;
Expand Down
13 changes: 13 additions & 0 deletions src/Commands/ApplicationCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Laracord\Commands;

use Discord\Parts\Interactions\Interaction;
use Discord\Parts\Permissions\RolePermission;

abstract class ApplicationCommand extends AbstractCommand
Expand Down Expand Up @@ -41,4 +42,16 @@ public function isNsfw(): bool
{
return $this->nsfw;
}

/**
* Handle the denied command.
*/
public function handleDenied(Interaction $interaction): void
{
$this
->message('You do not have permission to run this command.')
->title('Permission Denied')
->error()
->reply($interaction, ephemeral: true);
}
}
26 changes: 10 additions & 16 deletions src/Commands/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Laracord\Commands;

use Discord\Parts\Channel\Message;
use Illuminate\Support\Str;
use Laracord\Commands\Contracts\Command as CommandContract;

Expand Down Expand Up @@ -37,12 +38,8 @@ abstract class Command extends AbstractCommand implements CommandContract

/**
* Maybe handle the command.
*
* @param \Discord\Parts\Channel\Message $message
* @param array $args
* @return mixed
*/
public function maybeHandle($message, $args)
public function maybeHandle(Message $message, array $args): void
{
if (! $this->canDirectMessage() && ! $message->guild_id) {
return;
Expand All @@ -53,7 +50,10 @@ public function maybeHandle($message, $args)
}

if (! $this->isAdminCommand()) {
$this->handle($message, $args);
$this->resolveHandler([
'message' => $message,
'args' => $args,
]);

return;
}
Expand All @@ -62,18 +62,12 @@ public function maybeHandle($message, $args)
return;
}

$this->handle($message, $args);
$this->resolveHandler([
'message' => $message,
'args' => $args,
]);
}

/**
* Handle the command.
*
* @param \Discord\Parts\Channel\Message $message
* @param array $args
* @return mixed
*/
abstract public function handle($message, $args);

/**
* Retrieve the command cooldown.
*
Expand Down
33 changes: 12 additions & 21 deletions src/Commands/ContextMenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

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 @@ -33,18 +31,10 @@ public function create(): DiscordCommand
return new DiscordCommand($this->discord(), $menu->all());
}

/**
* Handle the context menu 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 mixed
*/
public function maybeHandle($interaction)
public function maybeHandle(Interaction $interaction): void
{
$target = match ($this->getType()) {
DiscordCommand::USER => $interaction->data->resolved->users?->first(),
Expand All @@ -53,23 +43,24 @@ public function maybeHandle($interaction)
};

if (! $this->isAdminCommand()) {
$this->handle($interaction, $target);
$this->resolveHandler([
'interaction' => $interaction,
'target' => $target,
]);

return;
}

if ($this->isAdminCommand() && ! $this->isAdmin($interaction->member->user)) {
return $interaction->respondWithMessage(
$this
->message('You do not have permission to run this command.')
->title('Permission Denied')
->error()
->build(),
ephemeral: true
);
$this->handleDenied($interaction);

return;
}

$this->handle($interaction, $target);
$this->resolveHandler([
'interaction' => $interaction,
'target' => $target,
]);
}

/**
Expand Down
7 changes: 1 addition & 6 deletions src/Commands/Contracts/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@

namespace Laracord\Commands\Contracts;

use Discord\Parts\Channel\Message;

interface Command
{
/**
* Handle the command.
*/
public function handle(Message $message, array $args);
//
}
9 changes: 1 addition & 8 deletions src/Commands/Contracts/ContextMenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,7 @@

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, Message|User|null $target): mixed;
//
}
7 changes: 1 addition & 6 deletions src/Commands/Contracts/SlashCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@

namespace Laracord\Commands\Contracts;

use Discord\Parts\Interactions\Interaction;

interface SlashCommand
{
/**
* Handle the slash command.
*/
public function handle(Interaction $interaction);
//
}
32 changes: 10 additions & 22 deletions src/Commands/SlashCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,46 +65,34 @@ public function create(): DiscordCommand
return new DiscordCommand($this->discord(), $command);
}

/**
* Handle the slash command.
*
* @param \Discord\Parts\Interactions\Interaction $interaction
* @return mixed
*/
abstract public function handle($interaction);

/**
* Maybe handle the slash command.
*
* @param \Discord\Parts\Interactions\Interaction $interaction
* @return mixed
*/
public function maybeHandle($interaction)
public function maybeHandle(Interaction $interaction): void
{
if (! $this->isAdminCommand()) {
$this->parseOptions($interaction);

$this->handle($interaction);
$this->resolveHandler([
'interaction' => $interaction,
]);

$this->clearOptions();

return;
}

if ($this->isAdminCommand() && ! $this->isAdmin($interaction->member->user)) {
return $interaction->respondWithMessage(
$this
->message('You do not have permission to run this command.')
->title('Permission Denied')
->error()
->build(),
ephemeral: true
);
$this->handleDenied($interaction);

return;
}

$this->parseOptions($interaction);

$this->handle($interaction);
$this->resolveHandler([
'interaction' => $interaction,
]);

$this->clearOptions();
}
Expand Down
22 changes: 22 additions & 0 deletions src/Concerns/HasHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Laracord\Concerns;

use Exception;

trait HasHandler
{
/**
* Resolve the handler using the container.
*/
protected function resolveHandler(array $parameters = []): mixed
{
if (! method_exists($this, 'handle')) {
$class = get_class($this);

throw new Exception("{$class} must implement a handle method.");
}

return $this->bot->app->call([$this, 'handle'], $parameters);
}
}
9 changes: 3 additions & 6 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\Channel\Message;
use Discord\Parts\Interactions\Interaction;
use Laracord\Commands\Command;

Expand Down Expand Up @@ -37,19 +38,15 @@ class {{ class }} extends Command

/**
* Handle the command.
*
* @param \Discord\Parts\Channel\Message $message
* @param array $args
* @return void
*/
public function handle($message, $args)
public function handle(Message $message, array $args): void
{
return $this
->message()
->title('{{ title }}')
->content('Hello world!')
->button('πŸ‘‹', route: 'wave')
->send($message);
->reply($message);
}

/**
Expand Down
16 changes: 7 additions & 9 deletions src/Console/Commands/stubs/context-menu.stub
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,14 @@ class {{ class }} extends ContextMenu
/**
* Handle the context menu interaction.
*/
public function handle(Interaction $interaction, Message|User|null $target): mixed
public function handle(Interaction $interaction, Message|User|null $target): void
{
$interaction->respondWithMessage(
$this
->message()
->title('{{ title }}')
->content('Hello world!')
->button('πŸ‘‹', route: 'wave')
->build()
);
$this
->message()
->title('{{ title }}')
->content('Hello world!')
->button('πŸ‘‹', route: 'wave')
->reply($interaction);
}

/**
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 }}): void
{
$this->console()->log('The {{ eventName }} event has fired!');
}
Expand Down
7 changes: 6 additions & 1 deletion src/Console/Commands/stubs/service.stub
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,15 @@ class {{ class }} extends Service
*/
protected int $interval = 5;

/**
* Determine if the service handler should execute during boot.
*/
protected bool $eager = false;

/**
* Handle the service.
*/
public function handle(): mixed
public function handle(): void
{
$this->console()->log('Hello world.');
}
Expand Down
Loading
Loading