Skip to content

Commit

Permalink
Merge branch 'master' into collection-enhancement
Browse files Browse the repository at this point in the history
  • Loading branch information
valzargaming authored Dec 24, 2024
2 parents 2c1435b + ad0b2d7 commit 1c1dccb
Show file tree
Hide file tree
Showing 43 changed files with 568 additions and 455 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,22 @@
"trafficcophp/bytebuffer": "^0.3",
"monolog/monolog": "^2.1.1 || ^3.0",
"react/event-loop": "^1.2",
"react/promise": "^2.2",
"ext-json": "*",
"ext-zlib": "*",
"discord-php/http": "^10.1.7",
"react/child-process": "^0.6.3",
"discord/interactions": "^2.2",
"react/async": "^4.0 || ^3.0",
"react/cache": "^0.5 || ^0.6 || ^1.0",
"react/promise": "^3.0.0",
"psr/simple-cache": "^1.0 || ^2.0 || ^3.0"
},
"require-dev": {
"symfony/var-dumper": "*",
"friendsofphp/php-cs-fixer": "^3",
"phpunit/phpunit": "^9.4.4",
"davidcole1340/reactsh": "dev-master",
"wyrihaximus/react-cache-redis": "^3.0 || >=4.0 <4.4",
"wyrihaximus/react-cache-redis": "^4.5",
"symfony/cache": "^5.4"
},
"autoload": {
Expand Down
28 changes: 20 additions & 8 deletions src/Discord/Builders/MessageBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,16 @@ public function setPoll(Poll|null $poll): self
return $this;
}

/**
* Returns the poll of the message.
*
* @return Poll|null
*/
public function getPoll(): ?Poll
{
return $this->poll;
}

/**
* Override the default username of the webhook. Only used for executing webhook.
*
Expand Down Expand Up @@ -318,6 +328,16 @@ public function setEmbeds(array $embeds): self
return $this->addEmbed(...$embeds);
}

/**
* Returns all the embeds in the builder.
*
* @return array[]|null
*/
public function getEmbeds(): ?array
{
return $this->embeds;
}

/**
* Sets the allowed mentions object of the message.
*
Expand Down Expand Up @@ -750,14 +770,6 @@ public function jsonSerialize(): ?array
$body['poll'] = $this->poll;
}

if (isset($this->enforce_nonce)) {
$body['enforce_nonce'] = $this->enforce_nonce;
}

if (isset($this->poll)) {
$body['poll'] = $this->poll;
}

return $body;
}
}
51 changes: 24 additions & 27 deletions src/Discord/Discord.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use Discord\Factory\Factory;
use Discord\Helpers\BigInt;
use Discord\Helpers\CacheConfig;
use Discord\Helpers\Deferred;
use Discord\Helpers\RegisteredCommand;
use Discord\Http\Drivers\React;
use Discord\Http\Endpoint;
Expand Down Expand Up @@ -51,7 +50,8 @@
use React\EventLoop\Loop;
use React\EventLoop\LoopInterface;
use React\EventLoop\TimerInterface;
use React\Promise\ExtendedPromiseInterface;
use React\Promise\Deferred;
use React\Promise\PromiseInterface;
use React\Socket\Connector as SocketConnector;
use Symfony\Component\OptionsResolver\OptionsResolver;

Expand Down Expand Up @@ -468,10 +468,10 @@ protected function handleReady(object $data)
$unavailable = [];

foreach ($content->guilds as $guild) {
/** @var ExtendedPromiseInterface */
/** @var PromiseInterface */
$promise = coroutine([$event, 'handle'], $guild);

$promise->done(function ($d) use (&$unavailable) {
$promise->then(function ($d) use (&$unavailable) {
if (! empty($d->unavailable)) {
$unavailable[$d->id] = $d->unavailable;
}
Expand All @@ -497,7 +497,7 @@ protected function handleReady(object $data)
unset($unavailable[$guild->id]);
}
if (count($unavailable) < 1) {
$guildLoad->resolve();
$guildLoad->resolve(null);
}
};
$this->on(Event::GUILD_CREATE, $onGuildCreate);
Expand All @@ -512,18 +512,18 @@ protected function handleReady(object $data)
unset($unavailable[$guild->id]);
}
if (count($unavailable) < 1) {
$guildLoad->resolve();
$guildLoad->resolve(null);
}
};
$this->on(Event::GUILD_DELETE, $onGuildDelete);

$guildLoad->promise()->always(function () use ($onGuildCreate, $onGuildDelete) {
$guildLoad->promise()->finally(function () use ($onGuildCreate, $onGuildDelete) {
$this->removeListener(Event::GUILD_CREATE, $onGuildCreate);
$this->removeListener(Event::GUILD_DELETE, $onGuildDelete);
$this->logger->info('all guilds are now available', ['count' => $this->guilds->count()]);

$this->setupChunking();
})->done();
});
}

