Add workaround for guild role api changes on canary/ptb

fixes RoleColorEverywhere, ServerInfo, PermissionViewer, BetterRoleContext
This commit is contained in:
Vendicated 2024-03-11 16:13:07 +01:00
parent 497f0de9a1
commit 34390e0365
No known key found for this signature in database
GPG key ID: D66986BAF75ECF18
8 changed files with 35 additions and 18 deletions

View file

@ -5,7 +5,7 @@
*/
import { Devs } from "@utils/constants";
import { getCurrentGuild } from "@utils/discord";
import { getCurrentGuild, getGuildRoles } from "@utils/discord";
import definePlugin from "@utils/types";
import { findByPropsLazy } from "@webpack";
import { Clipboard, Menu, PermissionStore, TextAndImagesSettingsStores } from "@webpack/common";
@ -47,7 +47,9 @@ export default definePlugin({
contextMenus: {
"dev-context"(children, { id }: { id: string; }) {
const guild = getCurrentGuild();
const role = guild?.roles[id];
if (!guild) return;
const role = getGuildRoles(guild.id)[id];
if (!role) return;
if (role.colorString) {

View file

@ -19,7 +19,7 @@
import ErrorBoundary from "@components/ErrorBoundary";
import { Flex } from "@components/Flex";
import { InfoIcon, OwnerCrownIcon } from "@components/Icons";
import { getUniqueUsername } from "@utils/discord";
import { getGuildRoles, getUniqueUsername } from "@utils/discord";
import { ModalCloseButton, ModalContent, ModalHeader, ModalProps, ModalRoot, ModalSize, openModal } from "@utils/modal";
import { ContextMenuApi, FluxDispatcher, GuildMemberStore, Menu, PermissionsBits, Text, Tooltip, useEffect, UserStore, useState, useStateFromStores } from "@webpack/common";
import type { Guild } from "discord-types/general";
@ -78,6 +78,8 @@ function RolesAndUsersPermissionsComponent({ permissions, guild, modalProps, hea
const [selectedItemIndex, selectItem] = useState(0);
const selectedItem = permissions[selectedItemIndex];
const roles = getGuildRoles(guild.id);
return (
<ModalRoot
{...modalProps}
@ -100,7 +102,7 @@ function RolesAndUsersPermissionsComponent({ permissions, guild, modalProps, hea
<div className={cl("perms-list")}>
{permissions.map((permission, index) => {
const user = UserStore.getUser(permission.id ?? "");
const role = guild.roles[permission.id ?? ""];
const role = roles[permission.id ?? ""];
return (
<button
@ -201,7 +203,7 @@ function RoleContextMenu({ guild, roleId, onClose }: { guild: Guild; roleId: str
id="vc-pw-view-as-role"
label="View As Role"
action={() => {
const role = guild.roles[roleId];
const role = getGuildRoles(guild.id)[roleId];
if (!role) return;
onClose();

View file

@ -21,6 +21,7 @@ import "./styles.css";
import { findGroupChildrenByChildId, NavContextMenuPatchCallback } from "@api/ContextMenu";
import { definePluginSettings } from "@api/Settings";
import { Devs } from "@utils/constants";
import { getGuildRoles } from "@utils/discord";
import definePlugin, { OptionType } from "@utils/types";
import { ChannelStore, GuildMemberStore, GuildStore, Menu, PermissionsBits, UserStore } from "@webpack/common";
import type { Guild, GuildMember } from "discord-types/general";
@ -107,7 +108,7 @@ function MenuItem(guildId: string, id?: string, type?: MenuItemParentType) {
}
default: {
permissions = Object.values(guild.roles).map(role => ({
permissions = Object.values(getGuildRoles(guild.id)).map(role => ({
type: PermissionType.Role,
...role
}));

View file

@ -17,8 +17,9 @@
*/
import { classNameFactory } from "@api/Styles";
import { getGuildRoles } from "@utils/discord";
import { wordsToTitle } from "@utils/text";
import { GuildStore, i18n, Parser } from "@webpack/common";
import { i18n, Parser } from "@webpack/common";
import { Guild, GuildMember, Role } from "discord-types/general";
import type { ReactNode } from "react";
@ -67,7 +68,9 @@ export function getPermissionDescription(permission: string): ReactNode {
return "";
}
export function getSortedRoles({ roles, id }: Guild, member: GuildMember) {
export function getSortedRoles({ id }: Guild, member: GuildMember) {
const roles = getGuildRoles(id);
return [...member.roles, id]
.map(id => roles[id])
.sort((a, b) => b.position - a.position);
@ -85,13 +88,13 @@ export function sortUserRoles(roles: Role[]) {
}
export function sortPermissionOverwrites<T extends { id: string; type: number; }>(overwrites: T[], guildId: string) {
const guild = GuildStore.getGuild(guildId);
const roles = getGuildRoles(guildId);
return overwrites.sort((a, b) => {
if (a.type !== PermissionType.Role || b.type !== PermissionType.Role) return 0;
const roleA = guild.roles[a.id];
const roleB = guild.roles[b.id];
const roleA = roles[a.id];
const roleB = roles[b.id];
return roleB.position - roleA.position;
});

View file

@ -19,8 +19,9 @@
import { definePluginSettings } from "@api/Settings";
import ErrorBoundary from "@components/ErrorBoundary";
import { Devs } from "@utils/constants";
import { getGuildRoles } from "@utils/discord";
import definePlugin, { OptionType } from "@utils/types";
import { ChannelStore, GuildMemberStore, GuildStore } from "@webpack/common";
import { ChannelStore, GuildMemberStore } from "@webpack/common";
const settings = definePluginSettings({
chatMentions: {
@ -114,8 +115,7 @@ export default definePlugin({
},
roleGroupColor: ErrorBoundary.wrap(({ id, count, title, guildId, label }: { id: string; count: number; title: string; guildId: string; label: string; }) => {
const guild = GuildStore.getGuild(guildId);
const role = guild?.roles[id];
const role = getGuildRoles(guildId)[id];
return (
<span style={{

View file

@ -7,7 +7,7 @@
import "./styles.css";
import { classNameFactory } from "@api/Styles";
import { openImageModal, openUserProfile } from "@utils/discord";
import { getGuildRoles, openImageModal, openUserProfile } from "@utils/discord";
import { classes } from "@utils/misc";
import { ModalRoot, ModalSize, openModal } from "@utils/modal";
import { useAwaiter } from "@utils/react";
@ -172,7 +172,7 @@ function ServerInfoTab({ guild }: GuildProps) {
"Verification Level": ["None", "Low", "Medium", "High", "Highest"][guild.verificationLevel] || "?",
"Nitro Boosts": `${guild.premiumSubscriberCount ?? 0} (Level ${guild.premiumTier ?? 0})`,
"Channels": GuildChannelStore.getChannels(guild.id)?.count - 1 || "?", // - null category
"Roles": Object.keys(guild.roles).length - 1, // - @everyone
"Roles": Object.keys(getGuildRoles(guild.id)).length - 1, // - @everyone
};
return (

View file

@ -7,6 +7,7 @@
import { definePluginSettings } from "@api/Settings";
import { makeRange } from "@components/PluginSettings/components";
import { Devs } from "@utils/constants";
import { getGuildRoles } from "@utils/discord";
import { Logger } from "@utils/Logger";
import definePlugin, { OptionType, PluginNative } from "@utils/types";
import { findByPropsLazy } from "@webpack";
@ -196,7 +197,7 @@ export default definePlugin({
if (message.mention_roles.length > 0) {
for (const roleId of message.mention_roles) {
const role = GuildStore.getGuild(channel.guild_id).roles[roleId];
const role = getGuildRoles(channel.guild_id)[roleId];
if (!role) continue;
const roleColor = role.colorString ?? `#${pingColor}`;
finalMsg = finalMsg.replace(`<@&${roleId}>`, `<b><color=${roleColor}>@${role.name}</color></b>`);

View file

@ -18,7 +18,7 @@
import { MessageObject } from "@api/MessageEvents";
import { ChannelStore, ComponentDispatch, FluxDispatcher, GuildStore, InviteActions, MaskedLink, MessageActions, ModalImageClasses, PrivateChannelsStore, RestAPI, SelectedChannelStore, SelectedGuildStore, UserProfileActions, UserProfileStore, UserSettingsActionCreators, UserUtils } from "@webpack/common";
import { Guild, Message, User } from "discord-types/general";
import { Guild, Message, Role, User } from "discord-types/general";
import { ImageModal, ModalRoot, ModalSize, openModal } from "./modal";
@ -185,3 +185,11 @@ export async function fetchUserProfile(id: string, options?: FetchUserProfileOpt
export function getUniqueUsername(user: User) {
return user.discriminator === "0" ? user.username : user.tag;
}
// FIXME: remove this once discord merges the role change into stable
export function getGuildRoles(guildId: string): Record<string, Role> {
if ("getRoles" in GuildStore)
return (GuildStore as any).getRoles(guildId);
return GuildStore.getGuild(guildId)?.roles ?? {};
}