-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
229 lines (188 loc) · 6.52 KB
/
index.js
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
const { Plugin } = require('powercord/entities');
const {
getModule,
React,
constants
} = require('powercord/webpack');
const { Menu: { MenuItem } } = require('powercord/components');
const Permissions = Object.assign({}, constants.Permissions); // eslint-disable-line no-shadow
if (Permissions.MANAGE_GUILD) {
Permissions.MANAGE_SERVER = Permissions.MANAGE_GUILD;
delete Permissions.MANAGE_GUILD;
}
const { injectContextMenu, findInReactTree } = require("powercord/util");
const { uninject } = require('powercord/injector');
module.exports = class PermissionViewer extends Plugin {
async import (filter, functionName = filter) {
if (typeof filter === 'string') {
filter = [ filter ];
}
this[functionName] = (await getModule(filter))[functionName];
}
async doImport () {
await this.import('Messages');
await this.import('getMember');
await this.import('getGuild');
}
/* Whether or not permissions that are implied (by administrator) should be shown as well */
impliedPermissions = true;
getAllPermissionsRaw() {
return Object.values(Permissions).reduce((a, b) => a | b, 0n);
}
getPermissionsRaw (guildId, userId) {
let permissions = 0n;
const guild = this.getGuild(guildId);
const member = this.getMember(guildId, userId);
if (guild && member) {
if (guild.ownerId === userId) {
/* If they are the owner they have all the permissions */
return this.getAllPermissionsRaw();
}
/* @everyone is not inlcuded in the member's roles */
permissions |= guild.roles[guild.id].permissions;
for (const roleId of member.roles) {
permissions |= guild.roles[roleId].permissions;
}
if (this.impliedPermissions) {
if ((permissions & Permissions.ADMINISTRATOR) === Permissions.ADMINISTRATOR) {
return this.getAllPermissionsRaw();
}
}
}
return permissions;
}
toTitleCase (str) {
return str.replace(
/\w\S*/g,
(txt) => txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase()
);
}
getPermissions (guildId, userId) {
const raw = this.getPermissionsRaw(guildId, userId);
const permissions = {
raw,
entries: []
};
Object.keys(Permissions).forEach(key => {
if ((raw & Permissions[key]) === Permissions[key]) {
permissions.entries.push({
key,
readable: this.Messages[key] || this.toTitleCase(key.replace(/_/g, ' ')),
raw: Permissions[key]
});
}
});
return permissions;
}
getRolesWithPermission (guildId, permissions, roles = null) {
const withPermissions = [];
const guild = this.getGuild(guildId);
if (!roles) {
roles = guild.roles; // eslint-disable-line prefer-destructuring
}
for (let role of roles) {
if (typeof role === 'string') {
role = guild.roles[role];
}
if (role) {
const rolePermissions = role.permissions;
if ((rolePermissions & permissions) === permissions) {
withPermissions.push(role);
} else if (this.impliedPermissions) {
if ((rolePermissions & Permissions.ADMINISTRATOR) === Permissions.ADMINISTRATOR) {
withPermissions.push(role);
}
}
}
}
return withPermissions;
}
_injectContextMenu () {
injectContextMenu('permission-viewer-user', 'GuildChannelUserContextMenu', (args, res) => {
if (!res) return res;
// Attempt to find the context menu area containing the "Roles" item.
// If no such area is found (i.e. the user has no roles), then fall back
// to using the next menu area after the one containing "Block" or "Unblock"
// (the ID is the same regardless of whether a user is blocked).
const idsArray = ['roles', 'block', 'change-nickname'];
const menuItems = findInReactTree(res.props.children, e => {
return Array.isArray(e) && e.some(f => {
return Array.isArray(f?.props?.children) && f.props.children.some(g => {
return idsArray.includes(g?.props?.id);
});
});
});
let childIndex = 0;
let blockAreaIndex = 0;
const rolesMenuArea = menuItems.find(item => {
++childIndex;
// If the item is empty, we know it's not it
// The one we're looking for has an array of children
if (!(item && Array.isArray(item.props.children))) {
return false;
}
return item.props.children.some(c => {
if (c && c.props) {
if (c.props.id === 'roles') {
return true;
} else if (c.props.id === 'block' || c.props.id === 'change-nickname') {
blockAreaIndex = childIndex;
}
}
return false;
});
}) ?? menuItems[blockAreaIndex + 1];
const { guildId } = args[0];
const userId = args[0].user.id;
const member = this.getMember(guildId, userId);
if (member) {
const permissions = this.getPermissions(guildId, userId);
const items = [];
if (permissions.raw === 0n) {
items.push(React.createElement(MenuItem, {
id: 'none',
label: 'None'
}));
}
for (const permission of permissions.entries) {
const roles = this.getRolesWithPermission(guildId, permission.raw, member.roles.concat([ guildId ]));
if (roles.length > 0) {
items.push(React.createElement(MenuItem, {
id: permission.key.toLowerCase(),
label: permission.readable,
children: roles.map(role => React.createElement(MenuItem, {
id: role.id,
label: React.createElement("span", {
style: {
color: role.colorString
}
}, role.name)
}))
}));
} else {
items.push(React.createElement(MenuItem, {
id: permission.readable.toLowerCase(),
label: permission.readable
}));
}
}
if (!Array.isArray(rolesMenuArea.props.children)) {
rolesMenuArea.props.children = [rolesMenuArea.props.children];
}
rolesMenuArea.props.children.push(React.createElement(MenuItem, {
id: 'permissions',
label: 'Permissions',
children: items
}));
}
return res;
});
}
async startPlugin () {
await this.doImport();
this._injectContextMenu();
}
pluginWillUnload () {
uninject('permission-viewer-user');
}
};