-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathRole.php
233 lines (186 loc) · 6.8 KB
/
Role.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<?php
/*
* DiscordBot, PocketMine-MP Plugin.
*
* Licensed under the Open Software License version 3.0 (OSL-3.0)
* Copyright (C) 2020-present JaxkDev
*
* Discord :: JaxkDev
* Email :: [email protected]
*/
namespace JaxkDev\DiscordBot\Models;
use JaxkDev\DiscordBot\Communication\BinarySerializable;
use JaxkDev\DiscordBot\Communication\BinaryStream;
use JaxkDev\DiscordBot\Models\Permissions\RolePermissions;
use JaxkDev\DiscordBot\Plugin\Utils;
/**
* @implements BinarySerializable<Role>
* @link https://discord.com/developers/docs/topics/permissions#role-object
*/
final class Role implements BinarySerializable{
public const SERIALIZE_ID = 8;
private string $id;
/** The guild this role is part of, used for internal mapping. */
private string $guild_id;
/** Role name */
private string $name;
/** Integer representation of hexadecimal color code */
private int $colour;
/** If this role is pinned in the user listing */
private bool $hoist;
/** Role icon or role icon image data when updating icon. */
private ?string $icon;
/** Role unicode emoji */
private ?string $unicode_emoji;
/** Position of this role */
private int $position;
/** Permissions */
private RolePermissions $permissions;
/** Whether this role is managed by an integration */
private bool $managed;
/** Whether this role is mentionable */
private bool $mentionable;
/** The tags this role has */
private ?RoleTags $tags;
/**
* @internal See API::createRole()
* @see API::createRole()
*/
public function __construct(string $id, string $guild_id, string $name, int $colour, bool $hoist, ?string $icon,
?string $unicode_emoji, int $position, RolePermissions $permissions, bool $managed,
bool $mentionable, ?RoleTags $tags){
$this->setId($id);
$this->setGuildId($guild_id);
$this->setName($name);
$this->setColour($colour);
$this->setHoist($hoist);
$this->setIcon($icon);
$this->setUnicodeEmoji($unicode_emoji);
$this->setPosition($position);
$this->setPermissions($permissions);
$this->setManaged($managed);
$this->setMentionable($mentionable);
$this->setTags($tags);
}
public function getId(): string{
return $this->id;
}
public function setId(string $id): void{
if(!Utils::validDiscordSnowflake($id)){
throw new \AssertionError("Role ID '$id' is invalid.");
}
$this->id = $id;
}
public function getGuildId(): string{
return $this->guild_id;
}
public function setGuildId(string $guild_id): void{
if(!Utils::validDiscordSnowflake($guild_id)){
throw new \AssertionError("Guild ID '$guild_id' is invalid.");
}
$this->guild_id = $guild_id;
}
public function getName(): string{
return $this->name;
}
public function setName(string $name): void{
$this->name = $name;
}
public function getColour(): int{
return $this->colour;
}
/** @param int $colour Hex [0x000000 - 0xFFFFFF] */
public function setColour(int $colour): void{
if($colour < 0 || $colour > 0xFFFFFF){
throw new \AssertionError("Colour '$colour' is outside the bounds 0x000000-0xFFFFFF.");
}
$this->colour = $colour;
}
public function getHoist(): bool{
return $this->hoist;
}
public function setHoist(bool $hoist): void{
$this->hoist = $hoist;
}
public function getIconUrl(): ?string{
return ($this->icon === null || !Utils::validImageData($this->icon)) ? null : "https://cdn.discordapp.com/role-icons/{$this->id}/{$this->icon}.png";
}
public function getIcon(): ?string{
return $this->icon;
}
public function setIcon(?string $icon): void{
$this->icon = $icon;
}
public function getUnicodeEmoji(): ?string{
return $this->unicode_emoji;
}
public function setUnicodeEmoji(?string $unicode_emoji): void{
$this->unicode_emoji = $unicode_emoji;
}
public function getPosition(): int{
return $this->position;
}
public function setPosition(int $position): void{
$this->position = $position;
}
public function getPermissions(): RolePermissions{
return $this->permissions;
}
public function setPermissions(RolePermissions $permissions): void{
$this->permissions = $permissions;
}
public function getManaged(): bool{
return $this->managed;
}
public function setManaged(bool $managed): void{
$this->managed = $managed;
}
public function getMentionable(): bool{
return $this->mentionable;
}
public function setMentionable(bool $mentionable): void{
$this->mentionable = $mentionable;
}
public function getTags(): ?RoleTags{
return $this->tags;
}
public function setTags(?RoleTags $tags): void{
$this->tags = $tags;
}
public function __toString(): string{
return "<@&{$this->id}>";
}
//----- Serialization -----//
public function binarySerialize(): BinaryStream{
$stream = new BinaryStream();
$stream->putString($this->id);
$stream->putString($this->guild_id);
$stream->putString($this->name);
$stream->putInt($this->colour);
$stream->putBool($this->hoist);
$stream->putNullableString($this->icon);
$stream->putNullableString($this->unicode_emoji);
$stream->putInt($this->position);
$stream->putSerializable($this->permissions);
$stream->putBool($this->managed);
$stream->putBool($this->mentionable);
$stream->putNullableSerializable($this->tags);
return $stream;
}
public static function fromBinary(BinaryStream $stream): self{
return new self(
$stream->getString(), // id
$stream->getString(), // guild_id
$stream->getString(), // name
$stream->getInt(), // colour
$stream->getBool(), // hoist
$stream->getNullableString(), // icon
$stream->getNullableString(), // unicode_emoji
$stream->getInt(), // position
$stream->getSerializable(RolePermissions::class), // permissions
$stream->getBool(), // managed
$stream->getBool(), // mentionable
$stream->getNullableSerializable(RoleTags::class) // tags
);
}
}