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
38 changes: 15 additions & 23 deletions src/Bot/Concerns/HasApplicationCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Laracord\Bot\Concerns;

use Discord\Discord;
use Discord\Parts\Interactions\Command\Option;
use Illuminate\Support\Arr;
use Laracord\Bot\Hook;
Expand All @@ -11,6 +12,11 @@
use function React\Async\await;
use function React\Promise\all;

/**
* Concern to manage application commands.
*
* @method Discord discord() Get the Discord instance.
*/
trait HasApplicationCommands
{
/**
Expand Down Expand Up @@ -44,14 +50,14 @@ protected function bootApplicationCommands(): self

$existing = [];

$existing[] = $this->discord->application->commands->freshen();
$existing[] = $this->discord()->application->commands->freshen();

foreach ($this->discord->guilds as $guild) {
foreach ($this->discord()->guilds as $guild) {
$existing[] = $guild->commands->freshen();
}

$existing = all($existing)->then(fn ($commands) => collect($commands)
->flatMap(fn ($command) => $command->toArray())
->flatMap(fn ($command) => $command->jsonSerialize())
->map(fn ($command) => collect($command->getCreatableAttributes())
->merge([
'id' => $command->id,
Expand Down Expand Up @@ -182,7 +188,7 @@ protected function bootApplicationCommands(): self
if ($command['state'] instanceof ContextMenu) {
$menu = $command['state'];

$this->discord->listenCommand(
$this->discord()->listenCommand(
$name,
fn ($interaction) => rescue(fn () => $menu->maybeHandle($interaction))
);
Expand All @@ -208,7 +214,7 @@ protected function bootApplicationCommands(): self
$subcommands = $subcommands->merge($subcommandGroups);

if ($subcommands->isNotEmpty()) {
$subcommands->each(fn ($names) => $this->discord->listenCommand(
$subcommands->each(fn ($names) => $this->discord()->listenCommand(
$names,
fn ($interaction) => rescue(fn () => $command->maybeHandle($interaction)),
fn ($interaction) => rescue(fn () => $command->maybeHandleAutocomplete($interaction))
Expand All @@ -217,7 +223,7 @@ protected function bootApplicationCommands(): self
return;
}

$this->discord->listenCommand(
$this->discord()->listenCommand(
$name,
fn ($interaction) => rescue(fn () => $command->maybeHandle($interaction)),
fn ($interaction) => rescue(fn () => $command->maybeHandleAutocomplete($interaction))
Expand All @@ -236,21 +242,7 @@ protected function bootApplicationCommands(): self
*/
protected function registerApplicationCommand(ApplicationCommand $command): void
{
if ($command->getGuild()) {
$guild = $this->discord->guilds->get('id', $command->getGuild());

if (! $guild) {
$this->logger->warning("The <fg=yellow>{$command->getName()}</> command failed to register because the guild <fg=yellow>{$command->getGuild()}</> could not be found.");

return;
}

$guild->commands->save($command->create());

return;
}

$this->discord->application->commands->save($command->create());
$command->create()->save();
}

/**
Expand All @@ -259,7 +251,7 @@ protected function registerApplicationCommand(ApplicationCommand $command): void
protected function unregisterApplicationCommand(string $id, ?string $guildId = null): void
{
if ($guildId) {
$guild = $this->discord->guilds->get('id', $guildId);
$guild = $this->discord()->guilds->get('id', $guildId);

if (! $guild) {
$this->logger->warning("The command with ID <fg=yellow>{$id}</> failed to unregister because the guild <fg=yellow>{$guildId}</> could not be found.");
Expand All @@ -272,6 +264,6 @@ protected function unregisterApplicationCommand(string $id, ?string $guildId = n
return;
}

$this->discord->application->commands->delete($id);
$this->discord()->application->commands->delete($id);
}
}
9 changes: 8 additions & 1 deletion src/Commands/AbstractCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ abstract class AbstractCommand
{
use HasHandler, HasLaracord, HasModal;

/**
* The command usage.
*
* @var string
*/
protected $usage = '';

/**
* The command name.
*
Expand Down Expand Up @@ -123,7 +130,7 @@ public function message($content = '')
public function isAdmin(User|string $user): bool
{
if (! $user instanceof User) {
$user = $this->discord->users->get('id', $user);
$user = $this->discord()->users->get('id', $user);
}

if ($this->bot->getAdmins()) {
Expand Down
26 changes: 26 additions & 0 deletions src/Commands/ApplicationCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Laracord\Commands;

use Closure;
use Discord\Builders\CommandBuilder;
use Discord\Parts\Interactions\Command\Command;
use Discord\Parts\Interactions\Interaction;
use Discord\Parts\Permissions\RolePermission;

Expand All @@ -25,6 +27,30 @@ abstract class ApplicationCommand extends AbstractCommand
*/
protected bool $nsfw = false;

/**
* Create a Discord command instance.
*/
public function create(): Command
{
$command = CommandBuilder::new()
->setName($this->getName())
->setDescription($this->getDescription())
->setType($this->getType())
->setDmPermission($this->canDirectMessage())
->setNsfw($this->isNsfw());

if ($permissions = $this->getPermissions()) {
$command = $command->setDefaultMemberPermissions($permissions);
}

$command = collect($command->jsonSerialize())
->put('guild_id', $this->getGuild())
->filter()
->all();

return new Command($this->discord(), $command);
}

/**
* Retrieve the slash command bitwise permission.
*/
Expand Down
7 changes: 0 additions & 7 deletions src/Commands/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,6 @@ abstract class Command extends AbstractCommand implements CommandContract
*/
protected $aliases = [];

/**
* The command usage.
*
* @var string
*/
protected $usage = '';

/**
* Maybe handle the command.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/SlashCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function create(): DiscordCommand
}
}

$command = collect($command->toArray())
$command = collect($command->jsonSerialize())
->put('guild_id', $this->getGuild())
->filter()
->all();
Expand Down