Remove getGuildRoles

This commit is contained in:
Nuckyz 2024-03-12 20:18:44 -03:00
parent 6e363814d1
commit 6a7657de3f
No known key found for this signature in database
GPG key ID: 440BF8296E1C4AD9
10 changed files with 30 additions and 32 deletions

View file

@ -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) {

View file

@ -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 (
<ModalRoot
@ -203,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 = getGuildRoles(guild.id)[roleId];
const role = GuildStore.getRole(guild.id, roleId);
if (!role) return;
onClose();

View file

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

View file

@ -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<T extends { id: string; type: number; }>(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;

View file

@ -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 (
<span style={{

View file

@ -7,12 +7,12 @@
import "./styles.css";
import { classNameFactory } from "@api/Styles";
import { getGuildRoles, openImageModal, openUserProfile } from "@utils/discord";
import { openImageModal, openUserProfile } from "@utils/discord";
import { classes } from "@utils/misc";
import { ModalRoot, ModalSize, openModal } from "@utils/modal";
import { useAwaiter } from "@utils/react";
import { findByPropsLazy, findExportedComponentLazy } from "@webpack";
import { FluxDispatcher, Forms, GuildChannelStore, GuildMemberStore, IconUtils, Parser, PresenceStore, RelationshipStore, ScrollerThin, SnowflakeUtils, TabBar, Timestamp, useEffect, UserStore, UserUtils, useState, useStateFromStores } from "@webpack/common";
import { FluxDispatcher, Forms, GuildChannelStore, GuildMemberStore, GuildStore, IconUtils, Parser, PresenceStore, RelationshipStore, ScrollerThin, SnowflakeUtils, TabBar, Timestamp, useEffect, UserStore, UserUtils, useState, useStateFromStores } from "@webpack/common";
import { Guild, User } from "discord-types/general";
const IconClasses = findByPropsLazy("icon", "acronym", "childWrapper");
@ -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(getGuildRoles(guild.id)).length - 1, // - @everyone
"Roles": Object.keys(GuildStore.getRoles(guild.id)).length - 1, // - @everyone
};
return (

View file

@ -7,7 +7,6 @@
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";
@ -197,7 +196,7 @@ export default definePlugin({
if (message.mention_roles.length > 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}>`, `<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, 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<string, Role> {
if ("getRoles" in GuildStore)
return (GuildStore as any).getRoles(guildId);
return GuildStore.getGuild(guildId)?.roles ?? {};
}

View file

@ -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;

View file

@ -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<string, Guild>;
getGuildIds(): string[];
getRole(guildId: string, roleId: string): Role;
getRoles(guildId: string): Record<string, Role>;
getAllGuildRoles(): Record<string, Record<string, Role>>;
}