Skip to content

Commit

Permalink
Check thread feature flags
Browse files Browse the repository at this point in the history
  • Loading branch information
davidcole1340 committed Aug 30, 2021
1 parent 7ce6256 commit cdecade
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
24 changes: 21 additions & 3 deletions src/Discord/Parts/Channel/Channel.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
use Discord\Repository\Channel\ThreadRepository;
use InvalidArgumentException;
use React\Promise\ExtendedPromiseInterface;
use RuntimeException;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Traversable;

Expand Down Expand Up @@ -692,20 +693,37 @@ protected function setPermissionOverwritesAttribute(array $overwrites): void
*/
public function startThread(string $name, bool $private = false, int $auto_archive_duration = 1440): ExtendedPromiseInterface
{
if ($private && ! $this->guild->feature_private_threads) {
return reject(new RuntimeException('Guild does not have access to private threads.'));
}

if ($this->type == Channel::TYPE_NEWS) {
if ($private) {
throw new InvalidArgumentException('You cannot start a private thread within a news channel.');
return reject(new InvalidArgumentException('You cannot start a private thread within a news channel.'));
}

$type = Channel::TYPE_NEWS_THREAD;
} elseif ($this->type == Channel::TYPE_TEXT) {
$type = $private ? Channel::TYPE_PRIVATE_THREAD : Channel::TYPE_PUBLIC_THREAD;
} else {
throw new InvalidArgumentException('You cannot start a thread in this type of channel.');
return reject(new InvalidArgumentException('You cannot start a thread in this type of channel.'));
}

if (! in_array($auto_archive_duration, [60, 1440, 4320, 10080])) {
throw new InvalidArgumentException('`auto_archive_duration` must be one of 60, 1440, 4320, 10080.');
return reject(new InvalidArgumentException('`auto_archive_duration` must be one of 60, 1440, 4320, 10080.'));
}

switch ($auto_archive_duration) {
case 4320:
if (! $this->guild->feature_three_day_thread_archive) {
return reject(new RuntimeException('Guild does not have access to three day thread archive.'));
}
break;
case 10080:
if (! $this->guild->feature_seven_day_thread_archive) {
return reject(new RuntimeException('Guild does not have access to seven day thread archive.'));
}
break;
}

return $this->http->post(Endpoint::bind(Endpoint::CHANNEL_THREADS, $this->id), [
Expand Down
22 changes: 21 additions & 1 deletion src/Discord/Parts/Channel/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
use Discord\Repository\Channel\ReactionRepository;
use InvalidArgumentException;
use React\Promise\ExtendedPromiseInterface;
use RuntimeException;

use function React\Promise\reject;

/**
* A message which is posted to a Discord text channel.
Expand Down Expand Up @@ -500,8 +503,25 @@ public function getLinkAttribute(): ?string
*/
public function startThread(string $name, int $auto_archive_duration = 1440): ExtendedPromiseInterface
{
if (! $this->guild) {
return reject(new RuntimeException('You can only start threads on guild text channels.'));
}

if (! in_array($auto_archive_duration, [60, 1440, 4320, 10080])) {
throw new InvalidArgumentException('`auto_archive_duration` must be one of 60, 1440, 4320, 10080.');
return reject(new InvalidArgumentException('`auto_archive_duration` must be one of 60, 1440, 4320, 10080.'));
}

switch ($auto_archive_duration) {
case 4320:
if (! $this->guild->feature_three_day_thread_archive) {
return reject(new RuntimeException('Guild does not have access to three day thread archive.'));
}
break;
case 10080:
if (! $this->guild->feature_seven_day_thread_archive) {
return reject(new RuntimeException('Guild does not have access to seven day thread archive.'));
}
break;
}

return $this->http->post(Endpoint::bind(Endpoint::CHANNEL_MESSAGE_THREADS, $this->channel_id, $this->id), [
Expand Down

0 comments on commit cdecade

Please sign in to comment.