aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/permissionsViewer/index.tsx
blob: 480efc1506752a010ed38098c99d42e0c82de723 (plain)
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
/*
 * 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";
import { getSortedRoles, sortPermissionOverwrites } from "./utils";

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) {
    if (type === MenuItemParentType.User && !GuildMemberStore.isMember(guildId, id!)) return null;

    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!);

                        permissions = sortPermissionOverwrites(Object.values(channel.permissionOverwrites).map(({ id, allow, deny, type }) => ({
                            type: type as PermissionType,
                            id,
                            overwriteAllow: allow,
                            overwriteDeny: deny
                        })), guildId);

                        header = channel.name;

                        break;
                    }

                    default: {
                        permissions = Object.values(guild.roles).map(role => ({
                            type: PermissionType.Role,
                            ...role
                        }));

                        header = guild.name;

                        break;
                    }
                }

                openRolesAndUsersPermissionsModal(permissions, guild, header);
            }}
        />
    );
}

function makeContextMenuPatch(childId: string | string[], type?: MenuItemParentType): NavContextMenuPatchCallback {
    return (children, props) => () => {
        if (!props) return children;

        const group = findGroupChildrenByChildId(childId, children);

        const item = (() => {
            switch (type) {
                case MenuItemParentType.User:
                    return MenuItem(props.guildId, props.user.id, type);
                case MenuItemParentType.Channel:
                    return MenuItem(props.guild.id, props.channel.id, type);
                case MenuItemParentType.Guild:
                    return MenuItem(props.guild.id);
                default:
                    return null;
            }
        })();

        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>);
    };
}

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: [
        {
            find: ".Messages.BOT_PROFILE_SLASH_COMMANDS",
            replacement: {
                match: /showBorder:.{0,60}}\),(?<=guild:(\i),guildMember:(\i),.+?)/,
                replace: (m, guild, guildMember) => `${m}$self.UserPermissions(${guild},${guildMember}),`
            }
        }
    ],

    UserPermissions: (guild: Guild, guildMember?: GuildMember) => !!guildMember && <UserPermissions guild={guild} guildMember={guildMember} />,

    userContextMenuPatch: makeContextMenuPatch("roles", MenuItemParentType.User),
    channelContextMenuPatch: makeContextMenuPatch(["mute-channel", "unmute-channel"], MenuItemParentType.Channel),
    guildContextMenuPatch: makeContextMenuPatch("privacy", MenuItemParentType.Guild),

    start() {
        addContextMenuPatch("user-context", this.userContextMenuPatch);
        addContextMenuPatch("channel-context", this.channelContextMenuPatch);
        addContextMenuPatch("guild-context", this.guildContextMenuPatch);
    },

    stop() {
        removeContextMenuPatch("user-context", this.userContextMenuPatch);
        removeContextMenuPatch("channel-context", this.channelContextMenuPatch);
        removeContextMenuPatch("guild-context", this.guildContextMenuPatch);
    },
});