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

Overwrite repository->save for CommandBuilder #1256

Open
wants to merge 1 commit 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
57 changes: 57 additions & 0 deletions src/Discord/Repository/Guild/GuildCommandRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,14 @@
namespace Discord\Repository\Guild;

use Discord\Discord;
use Discord\Builders\CommandBuilder;
use Discord\Http\Endpoint;
use Discord\Parts\Part;
use Discord\Parts\Interactions\Command\Command;
use Discord\Repository\AbstractRepository;
use React\Promise\ExtendedPromiseInterface;

use function Discord\nowait;

/**
* Contains application guild commands.
Expand Down Expand Up @@ -57,4 +62,56 @@ public function __construct(Discord $discord, array $vars = [])

parent::__construct($discord, $vars);
}

/**
* Attempts to save a part to the Discord servers.
*
* @param CommandBuilder|Part $part The CommandBuilder or part to save.
* @param string|null $reason Reason for Audit Log (if supported).
*
* @return ExtendedPromiseInterface<Part>
*
* @throws \Exception
*/
public function save(CommandBuilder|Part $part, ?string $reason = null): ExtendedPromiseInterface
{
if ($part instanceof CommandBuilder) {
$method = 'post';
$endpoint = new Endpoint($this->endpoints['create']);
$endpoint->bindAssoc($this->vars);
$attributes = $part->toArray();
} else {
if ($part->created) {
$method = 'patch';
$endpoint = new Endpoint($this->endpoints['update']);
$endpoint->bindAssoc(array_merge($part->getRepositoryAttributes(), $this->vars));
$attributes = $part->getUpdatableAttributes();
} else {
$method = 'post';
$endpoint = new Endpoint($this->endpoints['create']);
$endpoint->bindAssoc(array_merge($part->getRepositoryAttributes(), $this->vars));
$attributes = $part->getCreatableAttributes();
}
}

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

return $this->http->{$method}($endpoint, $attributes, $headers)->then(function ($response) use ($method, $part) {
switch ($method) {
case 'patch': // Update old part
if ($part instanceof CommandBuilder) {
$part = nowait($this->cache->get($part->{$this->discrim})) ?? $this->factory->create($this->class, (array) $response, true);
}
$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);
}
});
}
}
57 changes: 57 additions & 0 deletions src/Discord/Repository/Interaction/GlobalCommandRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,14 @@

namespace Discord\Repository\Interaction;

use Discord\Builders\CommandBuilder;
use Discord\Http\Endpoint;
use Discord\Parts\Part;
use Discord\Parts\Interactions\Command\Command;
use Discord\Repository\AbstractRepository;
use React\Promise\ExtendedPromiseInterface;

use function Discord\nowait;

/**
* Contains application global commands.
Expand Down Expand Up @@ -46,4 +51,56 @@ class GlobalCommandRepository extends AbstractRepository
* {@inheritDoc}
*/
protected $class = Command::class;

/**
* Attempts to save a part to the Discord servers.
*
* @param CommandBuilder|Part $part The CommandBuilder or part to save.
* @param string|null $reason Reason for Audit Log (if supported).
*
* @return ExtendedPromiseInterface<Part>
*
* @throws \Exception
*/
public function save(CommandBuilder|Part $part, ?string $reason = null): ExtendedPromiseInterface
{
if ($part instanceof CommandBuilder) {
$method = 'post';
$endpoint = new Endpoint($this->endpoints['create']);
$endpoint->bindAssoc($this->vars);
$attributes = $part->toArray();
} else {
if ($part->created) {
$method = 'patch';
$endpoint = new Endpoint($this->endpoints['update']);
$endpoint->bindAssoc(array_merge($part->getRepositoryAttributes(), $this->vars));
$attributes = $part->getUpdatableAttributes();
} else {
$method = 'post';
$endpoint = new Endpoint($this->endpoints['create']);
$endpoint->bindAssoc(array_merge($part->getRepositoryAttributes(), $this->vars));
$attributes = $part->getCreatableAttributes();
}
}

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

return $this->http->{$method}($endpoint, $attributes, $headers)->then(function ($response) use ($method, $part) {
switch ($method) {
case 'patch': // Update old part
if ($part instanceof CommandBuilder) {
$part = nowait($this->cache->get($part->{$this->discrim})) ?? $this->factory->create($this->class, (array) $response, true);
}
$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);
}
});
}
}