Skip to content

Commit

Permalink
Bump version, deprecate invite stage instance, thread updateable (#728)
Browse files Browse the repository at this point in the history
* deprecate stage instance invite, bump version

* thread updateable and audit log reason

* fix auto_archive_duration wrong type

* add attachment to message builder

* make non close loop sends code 1000
  • Loading branch information
SQKo authored Feb 16, 2022
1 parent 02b35ac commit 9248705
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 12 deletions.
56 changes: 56 additions & 0 deletions src/Discord/Builders/MessageBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Discord\Exceptions\FileNotFoundException;
use Discord\Helpers\Multipart;
use Discord\Http\Exceptions\RequestFailedException;
use Discord\Parts\Channel\Attachment;
use Discord\Parts\Channel\Message;
use Discord\Parts\Embed\Embed;
use Discord\Parts\Guild\Sticker;
Expand Down Expand Up @@ -66,6 +67,13 @@ class MessageBuilder implements JsonSerializable
*/
private $files = [];

/**
* Attachments to send with this message.
*
* @var Attachment[]
*/
private $attachments = [];

/**
* Components to send with this message.
*
Expand Down Expand Up @@ -333,6 +341,50 @@ public function getComponents(): array
return $this->components;
}

/**
* Adds attachment(s) to the message.
*
* @param Attachment|string|int $attachment Attachment objects or IDs to add
*
* @return $this
*/
public function addAttachment(...$attachments): self
{
foreach ($attachments as $attachment) {
if ($attachment instanceof Attachment) {
$attachment = $attachment->getRawAttributes();
} else {
$attachment = ['id' => $attachment];
}

$this->attachments[] = $attachment;
}

return $this;
}

/**
* Returns all the attachments in the message.
*
* @return Attachment[]
*/
public function getAttachments(): array
{
return $this->attachments;
}

/**
* Removes all attachments from the message.
*
* @return $this
*/
public function clearAttachments(): self
{
$this->attachments = [];

return $this;
}

/**
* Sets the allowed mentions object of the message.
*
Expand Down Expand Up @@ -524,6 +576,10 @@ public function jsonSerialize(): array
];
}

if ($this->attachments) {
$content['attachments'] = $this->attachments;
}

if ($empty) {
throw new RequestFailedException('You cannot send an empty message. Set the content or add an embed or file.');
}
Expand Down
4 changes: 2 additions & 2 deletions src/Discord/Discord.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class Discord
*
* @var string Version.
*/
public const VERSION = 'v7.0.0';
public const VERSION = 'v7.0.2';

/**
* The logger.
Expand Down Expand Up @@ -1398,7 +1398,7 @@ public function run(): void
public function close(bool $closeLoop = true): void
{
$this->closing = true;
$this->ws->close(Op::CLOSE_UNKNOWN_ERROR, 'discordphp closing...');
$this->ws->close($closeLoop ? Op::CLOSE_UNKNOWN_ERROR : Op::CLOSE_NORMAL, 'discordphp closing...');
$this->emit('closed', [$this]);
$this->logger->info('discord closed');

Expand Down
3 changes: 1 addition & 2 deletions src/Discord/Parts/Channel/Invite.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
* @property int|null $approximate_presence_count Approximate count of online members, returned from the GET /invites/<code> endpoint when with_counts is true.
* @property int|null $approximate_member_count Approximate count of total members, returned from the GET /invites/<code> endpoint when with_counts is true.
* @property Carbon|null $expires_at The expiration date of this invite, returned from the GET /invites/<code> endpoint when with_expiration is true.
* @property object|null $stage_instance Stage instance data if there is a public Stage instance in the Stage channel this invite is for.
* @property ScheduledEvent|null $guild_scheduled_event Guild scheduled event data, only included if guild_scheduled_event_id contains a valid guild scheduled event id.
* @property int $uses How many times the invite has been used.
* @property int $max_uses How many times the invite can be used.
Expand All @@ -60,7 +59,7 @@ class Invite extends Part
'approximate_presence_count',
'approximate_member_count',
'expires_at',
'stage_instance',
'stage_instance', // deprecated
'guild_scheduled_event',

// Extra metadata
Expand Down
9 changes: 7 additions & 2 deletions src/Discord/Parts/Embed/Embed.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Carbon\Carbon;
use Discord\Helpers\Collection;
use Discord\Parts\Channel\Attachment;
use Discord\Parts\Part;
use function Discord\poly_strlen;

Expand Down Expand Up @@ -385,13 +386,17 @@ public function setFooter(string $text, string $iconurl = ''): self
/**
* Set the image of this embed.
*
* @param string $url
* @param string|Attachment $url
*
* @return $this
*/
public function setImage($url): self
{
$this->image = ['url' => (string) $url];
if ($url instanceof Attachment) {
$this->image = ['url' => 'attachment://'.$url->filename];
} else {
$this->image = ['url' => (string) $url];
}

return $this;
}
Expand Down
102 changes: 98 additions & 4 deletions src/Discord/Parts/Thread/Thread.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,36 @@ protected function getArchiverIdAttribute(): ?string
return $this->thread_metadata->archiver_id ?? null;
}

