Skip to content

Commit ddf4d59

Browse files
authored
Implement Guild Stage Instance (#643)
* Add role icon in guild discord/discord-api-docs#3847 * Add role icons feature in Guild.php * Add missing unicode emoji in Updateable Attributes * Create Stage Instance Repository Part of guild * Add StageInstance Part & Events * fix typo fillable * Fix stage instance endpoint and typos * Update src/Discord/Parts/Channel/StageInstance.php Remove debugging * remove null from getGuildAttribute * remove guilds->push() from stage instance events * Add stage instance const in audit log * resolve conflicts
1 parent c00158a commit ddf4d59

File tree

10 files changed

+273
-0
lines changed

10 files changed

+273
-0
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php
2+
3+
/*
4+
* This file is a part of the DiscordPHP project.
5+
*
6+
* Copyright (c) 2015-present David Cole <david.cole1340@gmail.com>
7+
*
8+
* This file is subject to the MIT license that is bundled
9+
* with this source code in the LICENSE.md file.
10+
*/
11+
12+
namespace Discord\Parts\Channel;
13+
14+
use Discord\Parts\Guild\Guild;
15+
use Discord\Parts\Part;
16+
17+
/**
18+
* A Stage Instance holds information about a live stage. on a Discord guild.
19+
*
20+
* @property string $id The unique identifier of the Stage Instance.
21+
* @property string $guild_id The unique identifier of the guild that the stage instance associated to.
22+
* @property Guild $guild The guild that the stage instance associated to.
23+
* @property string $channel_id The id of the associated Stage channel.
24+
* @property Channel $channel The channel that the stage instance associated to.
25+
* @property string $topic The topic of the Stage instance (1-120 characters).
26+
* @property int $privacy_level The privacy level of the Stage instance.
27+
* @property bool $discoverable_disabled Whether or not Stage Discovery is disabled.
28+
*/
29+
class StageInstance extends Part
30+
{
31+
public const PRIVACY_LEVEL_PUBLIC = 1;
32+
public const PRIVACY_LEVEL_GROUP_ONLY = 2;
33+
34+
/**
35+
* @inheritdoc
36+
*/
37+
protected $fillable = [
38+
'id',
39+
'guild_id',
40+
'channel_id',
41+
'topic',
42+
'privacy_level',
43+
'discoverable_disabled',
44+
];
45+
46+
/**
47+
* Returns the guild attribute.
48+
*
49+
* @return Guild The guild attribute.
50+
*/
51+
protected function getGuildAttribute(): Guild
52+
{
53+
return $this->discord->guilds->get('id', $this->guild_id);
54+
}
55+
56+
/**
57+
* Returns the channel attribute.
58+
*
59+
* @return Channel The Stage channel.
60+
*/
61+
protected function getChannelAttribute(): Part
62+
{
63+
if ($channel = $this->discord->getChannel($this->channel_id)) {
64+
return $channel;
65+
}
66+
67+
return $this->factory->create(Channel::class, [
68+
'id' => $this->channel_id,
69+
'type' => Channel::TYPE_STAGE_CHANNEL,
70+
], true);
71+
}
72+
73+
/**
74+
* @inheritdoc
75+
*/
76+
public function getCreatableAttributes(): array
77+
{
78+
return [
79+
'channel_id' => $this->channel_id,
80+
'topic' => $this->topic,
81+
'privacy_level' => $this->privacy_level,
82+
];
83+
}
84+
85+
/**
86+
* @inheritdoc
87+
*/
88+
public function getUpdatableAttributes(): array
89+
{
90+
return [
91+
'topic' => $this->topic,
92+
'privacy_level' => $this->privacy_level,
93+
];
94+
}
95+
96+
/**
97+
* @inheritdoc
98+
*/
99+
public function getRepositoryAttributes(): array
100+
{
101+
return [
102+
'channel_id' => $this->channel_id,
103+
];
104+
}
105+
}

src/Discord/Parts/Guild/AuditLog/Entry.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ class Entry extends Part
6565
public const INTEGRATION_CREATE = 80;
6666
public const INTEGRATION_UPDATE = 81;
6767
public const INTEGRATION_DELETE = 82;
68+
public const STAGE_INSTANCE_CREATE = 83;
69+
public const STAGE_INSTANCE_UPDATE = 84;
70+
public const STAGE_INSTANCE_DELETE = 85;
6871
public const THREAD_CREATE = 110;
6972
public const THREAD_UPDATE = 111;
7073
public const THREAD_DELETE = 112;

src/Discord/Parts/Guild/Guild.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use Discord\Parts\Guild\AuditLog\AuditLog;
2828
use Discord\Parts\Guild\AuditLog\Entry;
2929
use Discord\Repository\Guild\GuildTemplateRepository;
30+
use Discord\Repository\Guild\StageInstanceRepository;
3031
use Exception;
3132
use React\Promise\ExtendedPromiseInterface;
3233
use ReflectionClass;
@@ -105,6 +106,7 @@
105106
* @property BanRepository $bans
106107
* @property EmojiRepository $emojis
107108
* @property GuildTemplateRepository $templates
109+
* @property StageInstanceRepository $stage_instances
108110
*/
109111
class Guild extends Part
110112
{
@@ -169,6 +171,7 @@ class Guild extends Part
169171
'approximate_member_count',
170172
'approximate_presence_count',
171173
'nsfw_level',
174+
'stage_instances',
172175
'premium_progress_bar_enabled',
173176
];
174177

@@ -211,6 +214,7 @@ class Guild extends Part
211214
'invites' => InviteRepository::class,
212215
'emojis' => EmojiRepository::class,
213216
'templates' => GuildTemplateRepository::class,
217+
'stage_instances' => StageInstanceRepository::class,
214218
];
215219