/**
Expand Down Expand Up @@ -775,7 +775,7 @@ protected function handleDispatch(object $data): void
$handler = new $hData['class']($this);

$deferred = new Deferred();
$deferred->promise()->done(function ($d) use ($data, $hData) {
$deferred->promise()->then(function ($d) use ($data, $hData) {
if (is_array($d) && count($d) == 2) {
list($new, $old) = $d;
} else {
Expand Down Expand Up @@ -809,14 +809,14 @@ protected function handleDispatch(object $data): void

if (! $this->emittedInit && (! in_array($data->t, $parse))) {
$this->unparsedPackets[] = function () use (&$handler, &$deferred, &$data) {
/** @var ExtendedPromiseInterface */
/** @var PromiseInterface */
$promise = coroutine([$handler, 'handle'], $data->d);
$promise->done([$deferred, 'resolve'], [$deferred, 'reject']);
$promise->then([$deferred, 'resolve'], [$deferred, 'reject']);
};
} else {
/** @var ExtendedPromiseInterface */
/** @var PromiseInterface */
$promise = coroutine([$handler, 'handle'], $data->d);
$promise->done([$deferred, 'resolve'], [$deferred, 'reject']);
$promise->then([$deferred, 'resolve'], [$deferred, 'reject']);
}
}

