Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add lock/unlock method to Thread class #1240

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/Discord/Builders/CommandBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,12 @@ public function toArray(): array
{
$arrCommand = [
'name' => $this->name,
'description' => $this->description,
];

if ($this->type === Command::CHAT_INPUT) {
$arrCommand['description'] = $this->description;
}

$optionals = [
'type',
'name_localizations',
Expand Down
4 changes: 2 additions & 2 deletions src/Discord/DiscordCommandClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -447,9 +447,9 @@ protected function resolveCommandClientOptions(array $options): array
if (is_string($reason) || $reason instanceof \Stringable) {
$this->getLogger()->error($reason);
} else {
$this->getLogger()->warning('Unhandled internal rejected promise, $reason is not a Throwable, ' . gettype($reason) . ' given.');
$this->getLogger()->warning('Unhandled internal rejected promise, $reason is not a Throwable, '.gettype($reason).' given.');
}
}
},
]);

$options = $resolver->resolve($options);
Expand Down
6 changes: 3 additions & 3 deletions src/Discord/Helpers/LegacyCacheWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use function React\Promise\resolve;

/**
* Legacy v7.x in memory cache behavior
* Legacy v7.x in memory cache behavior.
*
* @since 10.0.0
* @internal
Expand Down Expand Up @@ -89,7 +89,7 @@ public function set($key, $value, $ttl = null)
*/
public function delete($key)
{
if (!array_key_exists($key, $this->items)) {
if (! array_key_exists($key, $this->items)) {
return resolve(false);
}

Expand Down Expand Up @@ -197,7 +197,7 @@ public function unserializer($value)
}
$part = $this->discord->getFactory()->part($this->class, $value->attributes, $value->created);
foreach ($value as $name => $var) {
if (!in_array($name, ['created', 'attributes'])) {
if (! in_array($name, ['created', 'attributes'])) {
$part->{$name} = $var;
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/Discord/Helpers/Multipart.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*/

namespace Discord\Helpers;

use Stringable;

/**
Expand Down Expand Up @@ -43,7 +44,7 @@ class Multipart implements Stringable
/**
* Multipart constructor.
*
* @param array $fields
* @param array $fields
* @param string $boundary Defaults to DiscordPHPSendFileBoundary
*/
public function __construct(array $fields = [], string $boundary = self::BOUNDARY)
Expand Down
4 changes: 1 addition & 3 deletions src/Discord/Parts/Embed/Embed.php
Original file line number Diff line number Diff line change
Expand Up @@ -501,11 +501,9 @@ public function setURL(string $url): self
* Ensures a URL is valid for use in embeds.
*
* @param ?string $url
* @param array $allowed Allowed URL scheme
* @param array $allowed Allowed URL scheme
*
* @throws \DomainException
*
* @return void
*/
protected function ensureValidUrl(?string $url = null, array $allowed = ['http', 'https', 'attachment']): void
{
Expand Down
28 changes: 14 additions & 14 deletions src/Discord/Parts/Interactions/Interaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,20 +87,20 @@ class Interaction extends Part
*/
protected $responded = false;

const TYPE_PING = 1;
const TYPE_APPLICATION_COMMAND = 2;
const TYPE_MESSAGE_COMPONENT = 3;
const TYPE_APPLICATION_COMMAND_AUTOCOMPLETE = 4;
const TYPE_MODAL_SUBMIT = 5;

const RESPONSE_TYPE_PONG = 1;
const RESPONSE_TYPE_CHANNEL_MESSAGE_WITH_SOURCE = 4;
const RESPONSE_TYPE_DEFERRED_CHANNEL_MESSAGE_WITH_SOURCE = 5;
const RESPONSE_TYPE_DEFERRED_UPDATE_MESSAGE = 6;
const RESPONSE_TYPE_UPDATE_MESSAGE = 7;
const RESPONSE_TYPE_APPLICATION_COMMAND_AUTOCOMPLETE_RESULT = 8;
const RESPONSE_TYPE_MODAL = 9;
const RESPONSE_TYPE_PREMIUM_REQUIRED = 10;
public const TYPE_PING = 1;
public const TYPE_APPLICATION_COMMAND = 2;
public const TYPE_MESSAGE_COMPONENT = 3;
public const TYPE_APPLICATION_COMMAND_AUTOCOMPLETE = 4;
public const TYPE_MODAL_SUBMIT = 5;

public const RESPONSE_TYPE_PONG = 1;
public const RESPONSE_TYPE_CHANNEL_MESSAGE_WITH_SOURCE = 4;
public const RESPONSE_TYPE_DEFERRED_CHANNEL_MESSAGE_WITH_SOURCE = 5;
public const RESPONSE_TYPE_DEFERRED_UPDATE_MESSAGE = 6;
public const RESPONSE_TYPE_UPDATE_MESSAGE = 7;
public const RESPONSE_TYPE_APPLICATION_COMMAND_AUTOCOMPLETE_RESULT = 8;
public const RESPONSE_TYPE_MODAL = 9;
public const RESPONSE_TYPE_PREMIUM_REQUIRED = 10;

/**
* Returns true if this interaction has been internally responded.
Expand Down
44 changes: 44 additions & 0 deletions src/Discord/Parts/Thread/Thread.php
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,50 @@ public function rename(string $name, ?string $reason = null): ExtendedPromiseInt
});
}

/**
* Lock the thread.
*
* @param string|null $reason
*
* @return ExtendedPromiseInterface
*/
public function lock(?string $reason = null): ExtendedPromiseInterface
{
$headers = [];
if (isset($reason)) {
$headers['X-Audit-Log-Reason'] = $reason;
}

return $this->http->patch(Endpoint::bind(Endpoint::THREAD, $this->id), ['locked' => true], $headers)
->then(function ($response) {
$this->locked = $response->thread_metadata->locked;

return $this;
});
}

/**
* Unlock the thread.
*
* @param string|null $reason
*
* @return ExtendedPromiseInterface
*/
public function unlock(?string $reason = null): ExtendedPromiseInterface
{
$headers = [];
if (isset($reason)) {
$headers['X-Audit-Log-Reason'] = $reason;
}

return $this->http->patch(Endpoint::bind(Endpoint::THREAD, $this->id), ['locked' => false], $headers)
->then(function ($response) {
$this->locked = $response->thread_metadata->locked;

return $this;
});
}

/**
* Archive the thread.
*
Expand Down
10 changes: 5 additions & 5 deletions src/Discord/Parts/User/Activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,12 @@ protected function getEmojiAttribute(): ?Emoji
public function __toString(): string
{
return match ($this->type) {
self::TYPE_GAME => 'Playing ' . $this->name,
self::TYPE_STREAMING => 'Streaming ' . $this->details,
self::TYPE_LISTENING => 'Listening to ' . $this->name,
self::TYPE_WATCHING => 'Watching ' . $this->name,
self::TYPE_GAME => 'Playing '.$this->name,
self::TYPE_STREAMING => 'Streaming '.$this->details,
self::TYPE_LISTENING => 'Listening to '.$this->name,
self::TYPE_WATCHING => 'Watching '.$this->name,
self::TYPE_CUSTOM => "{$this->emoji} {$this->state}",
self::TYPE_COMPETING => 'Competing in ' . $this->name,
self::TYPE_COMPETING => 'Competing in '.$this->name,
default => $this->name,
};
}
Expand Down
2 changes: 2 additions & 0 deletions src/Discord/Repository/AbstractRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,11 @@ public function save(Part $part, ?string $reason = null): ExtendedPromiseInterfa
case 'patch': // Update old part
$part->created = true;
$part->fill((array) $response);

return $this->cache->set($part->{$this->discrim}, $part)->then(fn ($success) => $part);
default: // Create new part
$newPart = $this->factory->create($this->class, (array) $response, true);

return $this->cache->set($newPart->{$this->discrim}, $this->factory->create($this->class, (array) $response, true))->then(fn ($success) => $newPart);
}
});
Expand Down