216220
/**
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
/*
4+
* This file is a part of the DiscordPHP project.
5+
*
6+
* Copyright (c) 2015-present David Cole <david.cole1340@gmail.com>
7+
*
8+
* This file is subject to the MIT license that is bundled
9+
* with this source code in the LICENSE.md file.
10+
*/
11+
12+
namespace Discord\Repository\Guild;
13+
14+
use Discord\Http\Endpoint;
15+
use Discord\Parts\Channel\StageInstance;
16+
use Discord\Repository\AbstractRepository;
17+
18+
/**
19+
* Contains a live stage instances channel.
20+
*
21+
* @method StageInstance|null get(string $discrim, $key) Gets an item from the collection.
22+
* @method StageInstance|null first() Returns the first element of the collection.
23+
* @method StageInstance|null pull($key, $default = null) Pulls an item from the repository, removing and returning the item.
24+
* @method StageInstance|null find(callable $callback) Runs a filter callback over the repository.
25+
*/
26+
class StageInstanceRepository extends AbstractRepository
27+
{
28+
/**
29+
* @inheritdoc
30+
*/
31+
protected $discrim = 'channel_id';
32+
33+
/**
34+
* @inheritdoc
35+
*/
36+
protected $endpoints = [
37+
'get' => Endpoint::STAGE_INSTANCE,
38+
'create' => Endpoint::STAGE_INSTANCES,
39+
'update' => Endpoint::STAGE_INSTANCE,
40+
'delete' => Endpoint::STAGE_INSTANCE,
41+
];
42+
43+
/**
44+
* @inheritdoc
45+
*/
46+
protected $class = StageInstance::class;
47+
}

src/Discord/WebSockets/Event.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ abstract class Event
8383
public const VOICE_STATE_UPDATE = 'VOICE_STATE_UPDATE';
8484
public const VOICE_SERVER_UPDATE = 'VOICE_SERVER_UPDATE';
8585

