Skip to content

Commit

Permalink
Add update presence & request guild members
Browse files Browse the repository at this point in the history
  • Loading branch information
Exanlv committed Feb 4, 2023
1 parent 8d051dd commit e26f123
Show file tree
Hide file tree
Showing 7 changed files with 236 additions and 7 deletions.
76 changes: 74 additions & 2 deletions src/Discord.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
use Discord\Http\Http;
use Exan\Dhp\Const\Events as Events;
use Exan\Dhp\Const\WebsocketEvents;
use Exan\Dhp\Enums\Gateway\StatusType;
use Exan\Dhp\Rest\Rest;
use Exan\Dhp\Websocket\Helpers\ActivityBuilder;
use Exan\Dhp\Websocket\Objects\D\Hello;
use Exan\Dhp\Websocket\Objects\Payload;
use JsonMapper;
Expand Down Expand Up @@ -225,9 +227,9 @@ private function handleEvent(Payload $payload)
$this->events->handle($payload);
}

private function sendPayload(array $data)
private function sendPayload(array $data, bool $useBucket = true)
{
$this->websocket->send(json_encode($data));
$this->websocket->send(json_encode($data), $useBucket);
}

private function startHeartbeat()
Expand All @@ -250,4 +252,74 @@ private function stopHeartbeat()
unset($this->heartbeatTimer);
}
}

/**
* @see https://discord.com/developers/docs/topics/gateway-events#update-presence
*/
public function updatePresence(
StatusType $status,
array $activities,
bool $afk = false,
?int $since = null
): void {
$presenceUpdate = [
'status' => $status->value,
'activities' => array_map(fn (ActivityBuilder $builder) => $builder->get(), $activities),
'afk' => $afk,
];

if (!is_null($since)) {
$presenceUpdate['since'] = $since;
}

$this->sendPayload($presenceUpdate);
}

/**
* @see https://discord.com/developers/docs/topics/gateway-events#request-guild-members
*/
public function requestGuildMembersByQuery(
string $guildId,
string $query = '',
int $limit = 0,
bool $presences = false,
?string $nonce = null
): void {
$guildMemberRequest = [
'guild_id' => $guildId,
'query' => $query,
'limit' => $limit,
'presences' => $presences,
];

if (!is_null($nonce)) {
$guildMemberRequest['nonce'] = $nonce;
}

$this->sendPayload($guildMemberRequest);
}

/**
* @see https://discord.com/developers/docs/topics/gateway-events#request-guild-members
*/
public function requestGuildMembersByUserIds(
string $guildId,
array $userIds,
int $limit = 0,
bool $presences = false,
?string $nonce = null
): void {
$guildMemberRequest = [
'guild_id' => $guildId,
'user_ids' => $userIds,
'limit' => $limit,
'presences' => $presences,
];

if (!is_null($nonce)) {
$guildMemberRequest['nonce'] = $nonce;
}

$this->sendPayload($guildMemberRequest);
}
}
12 changes: 12 additions & 0 deletions src/Enums/Gateway/StatusType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Exan\Dhp\Enums\Gateway;

enum StatusType: string
{
case ONLINE = 'online';
case DO_NOT_DISTURB = 'dnd';
case AFK = 'idle';
case INVISIBLE = 'invisible';
case OFFLINE = 'offline';
}
11 changes: 11 additions & 0 deletions src/Websocket/Helpers/ActivityBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Exan\Dhp\Websocket\Helpers;

class ActivityBuilder
{
public function get(): array
{
return [];
}
}
8 changes: 4 additions & 4 deletions tests/Discord/DiscordTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,13 @@ protected function mockIncomingMessage(array $message)
($this->websocketHandlers[WebsocketEvents::MESSAGE])($messageMock);
}

protected function assertMessageSent(array $message)
protected function assertMessageSent(array $message, bool $useBucket = true)
{
$this->discord->websocket->shouldHaveReceived('send', [json_encode($message)]);
$this->discord->websocket->shouldHaveReceived('send', [json_encode($message), $useBucket]);
}

protected function assertMessageNotSent(array $message)
protected function assertMessageNotSent(array $message, bool $useBucket = true)
{
$this->discord->websocket->shouldNotHaveReceived('send', [json_encode($message)]);
$this->discord->websocket->shouldNotHaveReceived('send', [json_encode($message), $useBucket]);
}
}
2 changes: 1 addition & 1 deletion tests/Discord/HandlesHeartbeatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function testHandlesHello()
$this->assertMessageSent([
'op' => 1,
'd' => null
]);
], false);
}

public function testAcknowledgesHearbeat()
Expand Down
90 changes: 90 additions & 0 deletions tests/Discord/RequestGuildMembersTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

namespace Tests\Exan\Dhp\Discord;

/**
* @runTestsInSeparateProcesses
* @preserveGlobalState disabled
*/
class RequestGuildMembersTest extends DiscordTestCase
{
public function testRequestGuildMembersByQuery()
{
$this->discord->requestGuildMembersByQuery(
'::guild id::',
'',
123,
true
);

$this->assertMessageSent(
[
'guild_id' => '::guild id::',
'query' => '',
'limit' => 123,
'presences' => true
]
);
}

public function testRequestGuildMembersByQueryWithNonce()
{
$this->discord->requestGuildMembersByQuery(
'::guild id::',
'',
123,
true,
'::nonce::'
);

$this->assertMessageSent(
[
'guild_id' => '::guild id::',
'query' => '',
'limit' => 123,
'presences' => true,
'nonce' => '::nonce::'
]
);
}

public function testRequestGuildMembersByUserIds()
{
$this->discord->requestGuildMembersByUserIds(
'::guild id::',
['::user id::'],
123,
true
);

$this->assertMessageSent(
[
'guild_id' => '::guild id::',
'user_ids' => ['::user id::'],
'limit' => 123,
'presences' => true
]
);
}

public function testRequestGuildMembersByUserIdsWithNonce()
{
$this->discord->requestGuildMembersByUserIds(
'::guild id::',
['::user id::'],
123,
true,
'::testRequestGuildMembersByUserIdsWithNonce::'
);

$this->assertMessageSent(
[
'guild_id' => '::guild id::',
'user_ids' => ['::user id::'],
'limit' => 123,
'presences' => true,
'nonce' => '::testRequestGuildMembersByUserIdsWithNonce::'
]
);
}
}
44 changes: 44 additions & 0 deletions tests/Discord/UpdatePresenceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Tests\Exan\Dhp\Discord;

use Exan\Dhp\Enums\Gateway\StatusType;
use Exan\Dhp\Websocket\Helpers\ActivityBuilder;
use Tests\Exan\Dhp\Discord\DiscordTestCase;

/**
* @runTestsInSeparateProcesses
* @preserveGlobalState disabled
*/
class UpdatePresenceTest extends DiscordTestCase
{
public function testUpdatePresence()
{
$this->discord->updatePresence(
StatusType::ONLINE,
[new ActivityBuilder()],
);

$this->assertMessageSent([
'status' => StatusType::ONLINE->value,
'activities' => [[]],
'afk' => false,
]);
}

public function testUpdatePresenceWithSince()
{
$this->discord->updatePresence(
StatusType::ONLINE,
[new ActivityBuilder()],
since: 12345
);

$this->assertMessageSent([
'status' => StatusType::ONLINE->value,
'activities' => [[]],
'afk' => false,
'since' => 12345
]);
}
}

0 comments on commit e26f123

Please sign in to comment.