diff --git a/app/Services/Discord/Commands/ReleasesCommand.php b/app/Services/Discord/Commands/ReleasesCommand.php index 8d4b3fe..d621999 100644 --- a/app/Services/Discord/Commands/ReleasesCommand.php +++ b/app/Services/Discord/Commands/ReleasesCommand.php @@ -37,12 +37,14 @@ public function exec(array $payload): array public function callback(array $payload): array { $customId = $this->parseCustomId($payload); - $currentPage = $this->getCurrentPage($payload); + $args = $customId['args']; + $page = $args['page'] ?? 1; - return match ($customId['component']) { - self::COMPONENT_PREV_PAGE => $this->getGameReleases($payload, $currentPage - 1), - self::COMPONENT_NEXT_PAGE => $this->getGameReleases($payload, $currentPage + 1), - }; + if (!empty($args['period'])) { + Arr::set($payload, 'data.options.0.value', $args['period']); + } + + return $this->getGameReleases($payload, $page); } /** @@ -115,7 +117,9 @@ private function makeResponse(PaginatedResponse $response, string $period): arra 'embeds' => $this->makeGameEmbeds($response), 'components' => [ $this->makeActionRow( - $this->makePaginationComponents($response) + $this->makePaginationComponents($response, [ + 'period' => $period + ]) ) ] ]; diff --git a/app/Services/Discord/Utils/DiscordCallbackUtils.php b/app/Services/Discord/Utils/DiscordCallbackUtils.php index 6462944..6c2beae 100644 --- a/app/Services/Discord/Utils/DiscordCallbackUtils.php +++ b/app/Services/Discord/Utils/DiscordCallbackUtils.php @@ -10,17 +10,22 @@ trait DiscordCallbackUtils * @param string $name * @return string */ - private function formatCustomId(string $commandName, string $componentName): string - { - if (strpos($commandName, '_') !== false) { - throw new \Exception('Command name can not contain underscores.'); + private function formatCustomId( + string $commandName, + string $componentName, + array $args = [] + ): string { + if (strpos($commandName, '|') !== false) { + throw new \Exception('Command name can not contain pipelines.'); } - if (strpos($componentName, '_') !== false) { - throw new \Exception('Component name can not contain underscores.'); + if (strpos($componentName, '|') !== false) { + throw new \Exception('Component name can not contain pipelines.'); } - return sprintf('%s_%s_%s', $commandName, $componentName, uniqid()); + $args = http_build_query($args); + + return sprintf('%s|%s|%s|%s', $commandName, $componentName, $args, uniqid()); } /** @@ -32,14 +37,17 @@ private function parseCustomId( string $path = 'data.custom_id' ): array { $customId = explode( - '_', + '|', Arr::get($payload, $path) ); + parse_str($customId[2], $args); + return [ 'command' => $customId[0], 'component' => $customId[1], - 'uid' => $customId[2], + 'args' => $args, + 'uid' => $customId[3], ]; } } diff --git a/app/Services/Discord/Utils/DiscordComponentUtils.php b/app/Services/Discord/Utils/DiscordComponentUtils.php index dd8038e..a05f95d 100644 --- a/app/Services/Discord/Utils/DiscordComponentUtils.php +++ b/app/Services/Discord/Utils/DiscordComponentUtils.php @@ -13,17 +13,21 @@ trait DiscordComponentUtils use DiscordCallbackUtils; private const string COMPONENT_GAMES_FOUND = 'games-fround'; - private const string COMPONENT_CURRENT_PAGE = 'current-page-'; + private const string COMPONENT_CURRENT_PAGE = 'current-page'; private const string COMPONENT_NEXT_PAGE = 'next-page'; private const string COMPONENT_PREV_PAGE = 'previous-page'; - private function makeCustomId(string $componentName): string + private function makeCustomId(string $componentName, array $args = []): string { if (!$this instanceof CallbackCommandInterface) { throw new \Exception('Command does not implements CallbackCommandInterface.'); } - return $this->formatCustomId($this->getCommandName(), $componentName); + return $this->formatCustomId( + $this->getCommandName(), + $componentName, + $args + ); } /** @@ -99,11 +103,12 @@ private function makeButtonComponent( string $name, ButtonStyle $style = ButtonStyle::Primary, bool $disabled = false, - string $emoji = '' + string $emoji = '', + array $args = [] ): array { $button = [ 'type' => ComponentType::Button, - 'custom_id' => $this->makeCustomId($name), + 'custom_id' => $this->makeCustomId($name, $args), 'label' => strtoupper($label), 'style' => $style, 'disabled' => $disabled @@ -122,8 +127,10 @@ private function makeButtonComponent( * @param PaginatedResponse $response * @return array */ - private function makePaginationComponents(PaginatedResponse $response): array - { + private function makePaginationComponents( + PaginatedResponse $response, + array $args = [] + ): array { return [ $this->makeButtonComponent( name: self::COMPONENT_GAMES_FOUND, @@ -133,7 +140,7 @@ private function makePaginationComponents(PaginatedResponse $response): array emoji: '🔥' ), $this->makeButtonComponent( - name: self::COMPONENT_CURRENT_PAGE . $response->currentPage, + name: self::COMPONENT_CURRENT_PAGE, label: sprintf('Page %s of %s', $response->currentPage, $response->lastPage), style: ButtonStyle::Secundary, disabled: true, @@ -143,11 +150,13 @@ private function makePaginationComponents(PaginatedResponse $response): array name: self::COMPONENT_PREV_PAGE, label: 'Previous Page', disabled: $response->currentPage === 1, + args: array_merge($args, ['page' => $response->currentPage - 1]) ), $this->makeButtonComponent( name: self::COMPONENT_NEXT_PAGE, label: 'Next Page', disabled: $response->currentPage === $response->lastPage, + args: array_merge($args, ['page' => $response->currentPage + 1]) ) ]; }