Skip to content

Commit

Permalink
Merge branch 'master' into feat/message-components
Browse files Browse the repository at this point in the history
  • Loading branch information
Exanlv committed Mar 17, 2023
2 parents 03432ec + 6fa6ec1 commit ceb46cf
Show file tree
Hide file tree
Showing 38 changed files with 196 additions and 42 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 - Present Ragnarök

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
2 changes: 1 addition & 1 deletion examples/01-ping-pong-bot.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

use Exan\Fenrir\Bitwise\Bitwise;
use Exan\Fenrir\Const\Events;
use Exan\Fenrir\Constants\Events;
use Exan\Fenrir\Discord;
use Exan\Fenrir\Enums\Gateway\Intents;
use Exan\Fenrir\Rest\Helpers\Channel\MessageBuilder;
Expand Down
2 changes: 1 addition & 1 deletion examples/02-listening-to-raw-events.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ While Fenrir should cover most Events out of the box, you can also opt to listen

You can enable the emitting of raw events by passing the setting in the `option` param of the `Exan\Fenrir\Discord` constructor.

You can then set a listener for these events using `Exan\Fenrir\Const\Events::raw`. The handler receives a `Exan\Fenrir\Websocket\Objects\Payload` as its only parameter.
You can then set a listener for these events using `Exan\Fenrir\Constants\Events::raw`. The handler receives a `Exan\Fenrir\Websocket\Objects\Payload` as its only parameter.
2 changes: 1 addition & 1 deletion examples/02-listening-to-raw-events.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

use Exan\Fenrir\Bitwise\Bitwise;
use Exan\Fenrir\Const\Events;
use Exan\Fenrir\Constants\Events;
use Exan\Fenrir\Discord;
use Exan\Fenrir\Websocket\Objects\Payload;

Expand Down
2 changes: 1 addition & 1 deletion examples/04-async-in-order.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

use Exan\Fenrir\Bitwise\Bitwise;
use Exan\Fenrir\Const\Events;
use Exan\Fenrir\Constants\Events;
use Exan\Fenrir\Discord;
use Exan\Fenrir\Enums\Gateway\Intents;
use Exan\Fenrir\Rest\Helpers\Channel\MessageBuilder;
Expand Down
2 changes: 1 addition & 1 deletion examples/05-filtered-event-listener.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

use Exan\Fenrir\Bitwise\Bitwise;
use Exan\Fenrir\Const\Events;
use Exan\Fenrir\Constants\Events;
use Exan\Fenrir\Discord;
use Exan\Fenrir\Enums\Gateway\Intents;
use Exan\Fenrir\FilteredEventEmitter;
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Fenrir heavily relies on ReactPHP for async operations. Knowing the basics of as

