From 0c434065e3cd5ce0813a5a1e55d40fe0a6b81ad8 Mon Sep 17 00:00:00 2001 From: Exanlv <51094537+Exanlv@users.noreply.github.com> Date: Thu, 15 Jun 2023 09:56:22 +0200 Subject: [PATCH] chore: add (basic) guild bindings * Add Guild bindings * Fix namespace & composer issue * Add guild traits * Add bindings and tests * Add get bans & get ban calls * Add member role management calls * Add more bindings * More guild bindings * Add bindings * Add binding * still a lot of bindings to go woo * Guild bindings almost done :D * cs & md * Add last of the guild bindings * Add missing strict type --------- Co-authored-by: Exan --- composer.json | 4 +- src/Enums/DefaultMessageNotifications.php | 11 + src/Parts/ActiveGuildThreads.php | 18 + src/Parts/GuildBan.php | 11 + src/Parts/GuildPreview.php | 37 + src/Parts/PruneCount.php | 10 + src/Parts/Widget.php | 23 + src/Parts/WidgetSettings.php | 11 + src/Rest/Guild.php | 850 ++++++++++++++++++ .../Guild/Guild/Shared/SetAfkChannelId.php | 20 + .../Guild/Guild/Shared/SetAfkTimeout.php | 23 + .../Shared/SetDefaultMessageNotifications.php | 24 + .../Guild/Shared/SetExplicitContentFilter.php | 24 + .../Helpers/Guild/Guild/Shared/SetIcon.php | 22 + .../Helpers/Guild/Guild/Shared/SetName.php | 20 + .../Guild/Shared/SetSystemChannelFlags.php | 27 + .../Guild/Guild/Shared/SetSystemChannelId.php | 20 + .../Guild/Shared/SetVerificationLevel.php | 24 + .../Guild/ModifyChannelPositionsBuilder.php | 67 ++ src/Rest/Rest.php | 3 + tests/Rest/GuildTest.php | 125 +++ .../ModifyChannelPositionsBuilderTest.php | 59 ++ 22 files changed, 1431 insertions(+), 2 deletions(-) create mode 100644 src/Enums/DefaultMessageNotifications.php create mode 100644 src/Parts/ActiveGuildThreads.php create mode 100644 src/Parts/GuildBan.php create mode 100644 src/Parts/GuildPreview.php create mode 100644 src/Parts/PruneCount.php create mode 100644 src/Parts/Widget.php create mode 100644 src/Parts/WidgetSettings.php create mode 100644 src/Rest/Guild.php create mode 100644 src/Rest/Helpers/Guild/Guild/Shared/SetAfkChannelId.php create mode 100644 src/Rest/Helpers/Guild/Guild/Shared/SetAfkTimeout.php create mode 100644 src/Rest/Helpers/Guild/Guild/Shared/SetDefaultMessageNotifications.php create mode 100644 src/Rest/Helpers/Guild/Guild/Shared/SetExplicitContentFilter.php create mode 100644 src/Rest/Helpers/Guild/Guild/Shared/SetIcon.php create mode 100644 src/Rest/Helpers/Guild/Guild/Shared/SetName.php create mode 100644 src/Rest/Helpers/Guild/Guild/Shared/SetSystemChannelFlags.php create mode 100644 src/Rest/Helpers/Guild/Guild/Shared/SetSystemChannelId.php create mode 100644 src/Rest/Helpers/Guild/Guild/Shared/SetVerificationLevel.php create mode 100644 src/Rest/Helpers/Guild/ModifyChannelPositionsBuilder.php create mode 100644 tests/Rest/GuildTest.php create mode 100644 tests/Rest/Helpers/Guild/ModifyChannelPositionsBuilderTest.php diff --git a/composer.json b/composer.json index 659c60c5..156a1769 100644 --- a/composer.json +++ b/composer.json @@ -45,8 +45,8 @@ "phpunit": "phpunit", "phpunit-coverage": "@php -d xdebug.mode=coverage ./vendor/bin/phpunit --coverage-html .phpunit.cache/cov-html", - "test": "@php tests/WebsocketTestServer.php & PID=$!; composer phpunit; kill $PID;", - "test-coverage": "@php tests/WebsocketTestServer.php & PID=$!; composer phpunit-coverage; kill $PID;" + "test": "php tests/WebsocketTestServer.php & PID=$!; composer phpunit; kill $PID;", + "test-coverage": "php tests/WebsocketTestServer.php & PID=$!; composer phpunit-coverage; kill $PID;" }, "config": { "allow-plugins": { diff --git a/src/Enums/DefaultMessageNotifications.php b/src/Enums/DefaultMessageNotifications.php new file mode 100644 index 00000000..2878e184 --- /dev/null +++ b/src/Enums/DefaultMessageNotifications.php @@ -0,0 +1,11 @@ +features = []; + + foreach ($value as $entry) { + $this->features[] = GuildFeatures::from($entry); + } + } +} diff --git a/src/Parts/PruneCount.php b/src/Parts/PruneCount.php new file mode 100644 index 00000000..f0da871a --- /dev/null +++ b/src/Parts/PruneCount.php @@ -0,0 +1,10 @@ + + * + * @todo Convert to builder + */ + public function create(array $params): ExtendedPromiseInterface + { + return $this->mapPromise( + $this->http->post( + Endpoint::GUILDS, + $params + ), + PartsGuild::class, + )->otherwise($this->logThrowable(...)); + } + + /** + * @see https://discord.com/developers/docs/resources/guild#get-guild + * + * @return ExtendedPromiseInterface<\Ragnarok\Fenrir\Parts\Guild> + */ + public function get(string $guildId): ExtendedPromiseInterface + { + return $this->mapPromise( + $this->http->get( + Endpoint::bind( + Endpoint::GUILD, + $guildId + ) + ), + PartsGuild::class + )->otherwise($this->logThrowable(...)); + } + + /** + * @see https://discord.com/developers/docs/resources/guild#get-guild-preview + * + * @return ExtendedPromiseInterface<\Ragnarok\Fenrir\Parts\GuildPreview> + */ + public function getPreview(string $guildId): ExtendedPromiseInterface + { + return $this->mapPromise( + $this->http->get( + Endpoint::bind( + Endpoint::GUILD_PREVIEW, + $guildId + ) + ), + GuildPreview::class + )->otherwise($this->logThrowable(...)); + } + + /** + * @see https://discord.com/developers/docs/resources/guild#modify-guild + * + * @return ExtendedPromiseInterface<\Ragnarok\Fenrir\Parts\Guild> + * + * @todo Convert to builder + */ + public function modify(string $guildId, array $params, ?string $reason = null): ExtendedPromiseInterface + { + return $this->mapPromise( + $this->http->patch( + Endpoint::bind( + Endpoint::GUILD, + $guildId + ), + $params, + $this->getAuditLogReasonHeader($reason) + ), + PartsGuild::class + )->otherwise($this->logThrowable(...)); + } + + /** + * @see https://discord.com/developers/docs/resources/guild#delete-guild + * + * @return ExtendedPromiseInterface + */ + public function delete(string $guildId): ExtendedPromiseInterface + { + return $this->http->delete( + Endpoint::bind( + Endpoint::GUILD, + $guildId + ) + )->otherwise($this->logThrowable(...)); + } + + /** + * @see https://discord.com/developers/docs/resources/guild#get-guild-channels + * + * @return ExtendedPromiseInterface<\Ragnarok\Fenrir\Parts\Channel[]> + */ + public function getChannels(string $guildId): ExtendedPromiseInterface + { + return $this->mapArrayPromise( + $this->http->get( + Endpoint::bind( + Endpoint::GUILD_CHANNELS, + $guildId + ) + ), + Channel::class + )->otherwise($this->logThrowable(...)); + } + + /** + * @see https://discord.com/developers/docs/resources/guild#create-guild-channel + * + * @return ExtendedPromiseInterface<\Ragnarok\Fenrir\Parts\Channel> + * + * @todo Convert to builder + */ + public function createChannel(string $guildId, array $params, ?string $reason = null): ExtendedPromiseInterface + { + return $this->mapArrayPromise( + $this->http->post( + Endpoint::bind( + Endpoint::GUILD_CHANNELS, + $guildId + ), + $params, + $this->getAuditLogReasonHeader($reason) + ), + Channel::class + )->otherwise($this->logThrowable(...)); + } + + /** + * @param ModifyChannelPositionsBuilder[] $modifyChannelPositionsBuilders + * + * @return ExtendedPromiseInterface + */ + public function modifyChannelPositions( + string $guildId, + array $modifyChannelPositionsBuilders + ): ExtendedPromiseInterface { + return $this->http->patch( + Endpoint::bind( + Endpoint::GUILD_CHANNELS, + $guildId, + ), + array_map(fn (ModifyChannelPositionsBuilder $builder) => $builder->get(), $modifyChannelPositionsBuilders) + )->otherwise($this->logThrowable(...)); + } + + /** + * @see https://discord.com/developers/docs/resources/guild#list-active-guild-threads + * + * @return ExtendedPromiseInterface<\Ragnarok\Fenrir\Parts\ActiveGuildThreads> + */ + public function listActiveThreads(string $guildId): ExtendedPromiseInterface + { + return $this->mapPromise( + $this->http->get( + Endpoint::bind( + Endpoint::GUILD_THREADS_ACTIVE, + $guildId, + ) + ), + ActiveGuildThreads::class, + ); + } + + /** + * @see https://discord.com/developers/docs/resources/guild#get-guild-member + * + * @return ExtendedPromiseInterface<\Ragnarok\Fenrir\Parts\GuildMember> + */ + public function getMember(string $guildId, string $memberId): ExtendedPromiseInterface + { + return $this->mapPromise( + $this->http->get( + Endpoint::bind( + Endpoint::GUILD_MEMBER, + $guildId, + $memberId + ), + ), + GuildMember::class + )->otherwise($this->logThrowable(...)); + } + + /** + * @see https://discord.com/developers/docs/resources/guild#list-guild-members + * + * @return ExtendedPromiseInterface<\Ragnarok\Fenrir\Parts\GuildMember[]> + * + * @todo Convert to builder + */ + public function listMembers(string $guildId, array $queryParams): ExtendedPromiseInterface + { + $endpoint = Endpoint::bind( + Endpoint::GUILD_MEMBERS_SEARCH, + $guildId, + ); + + foreach ($queryParams as $key => $value) { + $endpoint->addQuery($key, $value); + } + + return $this->mapArrayPromise( + $this->http->get($endpoint), + GuildMember::class + )->otherwise($this->logThrowable(...)); + } + + /** + * @see https://discord.com/developers/docs/resources/guild#search-guild-members + * + * @return ExtendedPromiseInterface<\Ragnarok\Fenrir\Parts\GuildMember[]> + */ + public function searchMembers(string $guildId, array $queryParams): ExtendedPromiseInterface + { + $endpoint = Endpoint::bind( + Endpoint::GUILD_MEMBERS_SEARCH, + $guildId, + ); + + foreach ($queryParams as $key => $value) { + $endpoint->addQuery($key, $value); + } + + return $this->mapArrayPromise( + $this->http->get($endpoint), + GuildMember::class + )->otherwise($this->logThrowable(...)); + } + + /** + * @see https://discord.com/developers/docs/resources/guild#add-guild-member + * + * @return ExtendedPromiseInterface<\Ragnarok\Fenrir\Parts\GuildMember> + */ + public function addMember(string $guildId, string $userId, array $params): ExtendedPromiseInterface + { + return $this->mapPromise( + $this->http->put( + Endpoint::bind( + Endpoint::GUILD_MEMBER, + $guildId, + $userId, + ), + $params, + ), + GuildMember::class, + )->otherwise($this->logThrowable(...)); + } + + /** + * @see https://discord.com/developers/docs/resources/guild#modify-guild-member + * + * @return ExtendedPromiseInterface + */ + public function modifyMember( + string $guildId, + string $userId, + array $params, + ?string $reason = null + ): ExtendedPromiseInterface { + return $this->http->patch( + Endpoint::bind( + Endpoint::GUILD_MEMBER, + $guildId, + $userId, + ), + $params, + $this->getAuditLogReasonHeader($reason), + )->otherwise($this->logThrowable(...)); + } + + /** + * @see https://discord.com/developers/docs/resources/guild#modify-current-member + * + * @return ExtendedPromiseInterface + */ + public function modifyCurrentMember( + string $guildId, + array $params, + ?string $reason = null + ): ExtendedPromiseInterface { + return $this->http->patch( + Endpoint::bind( + Endpoint::GUILD_MEMBER_SELF, + $guildId, + ), + $params, + $this->getAuditLogReasonHeader($reason), + )->otherwise($this->logThrowable(...)); + } + + /** + * @see https://discord.com/developers/docs/resources/guild#add-guild-member-role + * + * @return ExtendedPromiseInterface + */ + public function addMemberRole( + string $guildId, + string $userId, + string $roleId, + ?string $reason = null + ): ExtendedPromiseInterface { + return $this->http->put( + Endpoint::bind( + Endpoint::GUILD_MEMBER_ROLE, + $guildId, + $userId, + $roleId, + ), + headers: $this->getAuditLogReasonHeader($reason), + )->otherwise($this->logThrowable(...)); + } + + /** + * @see https://discord.com/developers/docs/resources/guild#remove-guild-member-role + * + * @return ExtendedPromiseInterface + */ + public function removeMemberRole( + string $guildId, + string $userId, + string $roleId, + ?string $reason = null + ): ExtendedPromiseInterface { + return $this->http->delete( + Endpoint::bind( + Endpoint::GUILD_MEMBER_ROLE, + $guildId, + $userId, + $roleId, + ), + headers: $this->getAuditLogReasonHeader($reason), + )->otherwise($this->logThrowable(...)); + } + + /** + * @see https://discord.com/developers/docs/resources/guild#remove-guild-member + * + * @return ExtendedPromiseInterface + */ + public function removeGuildMember(string $guildId, string $userId, ?string $reason = null): ExtendedPromiseInterface + { + return $this->http->delete( + Endpoint::bind( + Endpoint::GUILD_MEMBER, + $guildId, + $userId, + ), + headers: $this->getAuditLogReasonHeader($reason), + )->otherwise($this->logThrowable(...)); + } + + /** + * @see https://discord.com/developers/docs/resources/guild#get-guild-bans + * + * @return ExtendedPromiseInterface<\Ragnarok\Fenrir\Parts\GuildBan[]> + */ + public function getBans(string $guildId): ExtendedPromiseInterface + { + return $this->mapArrayPromise( + $this->http->get( + Endpoint::bind( + Endpoint::GUILD_BANS, + $guildId, + ) + ), + GuildBan::class + )->otherwise($this->logThrowable(...)); + } + + /** + * @see https://discord.com/developers/docs/resources/guild#get-guild-ban + * + * @return ExtendedPromiseInterface<\Ragnarok\Fenrir\Parts\GuildBan[]> + */ + public function getBan(string $guildId, string $userId): ExtendedPromiseInterface + { + return $this->mapPromise( + $this->http->get( + Endpoint::bind( + Endpoint::GUILD_BAN, + $guildId, + $userId + ) + ), + GuildBan::class + )->otherwise($this->logThrowable(...)); + } + + /** + * @see https://discord.com/developers/docs/resources/guild#create-guild-ban + * + * @return ExtendedPromiseInterface + */ + public function createBan( + string $guildId, + string $userId, + array $params, + ?string $reason = null + ): ExtendedPromiseInterface { + return $this->http->put( + Endpoint::bind( + Endpoint::GUILD_BAN, + $guildId, + $userId, + ), + $params, + $this->getAuditLogReasonHeader($reason) + )->otherwise($this->logThrowable(...)); + } + + /** + * @see https://discord.com/developers/docs/resources/guild#remove-guild-ban + * + * @return ExtendedPromiseInterface + */ + public function removeBan(string $guildId, string $userId, ?string $reason = null): ExtendedPromiseInterface + { + return $this->http->delete( + Endpoint::bind( + Endpoint::GUILD_BAN, + $guildId, + $userId, + ), + headers: $this->getAuditLogReasonHeader($reason) + )->otherwise($this->logThrowable(...)); + } + + /** + * @see https://discord.com/developers/docs/resources/guild#get-guild-roles + * + * @return ExtendedPromiseInterface<\Ragnarok\Fenrir\Parts\Role[]> + */ + public function getRoles(string $guildId): ExtendedPromiseInterface + { + return $this->mapArrayPromise( + $this->http->get( + Endpoint::bind( + Endpoint::GUILD_ROLES, + $guildId + ) + ), + Role::class, + )->otherwise($this->logThrowable(...)); + } + + /** + * @see https://discord.com/developers/docs/resources/guild#create-guild-role + * + * @return ExtendedPromiseInterface<\Ragnarok\Fenrir\Parts\Role> + */ + public function createRole(string $guildId, array $params, ?string $reason = null): ExtendedPromiseInterface + { + return $this->mapPromise( + $this->http->post( + Endpoint::bind( + Endpoint::GUILD_ROLES, + $guildId + ), + $params, + $this->getAuditLogReasonHeader($reason), + ), + Role::class, + )->otherwise($this->logThrowable(...)); + } + + /** + * @see https://discord.com/developers/docs/resources/guild#modify-guild-role-positions + * + * @return ExtendedPromiseInterface<\Ragnarok\Fenrir\Parts\Role[]> + */ + public function modifyRolePositions( + string $guildId, + array $params, + ?string $reason = null + ): ExtendedPromiseInterface { + return $this->mapArrayPromise( + $this->http->patch( + Endpoint::bind( + Endpoint::GUILD_ROLES, + $guildId, + ), + $params, + $this->getAuditLogReasonHeader($reason), + ), + Role::class, + )->otherwise($this->logThrowable(...)); + } + + /** + * @see https://discord.com/developers/docs/resources/guild#modify-guild-role + * + * @return ExtendedPromiseInterface<\Ragnarok\Fenrir\Parts\Role> + */ + public function modifyRole( + string $guildId, + string $roleId, + array $params, + ?string $reason = null + ): ExtendedPromiseInterface { + return $this->mapPromise( + $this->http->patch( + Endpoint::bind( + Endpoint::GUILD_ROLE, + $guildId, + $roleId + ), + $params, + $this->getAuditLogReasonHeader($reason), + ), + Role::class, + )->otherwise($this->logThrowable(...)); + } + + /** + * @see https://discord.com/developers/docs/resources/guild#modify-guild-mfa-level + * + * @return ExtendedPromiseInterface + */ + public function modifyMfaLevel( + string $guildId, + MfaLevels $mfaLevel, + ?string $reason = null + ): ExtendedPromiseInterface { + return $this->http->post( + Endpoint::bind( + Endpoint::GUILD_MFA, + $guildId, + ), + ['level' => $mfaLevel->value], + $this->getAuditLogReasonHeader($reason), + )->otherwise($this->logThrowable(...)); + } + + /** + * @see https://discord.com/developers/docs/resources/guild#delete-guild-role + * + * @return ExtendedPromiseInterface + */ + public function deleteRole(string $guildId, string $roleId, ?string $reason = null): ExtendedPromiseInterface + { + return $this->http->delete( + Endpoint::bind( + Endpoint::GUILD_ROLE, + $guildId, + $roleId, + ), + headers: $this->getAuditLogReasonHeader($reason), + )->otherwise($this->logThrowable(...)); + } + + /** + * @see https://discord.com/developers/docs/resources/guild#get-guild-prune-count + * + * @return ExtendedPromiseInterface<\Ragnarok\Fenrir\Parts\PruneCount> + */ + public function getPruneCount(string $guildId, array $queryParams): ExtendedPromiseInterface + { + $endpoint = Endpoint::bind( + Endpoint::GUILD_PRUNE, + $guildId, + ); + + foreach ($queryParams as $key => $value) { + $endpoint->addQuery($key, $value); + } + + return $this->mapPromise( + $this->http->get( + $endpoint, + ), + PruneCount::class, + )->otherwise($this->logThrowable(...)); + } + + /** + * @see https://discord.com/developers/docs/resources/guild#begin-guild-prune + * + * @return ExtendedPromiseInterface<\Ragnarok\Fenrir\Parts\PruneCount> + */ + public function beginPrune(string $guildId, array $params, ?string $reason = null): ExtendedPromiseInterface + { + return $this->mapPromise( + $this->http->post( + Endpoint::bind( + Endpoint::GUILD_PRUNE, + $guildId, + ), + $params, + headers: $this->getAuditLogReasonHeader($reason) + ), + PruneCount::class, + )->otherwise($this->logThrowable(...)); + } + + /** + * @see https://discord.com/developers/docs/resources/guild#get-guild-voice-regions + * + * @return ExtendedPromiseInterface<\Ragnarok\Fenrir\Parts\VoiceRegion> + */ + public function getVoiceRegions(string $guildId): ExtendedPromiseInterface + { + return $this->mapArrayPromise( + $this->http->get( + Endpoint::bind( + Endpoint::GUILD_REGIONS, + $guildId, + ), + ), + VoiceRegion::class + )->otherwise($this->logThrowable(...)); + } + + /** + * @see https://discord.com/developers/docs/resources/guild#get-guild-invites + * + * @return ExtendedPromiseInterface<\Ragnarok\Fenrir\Parts\Invite> + */ + public function getInvites(string $guildId): ExtendedPromiseInterface + { + return $this->mapArrayPromise( + $this->http->get( + Endpoint::bind( + Endpoint::GUILD_REGIONS, + $guildId, + ), + ), + Invite::class + )->otherwise($this->logThrowable(...)); + } + + /** + * @see https://discord.com/developers/docs/resources/guild#get-guild-integrations + * + * @return ExtendedPromiseInterface<\Ragnarok\Fenrir\Parts\Integration> + */ + public function getIntegrations(string $guildId): ExtendedPromiseInterface + { + return $this->mapArrayPromise( + $this->http->get( + Endpoint::bind( + Endpoint::GUILD_INTEGRATIONS, + $guildId, + ), + ), + Integration::class + )->otherwise($this->logThrowable(...)); + } + + /** + * @see https://discord.com/developers/docs/resources/guild#delete-guild-integration + * + * @return ExtendedPromiseInterface + */ + public function deleteIntegration( + string $guildId, + string $integrationId, + ?string $reason = null + ): ExtendedPromiseInterface { + return $this->http->delete( + Endpoint::bind( + Endpoint::GUILD_INTEGRATION, + $guildId, + $integrationId, + ), + headers: $this->getAuditLogReasonHeader($reason), + )->otherwise($this->logThrowable(...)); + } + + /** + * @see https://discord.com/developers/docs/resources/guild#get-guild-widget-settings + * + * @return ExtendedPromiseInterface<\Ragnarok\Fenrir\Parts\WidgetSettings> + */ + public function getWidgetSettings(string $guildId): ExtendedPromiseInterface + { + return $this->mapPromise( + $this->http->get( + Endpoint::bind( + Endpoint::GUILD_WIDGET_SETTINGS, + $guildId, + ), + ), + WidgetSettings::class, + )->otherwise($this->logThrowable(...)); + } + + /** + * @see https://discord.com/developers/docs/resources/guild#modify-guild-widget + * + * @return ExtendedPromiseInterface<\Ragnarok\Fenrir\Parts\WidgetSettings> + */ + public function modifyWidget(string $guildId, array $params, ?string $reason = null): ExtendedPromiseInterface + { + return $this->mapPromise( + $this->http->patch( + Endpoint::bind( + Endpoint::GUILD_WIDGET_SETTINGS, + $guildId, + ), + $params, + $this->getAuditLogReasonHeader($reason), + ), + WidgetSettings::class, + )->otherwise($this->logThrowable(...)); + } + + /** + * @see https://discord.com/developers/docs/resources/guild#get-guild-widget + * + * @return ExtendedPromiseInterface<\Ragnarok\Fenrir\Parts\Widget> + */ + public function getWidget(string $guildId): ExtendedPromiseInterface + { + return $this->mapPromise( + $this->http->get( + Endpoint::bind( + Endpoint::GUILD_WIDGET, + $guildId, + ) + ), + Widget::class, + )->otherwise($this->logThrowable(...)); + } + + /** + * @see https://discord.com/developers/docs/resources/guild#get-guild-vanity-url + * + * @return ExtendedPromiseInterface<\Ragnarok\Fenrir\Parts\Invite> + */ + public function getVanityUrl(string $guildId): ExtendedPromiseInterface + { + return $this->mapPromise( + $this->http->get( + Endpoint::bind( + Endpoint::GUILD_VANITY_URL, + $guildId, + ) + ), + Invite::class, + )->otherwise($this->logThrowable(...)); + } + + /** + * @see https://discord.com/developers/docs/resources/guild#get-guild-widget-image + * + * @return string The url of the guild widget image + */ + public function getWidgetImage(string $guildId, array $queryParams): string + { + $endpoint = Endpoint::bind( + Endpoint::GUILD_WIDGET_IMAGE, + $guildId, + ); + + foreach ($queryParams as $key => $value) { + $endpoint->addQuery($key, $value); + } + + return (string) $endpoint; + } + + /** + * @see https://discord.com/developers/docs/resources/guild#get-guild-welcome-screen + * + * @return ExtendedPromiseInterface<\Ragnarok\Fenrir\Parts\WelcomeScreen> + */ + public function getWelcomeScreen(string $guildId): ExtendedPromiseInterface + { + return $this->mapPromise( + $this->http->get( + Endpoint::bind( + Endpoint::GUILD_WELCOME_SCREEN, + $guildId, + ), + ), + WelcomeScreen::class, + )->otherwise($this->logThrowable(...)); + } + + /** + * @see https://discord.com/developers/docs/resources/guild#modify-current-user-voice-state + * + * @return ExtendedPromiseInterface + */ + public function modifyCurrentUserVoiceState(string $guildId, array $params): ExtendedPromiseInterface + { + return $this->http->patch( + Endpoint::bind( + Endpoint::GUILD_USER_CURRENT_VOICE_STATE, + $guildId, + ), + $params, + )->otherwise($this->logThrowable(...)); + } + + /** + * @see https://discord.com/developers/docs/resources/guild#modify-user-voice-state + * + * @return ExtendedPromiseInterface + */ + public function modifyUserVoiceState(string $guildId, string $userId, array $params): ExtendedPromiseInterface + { + return $this->http->patch( + Endpoint::bind( + Endpoint::GUILD_USER_CURRENT_VOICE_STATE, + $guildId, + $userId + ), + $params, + )->otherwise($this->logThrowable(...)); + } +} diff --git a/src/Rest/Helpers/Guild/Guild/Shared/SetAfkChannelId.php b/src/Rest/Helpers/Guild/Guild/Shared/SetAfkChannelId.php new file mode 100644 index 00000000..a92ade80 --- /dev/null +++ b/src/Rest/Helpers/Guild/Guild/Shared/SetAfkChannelId.php @@ -0,0 +1,20 @@ +data['afk_channel_id'] = $snowflake; + + return $this; + } + + public function getAfkChannelId(): ?string + { + return $this->data['afk_channel_id'] ?? null; + } +} diff --git a/src/Rest/Helpers/Guild/Guild/Shared/SetAfkTimeout.php b/src/Rest/Helpers/Guild/Guild/Shared/SetAfkTimeout.php new file mode 100644 index 00000000..0e3fec41 --- /dev/null +++ b/src/Rest/Helpers/Guild/Guild/Shared/SetAfkTimeout.php @@ -0,0 +1,23 @@ +data['afk_timeout'] = $timeout; + + return $this; + } + + public function getAfkTimeout(): ?int + { + return $this->data['afk_timeout'] ?? null; + } +} diff --git a/src/Rest/Helpers/Guild/Guild/Shared/SetDefaultMessageNotifications.php b/src/Rest/Helpers/Guild/Guild/Shared/SetDefaultMessageNotifications.php new file mode 100644 index 00000000..c86ea6a0 --- /dev/null +++ b/src/Rest/Helpers/Guild/Guild/Shared/SetDefaultMessageNotifications.php @@ -0,0 +1,24 @@ +data['default_message_notifications'] = $filter->value; + + return $this; + } + + public function getDefaultMessageNotifications(): ?DefaultMessageNotifications + { + return isset($this->data['default_message_notifications']) + ? DefaultMessageNotifications::from($this->data['default_message_notifications']) + : null; + } +} diff --git a/src/Rest/Helpers/Guild/Guild/Shared/SetExplicitContentFilter.php b/src/Rest/Helpers/Guild/Guild/Shared/SetExplicitContentFilter.php new file mode 100644 index 00000000..893bc7e7 --- /dev/null +++ b/src/Rest/Helpers/Guild/Guild/Shared/SetExplicitContentFilter.php @@ -0,0 +1,24 @@ +data['explicit_content_filter'] = $filter->value; + + return $this; + } + + public function getExplicitContentFilter(): ?ExplicitContentFilterLevels + { + return isset($this->data['explicit_content_filter']) + ? ExplicitContentFilterLevels::from($this->data['explicit_content_filter']) + : null; + } +} diff --git a/src/Rest/Helpers/Guild/Guild/Shared/SetIcon.php b/src/Rest/Helpers/Guild/Guild/Shared/SetIcon.php new file mode 100644 index 00000000..4a9db99a --- /dev/null +++ b/src/Rest/Helpers/Guild/Guild/Shared/SetIcon.php @@ -0,0 +1,22 @@ +data['icon'] = 'data:' . $imageData->value . ';base64,' . base64_encode($content); + + return $this; + } + + public function getIcon(): ?string + { + return $this->data['icon'] ?? null; + } +} diff --git a/src/Rest/Helpers/Guild/Guild/Shared/SetName.php b/src/Rest/Helpers/Guild/Guild/Shared/SetName.php new file mode 100644 index 00000000..bf5c0d82 --- /dev/null +++ b/src/Rest/Helpers/Guild/Guild/Shared/SetName.php @@ -0,0 +1,20 @@ +data['name'] = $name; + + return $this; + } + + public function getName(): ?string + { + return $this->data['name'] ?? null; + } +} diff --git a/src/Rest/Helpers/Guild/Guild/Shared/SetSystemChannelFlags.php b/src/Rest/Helpers/Guild/Guild/Shared/SetSystemChannelFlags.php new file mode 100644 index 00000000..97d76f42 --- /dev/null +++ b/src/Rest/Helpers/Guild/Guild/Shared/SetSystemChannelFlags.php @@ -0,0 +1,27 @@ + $flags + */ + public function setSystemChannelFlags(Bitwise $flags): static + { + $this->data['system_channel_flags'] = $flags->get(); + + return $this; + } + + public function getSystemChannelFlags(): ?Bitwise + { + return isset($this->data['system_channel_flags']) + ? new Bitwise($this->data['system_channel_flags']) + : null; + } +} diff --git a/src/Rest/Helpers/Guild/Guild/Shared/SetSystemChannelId.php b/src/Rest/Helpers/Guild/Guild/Shared/SetSystemChannelId.php new file mode 100644 index 00000000..7d95e3d1 --- /dev/null +++ b/src/Rest/Helpers/Guild/Guild/Shared/SetSystemChannelId.php @@ -0,0 +1,20 @@ +data['system_channel_id'] = $snowflake; + + return $this; + } + + public function getSystemChannelId(): ?string + { + return $this->data['system_channel_id'] ?? null; + } +} diff --git a/src/Rest/Helpers/Guild/Guild/Shared/SetVerificationLevel.php b/src/Rest/Helpers/Guild/Guild/Shared/SetVerificationLevel.php new file mode 100644 index 00000000..beb784a6 --- /dev/null +++ b/src/Rest/Helpers/Guild/Guild/Shared/SetVerificationLevel.php @@ -0,0 +1,24 @@ +data['verification_level'] = $level->value; + + return $this; + } + + public function getVerificationLevel(): ?VerificationLevels + { + return isset($this->data['verification_level']) + ? VerificationLevels::from($this->data['verification_level']) + : null; + } +} diff --git a/src/Rest/Helpers/Guild/ModifyChannelPositionsBuilder.php b/src/Rest/Helpers/Guild/ModifyChannelPositionsBuilder.php new file mode 100644 index 00000000..29eeee69 --- /dev/null +++ b/src/Rest/Helpers/Guild/ModifyChannelPositionsBuilder.php @@ -0,0 +1,67 @@ +data['id'] = $id; + + return $this; + } + + public function getId(): ?string + { + return $this->data['id'] ?? null; + } + + public function setPosition(int $position): self + { + $this->data['position'] = $position; + + return $this; + } + + public function getPosition(): ?int + { + return $this->data['position'] ?? null; + } + + public function setLockPermissions(bool $lockPermissions): self + { + $this->data['lock_permissions'] = $lockPermissions; + + return $this; + } + + public function getLockPermissions(): ?bool + { + return $this->data['lock_permissions'] ?? null; + } + + public function setParentId(string $parentId): self + { + $this->data['parent_id'] = $parentId; + + return $this; + } + + public function getParentId(): ?string + { + return $this->data['parent_id'] ?? null; + } + + public function get(): array + { + return $this->data; + } +} diff --git a/src/Rest/Rest.php b/src/Rest/Rest.php index 63212099..6e09d33a 100644 --- a/src/Rest/Rest.php +++ b/src/Rest/Rest.php @@ -5,6 +5,7 @@ namespace Ragnarok\Fenrir\Rest; use Discord\Http\Http; +use Ragnarok\Fenrir\Rest\Guild; use Ragnarok\Fenrir\DataMapper; use Psr\Log\LoggerInterface; @@ -22,6 +23,7 @@ class Rest public readonly GuildCommand $guildCommand; public readonly GlobalCommand $globalCommand; public readonly Webhook $webhook; + public readonly Guild $guild; /** * @todo add @@ -47,5 +49,6 @@ public function __construct(private Http $http, private DataMapper $dataMapper, $this->guildCommand = new GuildCommand(...$args); $this->globalCommand = new GlobalCommand(...$args); $this->webhook = new Webhook(...$args); + $this->guild = new Guild(...$args); } } diff --git a/tests/Rest/GuildTest.php b/tests/Rest/GuildTest.php new file mode 100644 index 00000000..4fe184e0 --- /dev/null +++ b/tests/Rest/GuildTest.php @@ -0,0 +1,125 @@ + [ + 'method' => 'get', + 'args' => ['::guild id::'], + 'mockOptions' => [ + 'method' => 'get', + 'return' => (object) [], + ], + 'validationOptions' => [ + 'returnType' => PartsGuild::class, + ] + ], + 'Get preview' => [ + 'method' => 'getPreview', + 'args' => ['::guild id::'], + 'mockOptions' => [ + 'method' => 'get', + 'return' => (object) [], + ], + 'validationOptions' => [ + 'returnType' => GuildPreview::class, + ] + ], + 'Delete guild' => [ + 'method' => 'delete', + 'args' => ['::guild id::'], + 'mockOptions' => [ + 'method' => 'delete', + 'return' => null, + ], + 'validationOptions' => [ + ] + ], + 'Get channels' => [ + 'method' => 'getChannels', + 'args' => ['::guild id::'], + 'mockOptions' => [ + 'method' => 'get', + 'return' => [(object) [], (object) [], (object) []], + ], + 'validationOptions' => [ + 'returnType' => Channel::class, + 'array' => true, + ] + ], + 'Modify channel position' => [ + 'method' => 'modifyChannelPositions', + 'args' => [ + '::guild id::', + [ + ModifyChannelPositionsBuilder::new(), + ModifyChannelPositionsBuilder::new(), + ] + ], + 'mockOptions' => [ + 'method' => 'patch', + 'return' => null, + ], + 'validationOptions' => [ + ] + ], + 'Get member' => [ + 'method' => 'getMember', + 'args' => ['::guild id::', '::member id::'], + 'mockOptions' => [ + 'method' => 'get', + 'return' => (object) [], + ], + 'validationOptions' => [ + 'returnType' => GuildMember::class, + ] + ], + 'Add member role' => [ + 'method' => 'addMemberRole', + 'args' => ['::guild id::', '::member id::', '::role id::'], + 'mockOptions' => [ + 'method' => 'put', + 'return' => null, + ], + 'validationOptions' => [] + ], + 'Remove member role' => [ + 'method' => 'removeMemberRole', + 'args' => ['::guild id::', '::member id::', '::role id::'], + 'mockOptions' => [ + 'method' => 'delete', + 'return' => null, + ], + 'validationOptions' => [] + ], + 'Get ban' => [ + 'method' => 'getBan', + 'args' => ['::guild id::', '::member id::'], + 'mockOptions' => [ + 'method' => 'get', + 'return' => (object) [], + ], + 'validationOptions' => [ + 'returnType' => GuildBan::class, + ] + ], + ]; + } +} diff --git a/tests/Rest/Helpers/Guild/ModifyChannelPositionsBuilderTest.php b/tests/Rest/Helpers/Guild/ModifyChannelPositionsBuilderTest.php new file mode 100644 index 00000000..629af05d --- /dev/null +++ b/tests/Rest/Helpers/Guild/ModifyChannelPositionsBuilderTest.php @@ -0,0 +1,59 @@ +assertNull($builder->getId()); + + $id = '123456789'; + $builder->setId($id); + + $this->assertSame($id, $builder->getId()); + } + + public function testPosition(): void + { + $builder = new ModifyChannelPositionsBuilder(); + + $this->assertNull($builder->getPosition()); + + $position = 34; + $builder->setPosition($position); + + $this->assertSame($position, $builder->getPosition()); + } + + public function testLockPermissions(): void + { + $builder = new ModifyChannelPositionsBuilder(); + + $this->assertNull($builder->getLockPermissions()); + + $lockPermissions = true; + $builder->setLockPermissions($lockPermissions); + + $this->assertSame($lockPermissions, $builder->getLockPermissions()); + } + + public function testParentId(): void + { + $builder = new ModifyChannelPositionsBuilder(); + + $this->assertNull($builder->getParentId()); + + $parentId = '987654321'; + $builder->setParentId($parentId); + + $this->assertSame($parentId, $builder->getParentId()); + } +}