2023-05-15 00:33:04 +00:00
|
|
|
/*
|
|
|
|
* Vencord, a modification for Discord's desktop app
|
|
|
|
* Copyright (c) 2023 Vendicated and contributors
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import "./styles.css";
|
|
|
|
|
|
|
|
import { addContextMenuPatch, findGroupChildrenByChildId, NavContextMenuPatchCallback, removeContextMenuPatch } from "@api/ContextMenu";
|
|
|
|
import { definePluginSettings } from "@api/Settings";
|
|
|
|
import { Devs } from "@utils/constants";
|
|
|
|
import definePlugin, { OptionType } from "@utils/types";
|
|
|
|
import { ChannelStore, GuildMemberStore, GuildStore, Menu, PermissionsBits, UserStore } from "@webpack/common";
|
|
|
|
import type { Guild, GuildMember } from "discord-types/general";
|
|
|
|
|
|
|
|
import openRolesAndUsersPermissionsModal, { PermissionType, RoleOrUserPermission } from "./components/RolesAndUsersPermissions";
|
|
|
|
import UserPermissions from "./components/UserPermissions";
|
2023-05-20 00:24:56 +00:00
|
|
|
import { getSortedRoles, sortPermissionOverwrites } from "./utils";
|
2023-05-15 00:33:04 +00:00
|
|
|
|
|
|
|
export const enum PermissionsSortOrder {
|
|
|
|
HighestRole,
|
|
|
|
LowestRole
|
|
|
|
}
|
|
|
|
|
|
|
|
const enum MenuItemParentType {
|
|
|
|
User,
|
|
|
|
Channel,
|
|
|
|
Guild
|
|
|
|
}
|
|
|
|
|
|
|
|
export const settings = definePluginSettings({
|
|
|
|
permissionsSortOrder: {
|
|
|
|
description: "The sort method used for defining which role grants an user a certain permission",
|
|
|
|
type: OptionType.SELECT,
|
|
|
|
options: [
|
|
|
|
{ label: "Highest Role", value: PermissionsSortOrder.HighestRole, default: true },
|
|
|
|
{ label: "Lowest Role", value: PermissionsSortOrder.LowestRole }
|
|
|
|
],
|
|
|
|
},
|
|
|
|
defaultPermissionsDropdownState: {
|
|
|
|
description: "Whether the permissions dropdown on user popouts should be open by default",
|
|
|
|
type: OptionType.BOOLEAN,
|
|
|
|
default: false,
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
function MenuItem(guildId: string, id?: string, type?: MenuItemParentType) {
|
2023-05-15 22:19:20 +00:00
|
|
|
if (type === MenuItemParentType.User && !GuildMemberStore.isMember(guildId, id!)) return null;
|
|
|
|
|
2023-05-15 00:33:04 +00:00
|
|
|
return (
|
|
|
|
<Menu.MenuItem
|
|
|
|
id="perm-viewer-permissions"
|
|
|
|
label="Permissions"
|
|
|
|
action={() => {
|
|
|
|
const guild = GuildStore.getGuild(guildId);
|
|
|
|
|
|
|
|
let permissions: RoleOrUserPermission[];
|
|
|
|
let header: string;
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case MenuItemParentType.User: {
|
|
|
|
const member = GuildMemberStore.getMember(guildId, id!);
|
|
|
|
|
|
|
|
permissions = getSortedRoles(guild, member)
|
|
|
|
.map(role => ({
|
|
|
|
type: PermissionType.Role,
|
|
|
|
...role
|
|
|
|
}));
|
|
|
|
|
|
|
|
if (guild.ownerId === id) {
|
|
|
|
permissions.push({
|
|
|
|
type: PermissionType.Owner,
|
|
|
|
permissions: Object.values(PermissionsBits).reduce((prev, curr) => prev | curr, 0n)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
header = member.nick ?? UserStore.getUser(member.userId).username;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case MenuItemParentType.Channel: {
|
|
|
|
const channel = ChannelStore.getChannel(id!);
|
|
|
|
|
2023-05-20 00:24:56 +00:00
|
|
|
permissions = sortPermissionOverwrites(Object.values(channel.permissionOverwrites).map(({ id, allow, deny, type }) => ({
|
2023-05-15 00:33:04 +00:00
|
|
|
type: type as PermissionType,
|
|
|
|
id,
|
|
|
|
overwriteAllow: allow,
|
|
|
|
overwriteDeny: deny
|
2023-05-20 00:24:56 +00:00
|
|
|
})), guildId);
|
2023-05-15 00:33:04 +00:00
|
|
|
|
|
|
|
header = channel.name;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default: {
|
|
|
|
permissions = Object.values(guild.roles).map(role => ({
|
|
|
|
type: PermissionType.Role,
|
|
|
|
...role
|
|
|
|
}));
|
|
|
|
|
|
|
|
header = guild.name;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
openRolesAndUsersPermissionsModal(permissions, guild, header);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-05-15 22:19:20 +00:00
|
|
|
function makeContextMenuPatch(childId: string | string[], type?: MenuItemParentType): NavContextMenuPatchCallback {
|
2023-05-15 00:33:04 +00:00
|
|
|
return (children, props) => () => {
|
2023-09-25 23:48:09 +00:00
|
|
|
if (!props || (type === MenuItemParentType.User && !props.user) || (type === MenuItemParentType.Guild && !props.guild)) return children;
|
2023-05-15 00:33:04 +00:00
|
|
|
|
|
|
|
const group = findGroupChildrenByChildId(childId, children);
|
|
|
|
|
2023-05-15 22:19:20 +00:00
|
|
|
const item = (() => {
|
2023-05-15 00:33:04 +00:00
|
|
|
switch (type) {
|
|
|
|
case MenuItemParentType.User:
|
2023-05-15 22:19:20 +00:00
|
|
|
return MenuItem(props.guildId, props.user.id, type);
|
2023-05-15 00:33:04 +00:00
|
|
|
case MenuItemParentType.Channel:
|
2023-05-15 22:19:20 +00:00
|
|
|
return MenuItem(props.guild.id, props.channel.id, type);
|
2023-05-15 00:33:04 +00:00
|
|
|
case MenuItemParentType.Guild:
|
2023-05-15 22:19:20 +00:00
|
|
|
return MenuItem(props.guild.id);
|
|
|
|
default:
|
|
|
|
return null;
|
2023-05-15 00:33:04 +00:00
|
|
|
}
|
2023-05-15 22:19:20 +00:00
|
|
|
})();
|
|
|
|
|
|
|
|
if (item == null) return;
|
|
|
|
|
|
|
|
if (group)
|
|
|
|
group.push(item);
|
|
|
|
else if (childId === "roles" && props.guildId)
|
|
|
|
// "roles" may not be present due to the member not having any roles. In that case, add it above "Copy ID"
|
|
|
|
children.splice(-1, 0, <Menu.MenuGroup>{item}</Menu.MenuGroup>);
|
2023-05-15 00:33:04 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export default definePlugin({
|
|
|
|
name: "PermissionsViewer",
|
|
|
|
description: "View the permissions a user or channel has, and the roles of a server",
|
|
|
|
authors: [Devs.Nuckyz, Devs.Ven],
|
|
|
|
settings,
|
|
|
|
|
|
|
|
patches: [
|
|
|
|
{
|
2023-10-25 17:54:13 +00:00
|
|
|
find: ".popularApplicationCommandIds,",
|
2023-05-15 00:33:04 +00:00
|
|
|
replacement: {
|
2023-11-21 02:26:07 +00:00
|
|
|
match: /showBorder:(.{0,60})}\),(?<=guild:(\i),guildMember:(\i),.+?)/,
|
|
|
|
replace: (m, showBoder, guild, guildMember) => `${m}$self.UserPermissions(${guild},${guildMember},${showBoder}),`
|
2023-05-15 00:33:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
],
|
|
|
|
|
2023-11-21 02:26:07 +00:00
|
|
|
UserPermissions: (guild: Guild, guildMember: GuildMember | undefined, showBoder: boolean) => !!guildMember && <UserPermissions guild={guild} guildMember={guildMember} showBorder={showBoder} />,
|
2023-05-15 00:33:04 +00:00
|
|
|
|
|
|
|
userContextMenuPatch: makeContextMenuPatch("roles", MenuItemParentType.User),
|
2023-05-15 22:19:20 +00:00
|
|
|
channelContextMenuPatch: makeContextMenuPatch(["mute-channel", "unmute-channel"], MenuItemParentType.Channel),
|
2023-05-15 00:33:04 +00:00
|
|
|
guildContextMenuPatch: makeContextMenuPatch("privacy", MenuItemParentType.Guild),
|
|
|
|
|
|
|
|
start() {
|
|
|
|
addContextMenuPatch("user-context", this.userContextMenuPatch);
|
|
|
|
addContextMenuPatch("channel-context", this.channelContextMenuPatch);
|
2023-09-05 16:34:12 +00:00
|
|
|
addContextMenuPatch(["guild-context", "guild-header-popout"], this.guildContextMenuPatch);
|
2023-05-15 00:33:04 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
stop() {
|
|
|
|
removeContextMenuPatch("user-context", this.userContextMenuPatch);
|
|
|
|
removeContextMenuPatch("channel-context", this.channelContextMenuPatch);
|
2023-09-05 16:34:12 +00:00
|
|
|
removeContextMenuPatch(["guild-context", "guild-header-popout"], this.guildContextMenuPatch);
|
2023-05-15 00:33:04 +00:00
|
|
|
},
|
|
|
|
});
|