Expand Down Expand Up @@ -1079,7 +1079,7 @@ protected function setupHeartbeat(int $interval): void
*/
protected function connectWs(): void
{
$this->setGateway()->done(function ($gateway) {
$this->setGateway()->then(function ($gateway) {
if (isset($gateway['session']) && $session = $gateway['session']) {
if ($session['remaining'] < 2) {
$this->logger->error('exceeded number of reconnects allowed, waiting before attempting reconnect', $session);
Expand All @@ -1093,12 +1093,9 @@ protected function connectWs(): void

$this->logger->info('starting connection to websocket', ['gateway' => $this->gateway]);

/** @var ExtendedPromiseInterface */
/** @var PromiseInterface */
$promise = ($this->wsFactory)($this->gateway);
$promise->done(
[$this, 'handleWsConnection'],
[$this, 'handleWsConnectionFailed']
);
$promise->then([$this, 'handleWsConnection'], [$this, 'handleWsConnectionFailed']);
});
}

Expand Down Expand Up @@ -1177,7 +1174,7 @@ protected function ready()
*
* @throws \UnexpectedValueException
*/
public function updatePresence(Activity $activity = null, bool $idle = false, string $status = 'online', bool $afk = false): void
public function updatePresence(?Activity $activity = null, bool $idle = false, string $status = 'online', bool $afk = false): void
{
$idle = $idle ? time() * 1000 : null;

Expand Down Expand Up @@ -1233,9 +1230,9 @@ public function getVoiceClient(string $guild_id): ?VoiceClient
* @since 10.0.0 Removed argument $check that has no effect (it is always checked)
* @since 4.0.0
*
* @return ExtendedPromiseInterface<VoiceClient>
* @return PromiseInterface<VoiceClient>
*/
public function joinVoiceChannel(Channel $channel, $mute = false, $deaf = true, ?LoggerInterface $logger = null): ExtendedPromiseInterface
public function joinVoiceChannel(Channel $channel, $mute = false, $deaf = true, ?LoggerInterface $logger = null): PromiseInterface
{
$deferred = new Deferred();

Expand Down Expand Up @@ -1329,9 +1326,9 @@ public function joinVoiceChannel(Channel $channel, $mute = false, $deaf = true,
*
* @param string|null $gateway Gateway URL to set.
*
* @return ExtendedPromiseInterface<array>
* @return PromiseInterface
*/
protected function setGateway(?string $gateway = null): ExtendedPromiseInterface
protected function setGateway(?string $gateway = null): PromiseInterface
{
$deferred = new Deferred();
$defaultSession = [
Expand Down Expand Up @@ -1359,7 +1356,7 @@ protected function setGateway(?string $gateway = null): ExtendedPromiseInterface
};

if (null === $gateway) {
$this->http->get(Endpoint::GATEWAY_BOT)->done(function ($response) use ($buildParams) {
$this->http->get(Endpoint::GATEWAY_BOT)->then(function ($response) use ($buildParams) {
if ($response->shards > 1) {
$this->logger->info('Please contact the DiscordPHP devs at https://discord.gg/dphp or https://github.com/discord-php/DiscordPHP/issues if you are interested in assisting us with sharding support development.');
}
Expand Down Expand Up @@ -1681,14 +1678,14 @@ public function getChannel($channel_id): ?Channel
* Add listener for incoming application command from interaction.
*
* @param string|array $name
* @param callable $callback
* @param callable|null $callback
* @param callable|null $autocomplete_callback
*
* @throws \LogicException
*
* @return RegisteredCommand
*/
public function listenCommand($name, callable $callback = null, ?callable $autocomplete_callback = null): RegisteredCommand
public function listenCommand($name, ?callable $callback = null, ?callable $autocomplete_callback = null): RegisteredCommand
{
if (is_array($name) && count($name) == 1) {
$name = array_shift($name);
Expand Down
15 changes: 8 additions & 7 deletions src/Discord/Helpers/Buffer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
use Discord\Exceptions\BufferTimedOutException;
use Evenement\EventEmitter;
use React\EventLoop\LoopInterface;
use React\Promise\ExtendedPromiseInterface;
use React\Promise\Deferred;
use React\Promise\PromiseInterface;
use React\Stream\WritableStreamInterface;

/**
Expand Down Expand Up @@ -106,11 +107,11 @@ private function readRaw(int $length)
* @param null|string $format Format to read the bytes in. See `pack()`.
* @param int $timeout Time in milliseconds before the read times out.
*
* @return ExtendedPromiseInterface<mixed, \RuntimeException>
* @return PromiseInterface<mixed, \RuntimeException>
*
* @throws \RuntimeException When there is an error unpacking the read bytes.
*/
public function read(int $length, ?string $format = null, ?int $timeout = -1): ExtendedPromiseInterface
public function read(int $length, ?string $format = null, ?int $timeout = -1): PromiseInterface
{
$deferred = new Deferred();

Expand Down Expand Up @@ -152,11 +153,11 @@ public function read(int $length, ?string $format = null, ?int $timeout = -1): E
*
* @param int $timeout Time in milliseconds before the read times out.
*
* @return ExtendedPromiseInterface<int, \RuntimeException>
* @return PromiseInterface<int, \RuntimeException>
*
* @throws \RuntimeException When there is an error unpacking the read bytes.
*/
public function readInt32(int $timeout = -1): ExtendedPromiseInterface
public function readInt32(int $timeout = -1): PromiseInterface
{
return $this->read(4, 'l', $timeout);
}
Expand All @@ -166,11 +167,11 @@ public function readInt32(int $timeout = -1): ExtendedPromiseInterface
*
* @param int $timeout Time in milliseconds before the read times out.
*
* @return ExtendedPromiseInterface<int, \RuntimeException>
* @return PromiseInterface<int, \RuntimeException>
*
* @throws \RuntimeException When there is an error unpacking the read bytes.
*/
public function readInt16(int $timeout = -1): ExtendedPromiseInterface
public function readInt16(int $timeout = -1): PromiseInterface
{
return $this->read(2, 'v', $timeout);
}
Expand Down
28 changes: 0 additions & 28 deletions src/Discord/Helpers/Deferred.php

This file was deleted.

30 changes: 0 additions & 30 deletions src/Discord/Helpers/ExtendedPromisorInterface.php

This file was deleted.

14 changes: 7 additions & 7 deletions src/Discord/Helpers/RegisteredCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ class RegisteredCommand
* RegisteredCommand represents a command that has been registered with the
* Discord servers and has a handler to handle when the command is triggered.
*
* @param Discord $discord
* @param string $name
* @param callable $callback
* @param ?callable $autocomplete_callback Callback returning list of auto complete suggestions
* @param Discord $discord
* @param string $name
* @param callable|null $callback
* @param callable|null $autocomplete_callback Callback returning list of auto complete suggestions
*/
public function __construct(Discord $discord, string $name, callable $callback = null, ?callable $autocomplete_callback = null)
public function __construct(Discord $discord, string $name, ?callable $callback = null, ?callable $autocomplete_callback = null)
{
$this->discord = $discord;
$this->name = $name;
Expand Down Expand Up @@ -168,14 +168,14 @@ public function getSubCommand(string $name): ?RegisteredCommand
* Adds a sub-command to the command.
*
* @param string|array $name
* @param callable $callback
* @param callable|null $callback
* @param callable|null $autocomplete_callback
*
* @throws \LogicException
*
* @return static
*/
public function addSubCommand($name, callable $callback = null, ?callable $autocomplete_callback = null): RegisteredCommand
public function addSubCommand($name, ?callable $callback = null, ?callable $autocomplete_callback = null): RegisteredCommand
{
if (is_array($name) && count($name) == 1) {
$name = array_shift($name);
Expand Down
Loading

0 comments on commit 1c1dccb

Please sign in to comment.