-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Add support for Context Menus (#106)
- Loading branch information
1 parent
cee289e
commit 92b1527
Showing
12 changed files
with
563 additions
and
115 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
namespace Laracord\Commands; | ||
|
||
abstract class ApplicationCommand extends AbstractCommand | ||
{ | ||
/** | ||
* The permissions required to use the command. | ||
* | ||
* @var array | ||
*/ | ||
protected $permissions = []; | ||
|
||
/** | ||
* Determine if the command is not safe for work. | ||
*/ | ||
protected bool $nsfw = false; | ||
|
||
/** | ||
* Retrieve the slash command bitwise permission. | ||
*/ | ||
public function getPermissions(): ?string | ||
{ | ||
if (! $this->permissions) { | ||
return null; | ||
} | ||
|
||
$permissions = collect($this->permissions) | ||
->mapWithKeys(fn ($permission) => [$permission => true]) | ||
->all(); | ||
|
||
return (new RolePermission($this->discord(), $permissions))->__toString(); | ||
} | ||
|
||
/** | ||
* Determine if the command is not safe for work. | ||
*/ | ||
public function isNsfw(): bool | ||
{ | ||
return $this->nsfw; | ||
} | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
|
||
namespace Laracord\Commands; | ||
|
||
use Discord\Parts\Interactions\Command\Command as DiscordCommand; | ||
use Laracord\Commands\Contracts\ContextMenu as ContextMenuContract; | ||
|
||
abstract class ContextMenu extends ApplicationCommand implements ContextMenuContract | ||
{ | ||
/** | ||
* The context menu type. | ||
*/ | ||
protected string|int $type = 'message'; | ||
|
||
/** | ||
* Create a Discord command instance. | ||
*/ | ||
public function create(): DiscordCommand | ||
{ | ||
$menu = collect([ | ||
'name' => $this->getName(), | ||
'type' => $this->getType(), | ||
'guild_id' => $this->getGuild(), | ||
'default_member_permissions' => $this->getPermissions(), | ||
'default_permission' => true, | ||
'dm_permission' => $this->canDirectMessage(), | ||
'nsfw' => $this->isNsfw(), | ||
])->reject(fn ($value) => blank($value)); | ||
|
||
return new DiscordCommand($this->discord(), $menu->all()); | ||
} | ||
|
||
/** | ||
* Handle the context menu interaction. | ||
* | ||
* @param \Discord\Parts\Interactions\Interaction $interaction | ||
* @return void | ||
*/ | ||
abstract public function handle($interaction); | ||
|
||
/** | ||
* Maybe handle the context menu interaction. | ||
* | ||
* @param \Discord\Parts\Interactions\Interaction $interaction | ||
* @return void | ||
*/ | ||
public function maybeHandle($interaction) | ||
{ | ||
if (! $this->isAdminCommand()) { | ||
$this->handle($interaction); | ||
|
||
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->handle($interaction); | ||
} | ||
|
||
/** | ||
* Get the context menu type. | ||
*/ | ||
public function getType(): int | ||
{ | ||
return match ($this->type) { | ||
'user' => DiscordCommand::USER, | ||
DiscordCommand::USER => DiscordCommand::USER, | ||
default => DiscordCommand::MESSAGE, | ||
}; | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace Laracord\Commands\Contracts; | ||
|
||
use Discord\Parts\Interactions\Interaction; | ||
|
||
interface ContextMenu | ||
{ | ||
/** | ||
* Handle the context menu interaction. | ||
*/ | ||
public function handle(Interaction $interaction); | ||
} |
This file contains 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
Oops, something went wrong.