86+
// Stage Instance
87+
public const STAGE_INSTANCE_CREATE = 'STAGE_INSTANCE_CREATE';
88+
public const STAGE_INSTANCE_UPDATE = 'STAGE_INSTANCE_UPDATE';
89+
public const STAGE_INSTANCE_DELETE = 'STAGE_INSTANCE_DELETE';
90+
8691
// Messages
8792
public const MESSAGE_CREATE = 'MESSAGE_CREATE';
8893
public const MESSAGE_DELETE = 'MESSAGE_DELETE';
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
/*
4+
* This file is a part of the DiscordPHP project.
5+
*
6+
* Copyright (c) 2015-present David Cole <david.cole1340@gmail.com>
7+
*
8+
* This file is subject to the MIT license that is bundled
9+
* with this source code in the LICENSE.md file.
10+
*/
11+
12+
namespace Discord\WebSockets\Events;
13+
14+
use Discord\Parts\Channel\StageInstance;
15+
use Discord\WebSockets\Event;
16+
use Discord\Helpers\Deferred;
17+
18+
class StageInstanceCreate extends Event
19+
{
20+
/**
21+
* @inheritdoc
22+
*/
23+
public function handle(Deferred &$deferred, $data): void
24+
{
25+
/** @var StageInstance */
26+
$stage_instance = $this->factory->create(StageInstance::class, $data, true);
27+
28+
if ($guild = $this->discord->guilds->get('id', $stage_instance->guild_id)) {
29+
$guild->stage_instances->push($stage_instance);
30+
}
31+
32+
$deferred->resolve($stage_instance);
33+
}
34+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
/*
4+
* This file is a part of the DiscordPHP project.
5+
*
6+
* Copyright (c) 2015-present David Cole <david.cole1340@gmail.com>
7+
*
8+
* This file is subject to the MIT license that is bundled
9+
* with this source code in the LICENSE.md file.
10+
*/
11+
12+
namespace Discord\WebSockets\Events;
13+
14+
use Discord\Parts\Channel\StageInstance;
15+
use Discord\WebSockets\Event;
16+
use Discord\Helpers\Deferred;
17+
18+
class StageInstanceDelete extends Event
19+
{
20+
/**
21+
* @inheritdoc
22+
*/
23+
public function handle(Deferred &$deferred, $data): void
24+
{
25+
$stage_instance = $this->factory->create(StageInstance::class, $data);
26+
27+
if ($guild = $stage_instance->guild) {
28+
$guild->stage_instances->pull($stage_instance->id);
29+
}
30+
31+
$deferred->resolve($stage_instance);
32+
}
33+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
/*
4+
* This file is a part of the DiscordPHP project.
5+
*
6+
* Copyright (c) 2015-present David Cole <david.cole1340@gmail.com>
7+
*
8+
* This file is subject to the MIT license that is bundled
9+
* with this source code in the LICENSE.md file.
10+
*/
11+
12+
namespace Discord\WebSockets\Events;
13+
14+
use Discord\Parts\Channel\StageInstance;
15+
use Discord\WebSockets\Event;
16+
use Discord\Helpers\Deferred;
17+
18+
class StageInstanceUpdate extends Event
19+
{
20+
/**
21+
* @inheritdoc
22+
*/
23+
public function handle(Deferred &$deferred, $data): void
24+
{
25+
$stage_instance = $this->factory->create(StageInstance::class, $data, true);
26+
27+
if ($guild = $this->discord->guilds->get('id', $stage_instance->guild_id)) {
28+
$old = $guild->stage_instances->get('id', $stage_instance->id);
29+
$guild->stage_instances->push($stage_instance);
30+
}
31+
32+
$deferred->resolve([$stage_instance, $old]);
33+
}
34+
}

src/Discord/WebSockets/Handlers.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ public function __construct()
8585
$this->addHandler(Event::THREAD_LIST_SYNC, \Discord\WebSockets\Events\ThreadListSync::class);
8686
$this->addHandler(Event::THREAD_MEMBER_UPDATE, \Discord\WebSockets\Events\ThreadMemberUpdate::class);
8787
$this->addHandler(Event::THREAD_MEMBERS_UPDATE, \Discord\WebSockets\Events\ThreadMembersUpdate::class);
88+
89+
// Stage Instance Event Handlers
90+
$this->addHandler(Event::STAGE_INSTANCE_CREATE, \Discord\WebSockets\Events\StageInstanceCreate::class);
91+
$this->addHandler(Event::STAGE_INSTANCE_UPDATE, \Discord\WebSockets\Events\StageInstanceUpdate::class);
92+
$this->addHandler(Event::STAGE_INSTANCE_DELETE, \Discord\WebSockets\Events\StageInstanceDelete::class);
8893
}
8994

9095
/**

src/Discord/WebSockets/Intents.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ class Intents
2626
* - CHANNEL_UPDATE
2727
* - CHANNEL_DELETE
2828
* - CHANNEL_PINS_UPDATE
29+
* - STAGE_INSTANCE_CREATE
30+
* - STAGE_INSTANCE_UPDATE
31+
* - STAGE_INSTANCE_DELETE
2932
*/
3033
public const GUILDS = (1 << 0);
3134

0 commit comments

Comments
 (0)