-
-
Notifications
You must be signed in to change notification settings - Fork 252
Expand file tree
/
Copy pathPermission.php
More file actions
211 lines (189 loc) · 5.49 KB
/
Permission.php
File metadata and controls
211 lines (189 loc) · 5.49 KB
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
<?php
/*
* This file is a part of the DiscordPHP project.
*
* Copyright (c) 2015-present David Cole <david.cole1340@gmail.com>
*
* This file is subject to the MIT license that is bundled
* with this source code in the LICENSE.md file.
*/
namespace Discord\Parts\Permissions;
use Discord\Discord;
use Discord\Helpers\BigInt;
use Discord\Parts\Part;
/**
* Permission represents a set of permissions for a given role or overwrite.
*
* @link https://discord.com/developers/docs/topics/permissions
*
* @since 2.1.3 Namespace moved from Guild to Permissions
* @since 2.0.0
*
* @property int|string $bitwise
* @property bool $create_instant_invite
* @property bool $manage_channels
* @property bool $view_channel
* @property bool $mention_everyone
* @property bool $manage_roles
*/
abstract class Permission extends Part
{
/**
* Array of permissions that only apply to stage channels.
* i.e. indicated S in documentation.
*
* @var array
*/
public const STAGE_PERMISSIONS = [
'stream' => 9,
'connect' => 20,
'mute_members' => 22,
'move_members' => 24,
'request_to_speak' => 32,
'manage_events' => 33,
];
/**
* Array of permissions that only apply to voice channels.
* i.e. indicated V in documentation.
*
* @var array
*/
public const VOICE_PERMISSIONS = [
'priority_speaker' => 8,
'stream' => 9,
'connect' => 20,
'speak' => 21,
'mute_members' => 22,
'deafen_members' => 23,
'move_members' => 24,
'use_vad' => 25,
'manage_events' => 33,
'use_embedded_activities' => 39,
];
/**
* Array of permissions that only apply to text channels.
* i.e. indicated T in documentation.
*
* @var array
*/
public const TEXT_PERMISSIONS = [
'manage_threads' => 34,
'create_public_threads' => 35,
'create_private_threads' => 36,
'send_messages_in_threads' => 38,
];
/**
* Array of permissions that can only be applied to roles.
* i.e. indicated empty in documentation.
*
* @var array
*/
public const ROLE_PERMISSIONS = [
'kick_members' => 1,
'ban_members' => 2,
'administrator' => 3,
'manage_guild' => 5,
'view_audit_log' => 7,
'view_guild_insights' => 19,
'change_nickname' => 26,
'manage_nicknames' => 27,
'manage_emojis_and_stickers' => 30,
'moderate_members' => 40,
'view_creator_monetization_analytics' => 41,
];
/**
* Array of permissions for all roles.
* i.e. indicated T,V,S in documentation.
*
* @var array
*/
public const ALL_PERMISSIONS = [
'create_instant_invite' => 0,
'manage_channels' => 4,
'add_reactions' => 6,
'view_channel' => 10,
'send_messages' => 11,
'send_tts_messages' => 12,
'manage_messages' => 13,
'embed_links' => 14,
'attach_files' => 15,
'read_message_history' => 16,
'mention_everyone' => 17,
'use_external_emojis' => 18,
'manage_roles' => 28,
'manage_webhooks' => 29,
'use_application_commands' => 31,
'use_external_stickers' => 37,
];
/**
* Array of permissions.
*
* @var array
*/
private $permissions = [];
/**
* {@inheritDoc}
*/
public function __construct(Discord $discord, array $attributes = [], bool $created = false)
{
$this->permissions = $this->getPermissions();
$this->fillable = array_keys($this->permissions);
$this->fillable[] = 'bitwise';
parent::__construct($discord, $attributes, $created);
foreach ($this->fillable as $permission) {
if (! isset($this->attributes[$permission])) {
$this->attributes[$permission] = false;
}
}
}
/**
* Returns an array of extra permissions.
*
* @return array
*/
abstract public static function getPermissions(): array;
/**
* Gets the bitwise attribute of the permission.
*
* @link https://discord.com/developers/docs/topics/permissions#permissions-bitwise-permission-flags
*
* @return int|string
*/
protected function getBitwiseAttribute()
{
if (BigInt::is32BitWithGMP()) { // x86 with GMP
$bitwise = \gmp_init(0);
foreach ($this->permissions as $permission => $value) {
\gmp_setbit($bitwise, $value, $this->attributes[$permission]);
}
return \gmp_strval($bitwise);
}
$bitwise = 0;
foreach ($this->permissions as $permission => $value) {
if ($this->attributes[$permission]) {
$bitwise |= 1 << $value;
}
}
return $bitwise;
}
/**
* Sets the bitwise attribute of the permission.
*
* @link https://discord.com/developers/docs/topics/permissions#permissions-bitwise-permission-flags
*
* @param int|string $bitwise
*/
protected function setBitwiseAttribute($bitwise)
{
if (PHP_INT_SIZE === 8 && is_string($bitwise)) { // x64
$bitwise = (int) $bitwise;
}
foreach ($this->permissions as $permission => $value) {
$this->attributes[$permission] = BigInt::test($bitwise, $value);
}
}
public function __toString(): string
{
return (string) $this->bitwise;
}
}