Skip to content

Commit

Permalink
Fix & Improve Ban (#854)
Browse files Browse the repository at this point in the history
* Fix & improve ban

* Bump version, fix wording zlib-stream

* forgot to remove !

* change check for snowflake int|string

* use is_scalar instead to check for array keys

* forgot to remove ! again
  • Loading branch information
SQKo authored Jul 4, 2022
1 parent 200202d commit b355887
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 37 deletions.
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.1.0';
public const VERSION = 'v7.1.2';

/**
* The logger.
Expand Down Expand Up @@ -1275,7 +1275,7 @@ protected function setGateway(?string $gateway = null): ExtendedPromiseInterface
];

if (class_exists('\Clue\React\Zlib\Decompressor')) {
$this->logger->warning('The `clue/reactphp-zlib` is present, Enabling experimental zlib-stream compressed gateway message.');
$this->logger->warning('The `clue/zlib-react` is installed, Enabling experimental zlib-stream compressed gateway message.');
$this->zlibDecompressor = new \Clue\React\Zlib\Decompressor(ZLIB_ENCODING_DEFLATE);
$params['compress'] = 'zlib-stream';
}
Expand Down
12 changes: 2 additions & 10 deletions src/Discord/Parts/Guild/Ban.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ protected function getGuildAttribute(): ?Guild
*/
protected function getUserAttribute(): User
{
if (isset($this->attributes['user_id'])) {
return $this->discord->users->get('id', $this->attributes['user_id']);
if (isset($this->attributes['user_id']) && $user = $this->discord->users->get('id', $this->attributes['user_id'])) {
return $user;
}

if ($user = $this->discord->users->get('id', $this->attributes['user']->id)) {
Expand All @@ -85,14 +85,6 @@ protected function getUserAttribute(): User
return $this->factory->part(User::class, (array) $this->attributes['user'], true);
}

/**
* @inheritdoc
*/
public function getUpdatableAttributes(): array
{
return [];
}

/**
* @inheritdoc
*/
Expand Down
57 changes: 32 additions & 25 deletions src/Discord/Repository/Guild/BanRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace Discord\Repository\Guild;

use Discord\Helpers\Deferred;
use Discord\Http\Endpoint;
use Discord\Parts\Guild\Ban;
use Discord\Parts\User\Member;
Expand Down Expand Up @@ -56,61 +55,69 @@ class BanRepository extends AbstractRepository
*
* @see https://discord.com/developers/docs/resources/guild#create-guild-ban
*
* @param Member|string $member
* @param int|null $daysToDeleteMessages
* @param string|null $reason
* @param User|Member|string $user
* @param int|null $daysToDeleteMessages
* @param string|null $reason
*
* @return ExtendedPromiseInterface
*/
public function ban($member, ?int $daysToDeleteMessages = null, ?string $reason = null): ExtendedPromiseInterface
public function ban($user, ?int $daysToDeleteMessages = null, ?string $reason = null): ExtendedPromiseInterface
{
$deferred = new Deferred();
$content = [];
$headers = [];

if ($member instanceof Member) {
$member = $member->id;
if ($user instanceof Member) {
$user = $user->user;
} elseif (! ($user instanceof User)) {
$user = $this->factory->part(User::class, ['id' => $user], true);
}

if (! is_null($daysToDeleteMessages)) {
if (isset($daysToDeleteMessages)) {
$content['delete_message_days'] = $daysToDeleteMessages;
}

if (! is_null($reason)) {
if (isset($reason)) {
$headers['X-Audit-Log-Reason'] = $reason;
}

$this->http->put(
Endpoint::bind(Endpoint::GUILD_BAN, $this->vars['guild_id'], $member),
return $this->http->put(
Endpoint::bind(Endpoint::GUILD_BAN, $this->vars['guild_id'], $user->id),
empty($content) ? null : $content,
$headers
)->done(function ($response) use ($deferred) {
$ban = $this->factory->create(Ban::class, $response, true);
$this->push($ban);
$deferred->resolve($ban);
}, [$deferred, 'reject']);
)->then(function () use ($user, $reason) {
$ban = $this->factory->create(Ban::class, [
'user' => (object) $user->getRawAttributes(),
'reason' => $reason,
'guild_id' => $this->vars['guild_id'],
], true);
$this->pushItem($ban);

return $deferred->promise();
return $ban;
});
}

/**
* Unbans a member from the guild.
*
* @see https://discord.com/developers/docs/resources/guild#remove-guild-ban
*
* @param User|Ban|string $user User or Ban Part, or User ID
* @param User|Ban|string $ban User or Ban Part, or User ID
* @param string|null $reason Reason for Audit Log.
*
* @return ExtendedPromiseInterface
*/
public function unban($user, ?string $reason = null): ExtendedPromiseInterface
public function unban($ban, ?string $reason = null): ExtendedPromiseInterface
{
if ($user instanceof User || $user instanceof Member) {
$user = $user->id;
} elseif ($user instanceof Ban) {
$user = $user->user_id;
if ($ban instanceof User || $ban instanceof Member) {
$ban = $ban->id;
}

return $this->delete($user, $reason);
if (is_scalar($ban)) {
if ($banPart = $this->get('user_id', $ban)) {
$ban = $banPart;
}
}

return $this->delete($ban, $reason);
}
}

0 comments on commit b355887

Please sign in to comment.