```php
use Exan\Fenrir\Bitwise\Bitwise;
use Exan\Fenrir\Const\Events;
use Exan\Fenrir\Constants\Events;
use Exan\Fenrir\Discord;
use Exan\Fenrir\Enums\Gateway\Intents;
use Exan\Fenrir\Rest\Helpers\Channel\MessageBuilder;
Expand Down
97 changes: 97 additions & 0 deletions src/CommandHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

declare(strict_types=1);

namespace Exan\Fenrir;

use Exan\Fenrir\Command\FiredCommand;
use Exan\Fenrir\Constants\Events;
use Exan\Fenrir\Parts\ApplicationCommand;
use Exan\Fenrir\Rest\Helpers\Command\CommandBuilder;
use Exan\Fenrir\Websocket\Events\InteractionCreate;
use Exan\Fenrir\Websocket\Events\Ready;

class CommandHandler
{
private bool $activated = false;

/** @var array<string, callable> */
private array $commands = [];

private bool $devMode = false;

public function __construct(private Discord $discord, private ?string $devGuildId = null)
{
if (!is_null($this->devGuildId)) {
$this->devMode = true;
}
}

public function registerCommand(CommandBuilder $commandBuilder, callable $handler): void
{
if ($this->devMode) {
$this->registerGuildCommand($commandBuilder, $this->devGuildId, $handler);
} else {
$this->registerGlobalCommand($commandBuilder, $handler);
}
}

public function registerGuildCommand(CommandBuilder $commandBuilder, string $guildId, callable $handler): void
{
$this->activateListener();

/** Ready event includes Application ID */
$this->discord->gateway->events->once(
Events::READY,
function (Ready $ready) use ($commandBuilder, $guildId, $handler) {
$this->discord->rest->guildCommand->createApplicationCommand(
$ready->user->id,
$guildId,
$commandBuilder
)->then(
fn (ApplicationCommand $applicationCommand) => $this->commands[$applicationCommand->id] = $handler
);
}
);
}

public function registerGlobalCommand(CommandBuilder $commandBuilder, callable $handler): void
{
$this->activateListener();

/** Ready event includes Application ID */
$this->discord->gateway->events->once(Events::READY, function (Ready $ready) use ($commandBuilder, $handler) {
$this->discord->rest->globalCommand->createApplicationCommand(
$ready->user->id,
$commandBuilder
)->then(
fn (ApplicationCommand $applicationCommand) => $this->commands[$applicationCommand->id] = $handler
);
});
}

private function activateListener()
{
if ($this->activated) {
return;
}

$this->activated = true;

$this->discord->gateway->events->on(
Events::INTERACTION_CREATE,
$this->handleInteraction(...)
);
}

private function handleInteraction(InteractionCreate $interactionCreate)
{
if (!isset($this->commands[$interactionCreate->data->id])) {
return;
}

$firedCommand = new FiredCommand($interactionCreate, $this->discord);

$this->commands[$interactionCreate->data->id]($firedCommand);
}
}
2 changes: 1 addition & 1 deletion src/Const/Events.php → src/Constants/Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Exan\Fenrir\Const;
namespace Exan\Fenrir\Constants;

class Events
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Exan\Fenrir\Const\Validation;
namespace Exan\Fenrir\Constants\Validation;

class Command
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

declare(strict_types=1);

namespace Exan\Fenrir\Const\Validation;
namespace Exan\Fenrir\Constants\Validation;

use Exan\Fenrir\Const\Validation\Traits\WithinLimit;
use Exan\Fenrir\Constants\Validation\Traits\WithinLimit;

class ItemLimit
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

declare(strict_types=1);

namespace Exan\Fenrir\Const\Validation;
namespace Exan\Fenrir\Constants\Validation;

use Exan\Fenrir\Const\Validation\Traits\WithinLimit;
use Exan\Fenrir\Constants\Validation\Traits\WithinLimit;

class RateLimit
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Exan\Fenrir\Const\Validation\Traits;
namespace Exan\Fenrir\Constants\Validation\Traits;

trait WithinLimit
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Exan\Fenrir\Const;
namespace Exan\Fenrir\Constants;

class WebsocketEvents
{
Expand Down
2 changes: 1 addition & 1 deletion src/EventHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Exan\Fenrir;

use Evenement\EventEmitter;
use Exan\Fenrir\Const\Events;
use Exan\Fenrir\Constants\Events;
use Exan\Fenrir\Websocket\Objects\Payload;
use JsonMapper;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Exan\Fenrir\Exceptions\Rest\Helpers\Command;

use Exan\Fenrir\Const\Validation\Command;
use Exan\Fenrir\Constants\Validation\Command;
use Exception;

class InvalidCommandNameException extends Exception
Expand Down
4 changes: 2 additions & 2 deletions src/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
namespace Exan\Fenrir;

use Exan\Fenrir\Bitwise\Bitwise;
use Exan\Fenrir\Const\Events as Events;
use Exan\Fenrir\Const\WebsocketEvents;
use Exan\Fenrir\Constants\Events as Events;
use Exan\Fenrir\Constants\WebsocketEvents;
use Exan\Fenrir\Enums\Gateway\StatusType;
use Exan\Fenrir\Websocket\Helpers\ActivityBuilder;
use Exan\Fenrir\Websocket\Objects\D\Hello;
Expand Down
2 changes: 1 addition & 1 deletion src/Rest/Helpers/AuditLog/GetGuildAuditLogsBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Exan\Fenrir\Rest\Helpers\AuditLog;

use Exan\Fenrir\Const\Validation\ItemLimit;
use Exan\Fenrir\Constants\Validation\ItemLimit;
use Exan\Fenrir\Rest\Helpers\GetNew;

class GetGuildAuditLogsBuilder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Exan\Fenrir\Rest\Helpers\Channel\Channel\Shared;

use Exan\Fenrir\Const\Validation\RateLimit;
use Exan\Fenrir\Constants\Validation\RateLimit;

trait SetDefaultThreadRateLimitPerUser
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Exan\Fenrir\Rest\Helpers\Channel\Channel\Shared;

use Exan\Fenrir\Const\Validation\RateLimit;
use Exan\Fenrir\Constants\Validation\RateLimit;

trait SetRateLimitPerUser
{
Expand Down
2 changes: 1 addition & 1 deletion src/Rest/Helpers/Channel/GetMessagesBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Exan\Fenrir\Rest\Helpers\Channel;

use Exan\Fenrir\Const\Validation\ItemLimit;
use Exan\Fenrir\Constants\Validation\ItemLimit;
use Exan\Fenrir\Rest\Helpers\GetNew;

class GetMessagesBuilder
Expand Down
2 changes: 1 addition & 1 deletion src/Rest/Helpers/Channel/GetReactionsBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Exan\Fenrir\Rest\Helpers\Channel;

use Exan\Fenrir\Const\Validation\ItemLimit;
use Exan\Fenrir\Constants\Validation\ItemLimit;
use Exan\Fenrir\Rest\Helpers\GetNew;

class GetReactionsBuilder
Expand Down
2 changes: 1 addition & 1 deletion src/Rest/Helpers/Channel/StartThreadFromMessageBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Exan\Fenrir\Rest\Helpers\Channel;

use Exan\Fenrir\Const\Validation\RateLimit;
use Exan\Fenrir\Constants\Validation\RateLimit;
use Exan\Fenrir\Enums\Parts\ThreadAutoArchiveDuration;
use Exan\Fenrir\Rest\Helpers\GetNew;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Exan\Fenrir\Rest\Helpers\Channel;

use Exan\Fenrir\Const\Validation\RateLimit;
use Exan\Fenrir\Constants\Validation\RateLimit;
use Exan\Fenrir\Enums\Parts\ChannelTypes;
use Exan\Fenrir\Enums\Parts\ThreadAutoArchiveDuration;
use Exan\Fenrir\Rest\Helpers\GetNew;
Expand Down
2 changes: 1 addition & 1 deletion src/Rest/Helpers/Command/CommandBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Exan\Fenrir\Rest\Helpers\Command;

use Exan\Fenrir\Bitwise\Bitwise;
use Exan\Fenrir\Const\Validation\Command;
use Exan\Fenrir\Constants\Validation\Command;
use Exan\Fenrir\Enums\Parts\ApplicationCommandTypes;
use Exan\Fenrir\Exceptions\Rest\Helpers\Command\InvalidCommandNameException;
use Exan\Fenrir\Rest\Helpers\GetNew;
Expand Down
17 changes: 13 additions & 4 deletions src/Websocket.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
namespace Exan\Fenrir;

use Evenement\EventEmitter;
use Exan\Fenrir\Const\WebsocketEvents;
use Exan\Fenrir\Constants\WebsocketEvents;
use Exan\Fenrir\Exceptions\Websocket\ConnectionNotInitializedException;
use JsonSerializable;
use Psr\Log\LoggerInterface;
use Ratchet\Client\Connector;
use Ratchet\Client\WebSocket as RatchetWebsocket;
Expand Down Expand Up @@ -43,7 +44,7 @@ public function __construct(private int $timeout, private LoggerInterface $logge
/**
* @throws ConnectionNotInitializedException
*/
private function mustHaveActiveConnection()
private function mustHaveActiveConnection(): void
{
if (!isset($this->connection)) {
throw new ConnectionNotInitializedException();
Expand Down Expand Up @@ -83,7 +84,7 @@ public function open(string $url): Promise
/**
* @throws ConnectionNotInitializedException
*/
public function close(int $code, string $reason)
public function close(int $code, string $reason): void
{
$this->mustHaveActiveConnection();

Expand All @@ -99,7 +100,7 @@ public function close(int $code, string $reason)
/**
* @throws ConnectionNotInitializedException
*/
public function send(string $message, bool $useBucket = true)
public function send(string $message, bool $useBucket = true): void
{
$this->mustHaveActiveConnection();

Expand All @@ -120,4 +121,12 @@ public function send(string $message, bool $useBucket = true)
$action();
}
}

/**
* @throws ConnectionNotInitializedException
*/
public function sendAsJson(array|JsonSerializable $item, bool $useBucket): void
{
$this->send(json_encode($item), $useBucket);
}
}
2 changes: 1 addition & 1 deletion tests/EventHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Tests\Exan\Fenrir;

use Exan\Fenrir\Const\Events;
use Exan\Fenrir\Constants\Events;
use Exan\Fenrir\EventHandler;
use Exan\Fenrir\Websocket\Objects\Payload;
use JsonMapper;
Expand Down
Loading

0 comments on commit ceb46cf

Please sign in to comment.