/**
* Set whether the thread is archived.
*
* @param bool $value
*/
protected function setArchivedAttribute(bool $value)
{
$this->thread_metadata->archived = $value;
}

/**
* Set whether the thread is locked.
*
* @param bool $value
*/
protected function setLockedAttribute(bool $value)
{
$this->thread_metadata->locked = $value;
}

/**
* Set the number of minutes of inactivity required for the thread to auto archive.
*
* @param int $value
*/
protected function setAutoArchiveDurationAttribute(int $value)
{
$this->thread_metadata->auto_archive_duration = $value;
}

/**
* Returns the user who archived the thread.
*
Expand Down Expand Up @@ -311,24 +341,74 @@ public function removeMember($user): ExtendedPromiseInterface
return $this->http->delete(Endpoint::bind(Endpoint::THREAD_MEMBER, $this->id, $user));
}

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

return $this->http->patch(Endpoint::bind(Endpoint::THREAD, $this->id), ['name' => $name], $headers);
}

/**
* Archive the thread.
*
* @param string|null $reason Reason for Audit Log.
*
* @return ExtendedPromiseInterface
*/
public function archive(): ExtendedPromiseInterface
public function archive(?string $reason = null): ExtendedPromiseInterface
{
return $this->http->patch(Endpoint::bind(Endpoint::THREAD, $this->id), ['archived' => true]);
$headers = [];
if (isset($reason)) {
$headers['X-Audit-Log-Reason'] = $reason;
}

return $this->http->patch(Endpoint::bind(Endpoint::THREAD, $this->id), ['archived' => true], $headers);
}

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

return $this->http->patch(Endpoint::bind(Endpoint::THREAD, $this->id), ['archived' => false], $headers);
}

/**
* Set auto archive duration of the thread.
*
* @param int $duration Duration in minutes.
* @param string|null $reason Reason for Audit Log.
*
* @return ExtendedPromiseInterface
*/
public function unarchive(): ExtendedPromiseInterface
public function setAutoArchiveDuration(int $duration, ?string $reason = null): ExtendedPromiseInterface
{
return $this->http->patch(Endpoint::bind(Endpoint::THREAD, $this->id), ['archived' => false]);
$headers = [];
if (isset($reason)) {
$headers['X-Audit-Log-Reason'] = $reason;
}

return $this->http->patch(Endpoint::bind(Endpoint::THREAD, $this->id), ['auto_archive_duration' => $duration], $headers);
}

/**
Expand Down Expand Up @@ -656,6 +736,20 @@ public function createMessageCollector(callable $filter, array $options = []): E
return $deferred->promise();
}

/**
* @inheritdoc
*/
public function getUpdatableAttributes(): array
{
return [
'name' => $this->name,
'rate_limit_per_user' => $this->rate_limit_per_user,
'archived' => $this->archived,
'auto_archive_duration' => $this->auto_archive_duration,
'locked' => $this->locked,
];
}

/**
* @inheritdoc
*/
Expand Down
4 changes: 2 additions & 2 deletions src/Discord/Parts/User/Member.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@ public function setNickname(?string $nick = null, ?string $reason = null): Exten
/**
* Moves the member to another voice channel.
*
* @param Channel|string $channel The channel to move the member to.
* @param string|null $reason Reason for Audit Log.
* @param Channel|string|null $channel The channel to move the member to.
* @param string|null $reason Reason for Audit Log.
*
* @return ExtendedPromiseInterface
*/
Expand Down

0 comments on commit 9248705

Please sign in to comment.