diff --git a/src/plugins/betterRoleContext/index.tsx b/src/plugins/betterRoleContext/index.tsx index e73779ad..3db3494f 100644 --- a/src/plugins/betterRoleContext/index.tsx +++ b/src/plugins/betterRoleContext/index.tsx @@ -5,10 +5,10 @@ */ import { Devs } from "@utils/constants"; -import { getCurrentGuild, getGuildRoles } from "@utils/discord"; +import { getCurrentGuild } from "@utils/discord"; import definePlugin from "@utils/types"; import { findByPropsLazy } from "@webpack"; -import { Clipboard, Menu, PermissionStore, TextAndImagesSettingsStores } from "@webpack/common"; +import { Clipboard, GuildStore, Menu, PermissionStore, TextAndImagesSettingsStores } from "@webpack/common"; const GuildSettingsActions = findByPropsLazy("open", "selectRole", "updateGuild"); @@ -49,7 +49,7 @@ export default definePlugin({ const guild = getCurrentGuild(); if (!guild) return; - const role = getGuildRoles(guild.id)[id]; + const role = GuildStore.getRole(guild.id, id); if (!role) return; if (role.colorString) { diff --git a/src/plugins/permissionsViewer/components/RolesAndUsersPermissions.tsx b/src/plugins/permissionsViewer/components/RolesAndUsersPermissions.tsx index adadc90b..c2e50ced 100644 --- a/src/plugins/permissionsViewer/components/RolesAndUsersPermissions.tsx +++ b/src/plugins/permissionsViewer/components/RolesAndUsersPermissions.tsx @@ -19,9 +19,9 @@ import ErrorBoundary from "@components/ErrorBoundary"; import { Flex } from "@components/Flex"; import { InfoIcon, OwnerCrownIcon } from "@components/Icons"; -import { getGuildRoles, getUniqueUsername } from "@utils/discord"; +import { 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 { ContextMenuApi, FluxDispatcher, GuildMemberStore, GuildStore, Menu, PermissionsBits, Text, Tooltip, useEffect, UserStore, useState, useStateFromStores } from "@webpack/common"; import type { Guild } from "discord-types/general"; import { settings } from ".."; @@ -78,7 +78,7 @@ function RolesAndUsersPermissionsComponent({ permissions, guild, modalProps, hea const [selectedItemIndex, selectItem] = useState(0); const selectedItem = permissions[selectedItemIndex]; - const roles = getGuildRoles(guild.id); + const roles = GuildStore.getRoles(guild.id); return ( { - const role = getGuildRoles(guild.id)[roleId]; + const role = GuildStore.getRole(guild.id, roleId); if (!role) return; onClose(); diff --git a/src/plugins/permissionsViewer/index.tsx b/src/plugins/permissionsViewer/index.tsx index 79ff2a27..b27a3c2f 100644 --- a/src/plugins/permissionsViewer/index.tsx +++ b/src/plugins/permissionsViewer/index.tsx @@ -21,7 +21,6 @@ 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"; @@ -108,7 +107,7 @@ function MenuItem(guildId: string, id?: string, type?: MenuItemParentType) { } default: { - permissions = Object.values(getGuildRoles(guild.id)).map(role => ({ + permissions = Object.values(GuildStore.getRoles(guild.id)).map(role => ({ type: PermissionType.Role, ...role })); diff --git a/src/plugins/permissionsViewer/utils.ts b/src/plugins/permissionsViewer/utils.ts index 11dc7e64..ac753797 100644 --- a/src/plugins/permissionsViewer/utils.ts +++ b/src/plugins/permissionsViewer/utils.ts @@ -17,9 +17,8 @@ */ import { classNameFactory } from "@api/Styles"; -import { getGuildRoles } from "@utils/discord"; import { wordsToTitle } from "@utils/text"; -import { i18n, Parser } from "@webpack/common"; +import { GuildStore, i18n, Parser } from "@webpack/common"; import { Guild, GuildMember, Role } from "discord-types/general"; import type { ReactNode } from "react"; @@ -69,7 +68,7 @@ export function getPermissionDescription(permission: string): ReactNode { } export function getSortedRoles({ id }: Guild, member: GuildMember) { - const roles = getGuildRoles(id); + const roles = GuildStore.getRoles(id); return [...member.roles, id] .map(id => roles[id]) @@ -88,7 +87,7 @@ export function sortUserRoles(roles: Role[]) { } export function sortPermissionOverwrites(overwrites: T[], guildId: string) { - const roles = getGuildRoles(guildId); + const roles = GuildStore.getRoles(guildId); return overwrites.sort((a, b) => { if (a.type !== PermissionType.Role || b.type !== PermissionType.Role) return 0; diff --git a/src/plugins/roleColorEverywhere/index.tsx b/src/plugins/roleColorEverywhere/index.tsx index eea33fbd..b421eb7f 100644 --- a/src/plugins/roleColorEverywhere/index.tsx +++ b/src/plugins/roleColorEverywhere/index.tsx @@ -19,9 +19,8 @@ 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 } from "@webpack/common"; +import { ChannelStore, GuildMemberStore, GuildStore } from "@webpack/common"; const settings = definePluginSettings({ chatMentions: { @@ -115,7 +114,7 @@ export default definePlugin({ }, roleGroupColor: ErrorBoundary.wrap(({ id, count, title, guildId, label }: { id: string; count: number; title: string; guildId: string; label: string; }) => { - const role = getGuildRoles(guildId)[id]; + const role = GuildStore.getRole(guildId, id); return ( 0) { for (const roleId of message.mention_roles) { - const role = getGuildRoles(channel.guild_id)[roleId]; + const role = GuildStore.getRole(channel.guild_id, roleId); if (!role) continue; const roleColor = role.colorString ?? `#${pingColor}`; finalMsg = finalMsg.replace(`<@&${roleId}>`, `@${role.name}`); diff --git a/src/utils/discord.tsx b/src/utils/discord.tsx index 08679db4..74e1aefe 100644 --- a/src/utils/discord.tsx +++ b/src/utils/discord.tsx @@ -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, Role, User } from "discord-types/general"; +import { Guild, Message, User } from "discord-types/general"; import { ImageModal, ModalRoot, ModalSize, openModal } from "./modal"; @@ -185,11 +185,3 @@ 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 { - if ("getRoles" in GuildStore) - return (GuildStore as any).getRoles(guildId); - - return GuildStore.getGuild(guildId)?.roles ?? {}; -} diff --git a/src/webpack/common/stores.ts b/src/webpack/common/stores.ts index 0c470d6a..f3a18d7b 100644 --- a/src/webpack/common/stores.ts +++ b/src/webpack/common/stores.ts @@ -46,7 +46,7 @@ export let ReadStateStore: GenericStore; export let PresenceStore: GenericStore; export let PoggerModeSettingsStore: GenericStore; -export let GuildStore: Stores.GuildStore & t.FluxStore; +export let GuildStore: t.GuildStore; export let UserStore: Stores.UserStore & t.FluxStore; export let UserProfileStore: GenericStore; export let SelectedChannelStore: Stores.SelectedChannelStore & t.FluxStore; diff --git a/src/webpack/common/types/stores.d.ts b/src/webpack/common/types/stores.d.ts index ecc87d74..8e89a6e2 100644 --- a/src/webpack/common/types/stores.d.ts +++ b/src/webpack/common/types/stores.d.ts @@ -17,7 +17,7 @@ */ import { DraftType } from "@webpack/common"; -import { Channel } from "discord-types/general"; +import { Channel, Guild, Role } from "discord-types/general"; import { FluxDispatcher, FluxEvents } from "./utils"; @@ -172,3 +172,13 @@ export class DraftStore extends FluxStore { getThreadDraftWithParentMessageId?(arg: any): any; getThreadSettings(channelId: string): any | null; } + +export class GuildStore extends FluxStore { + getGuild(guildId: string): Guild; + getGuildCount(): number; + getGuilds(): Record; + getGuildIds(): string[]; + getRole(guildId: string, roleId: string): Role; + getRoles(guildId: string): Record; + getAllGuildRoles(): Record>; +}