Skip to content

Commit ea27724

Browse files
Nuckyzjamesbt365
andcommitted
RoleColorEverywhere: Poll Results & Cleanup
Co-authored-by: jamesbt365 <jamesbt365@gmail.com>
1 parent 99458da commit ea27724

2 files changed

Lines changed: 90 additions & 47 deletions

File tree

src/plugins/roleColorEverywhere/index.tsx

Lines changed: 86 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { definePluginSettings } from "@api/Settings";
2020
import ErrorBoundary from "@components/ErrorBoundary";
2121
import { makeRange } from "@components/PluginSettings/components";
2222
import { Devs } from "@utils/constants";
23+
import { Logger } from "@utils/Logger";
2324
import definePlugin, { OptionType } from "@utils/types";
2425
import { findByCodeLazy } from "@webpack";
2526
import { ChannelStore, GuildMemberStore, GuildStore } from "@webpack/common";
@@ -51,6 +52,12 @@ const settings = definePluginSettings({
5152
description: "Show role colors in the reactors list",
5253
restartNeeded: true
5354
},
55+
pollResults: {
56+
type: OptionType.BOOLEAN,
57+
default: true,
58+
description: "Show role colors in the poll results",
59+
restartNeeded: true
60+
},
5461
colorChatMessages: {
5562
type: OptionType.BOOLEAN,
5663
default: false,
@@ -62,127 +69,159 @@ const settings = definePluginSettings({
6269
description: "Intensity of message coloring.",
6370
markers: makeRange(0, 100, 10),
6471
default: 30
65-
},
72+
}
6673
});
6774

68-
6975
export default definePlugin({
7076
name: "RoleColorEverywhere",
71-
authors: [Devs.KingFish, Devs.lewisakura, Devs.AutumnVN, Devs.Kyuuhachi],
77+
authors: [Devs.KingFish, Devs.lewisakura, Devs.AutumnVN, Devs.Kyuuhachi, Devs.jamesbt365],
7278
description: "Adds the top role color anywhere possible",
79+
settings,
80+
7381
patches: [
7482
// Chat Mentions
7583
{
7684
find: ".USER_MENTION)",
7785
replacement: [
7886
{
7987
match: /onContextMenu:\i,color:\i,\.\.\.\i(?=,children:)(?<=user:(\i),channel:(\i).{0,500}?)/,
80-
replace: "$&,color:$self.getUserColor($1?.id,{channelId:$2?.id})"
88+
replace: "$&,color:$self.getColorInt($1?.id,$2?.id)"
8189
}
8290
],
83-
predicate: () => settings.store.chatMentions,
91+
predicate: () => settings.store.chatMentions
8492
},
8593
// Slate
8694
{
8795
find: ".userTooltip,children",
8896
replacement: [
8997
{
90-
match: /let\{id:(\i),guildId:(\i)[^}]*\}.*?\.\i,{(?=children)/,
91-
replace: "$&color:$self.getUserColor($1,{guildId:$2}),"
98+
match: /let\{id:(\i),guildId:\i,channelId:(\i)[^}]*\}.*?\.\i,{(?=children)/,
99+
replace: "$&color:$self.getColorInt($1,$2),"
92100
}
93101
],
94-
predicate: () => settings.store.chatMentions,
102+
predicate: () => settings.store.chatMentions
95103
},
104+
// Member List Role Headers
96105
{
97106
find: 'tutorialId:"whos-online',
98107
replacement: [
99108
{
100109
match: /null,\i," ",\i\]/,
101-
replace: "null,$self.roleGroupColor(arguments[0])]"
110+
replace: "null,$self.RoleGroupColor(arguments[0])]"
102111
},
103112
],
104-
predicate: () => settings.store.memberList,
113+
predicate: () => settings.store.memberList
105114
},
106115
{
107116
find: "#{intl::THREAD_BROWSER_PRIVATE}",
108117
replacement: [
109118
{
110119
match: /children:\[\i," ",\i\]/,
111-
replace: "children:[$self.roleGroupColor(arguments[0])]"
120+
replace: "children:[$self.RoleGroupColor(arguments[0])]"
112121
},
113122
],
114-
predicate: () => settings.store.memberList,
123+
predicate: () => settings.store.memberList
115124
},
125+
// Voice Users
116126
{
117-
find: "renderPrioritySpeaker",
127+
find: "renderPrioritySpeaker(){",
118128
replacement: [
119129
{
120130
match: /renderName\(\){.+?usernameSpeaking\]:.+?(?=children)/,
121-
replace: "$&...$self.getVoiceProps(this.props),"
131+
replace: "$&style:$self.getColorStyle(this?.props?.user?.id,this?.props?.guildId),"
122132
}
123133
],
124-
predicate: () => settings.store.voiceUsers,
134+
predicate: () => settings.store.voiceUsers
125135
},
136+
// Reaction List
126137
{
127138
find: ".reactorDefault",
128139
replacement: {
129-
match: /,onContextMenu:e=>.{0,15}\((\i),(\i),(\i)\).{0,250}tag:"strong"/,
130-
replace: "$&,style:{color:$self.getColor($2?.id,$1)}"
140+
match: /,onContextMenu:\i=>.{0,15}\((\i),(\i),(\i)\).{0,250}tag:"strong"/,
141+
replace: "$&,style:$self.getColorStyle($2?.id,$1?.channel?.id)"
131142
},
132143
predicate: () => settings.store.reactorsList,
133144
},
145+
// Poll Results
146+
{
147+
find: ",reactionVoteCounts",
148+
replacement: {
149+
match: /\.nickname,(?=children:)/,
150+
replace: "$&style:$self.getColorStyle(arguments[0]?.user?.id,arguments[0]?.channel?.id),"
151+
},
152+
predicate: () => settings.store.pollResults
153+
},
154+
// Messages
134155
{
135156
find: "#{intl::MESSAGE_EDITED}",
136157
replacement: {
137158
match: /(?<=isUnsupported\]:(\i)\.isUnsupported\}\),)(?=children:\[)/,
138-
replace: "style:{color:$self.useMessageColor($1)},"
159+
replace: "style:$self.useMessageColorStyle($1),"
139160
},
140-
predicate: () => settings.store.colorChatMessages,
141-
},
161+
predicate: () => settings.store.colorChatMessages
162+
}
142163
],
143-
settings,
144164

145-
getColor(userId: string, { channelId, guildId }: { channelId?: string; guildId?: string; }) {
146-
if (!(guildId ??= ChannelStore.getChannel(channelId!)?.guild_id)) return null;
147-
return GuildMemberStore.getMember(guildId, userId)?.colorString ?? null;
165+
getColorString(userId: string, channelOrGuildId: string) {
166+
try {
167+
const guildId = ChannelStore.getChannel(channelOrGuildId)?.guild_id ?? GuildStore.getGuild(channelOrGuildId)?.id;
168+
if (guildId == null) return null;
169+
170+
return GuildMemberStore.getMember(guildId, userId)?.colorString ?? null;
171+
} catch (e) {
172+
new Logger("RoleColorEverywhere").error("Failed to get color string", e);
173+
}
174+
175+
return null;
148176
},
149177

150-
getUserColor(userId: string, ids: { channelId?: string; guildId?: string; }) {
151-
const colorString = this.getColor(userId, ids);
178+
getColorInt(userId: string, channelOrGuildId: string) {
179+
const colorString = this.getColorString(userId, channelOrGuildId);
152180
return colorString && parseInt(colorString.slice(1), 16);
153181
},
154182

155-
roleGroupColor: ErrorBoundary.wrap(({ id, count, title, guildId, label }: { id: string; count: number; title: string; guildId: string; label: string; }) => {
156-
const role = GuildStore.getRole(guildId, id);
183+
getColorStyle(userId: string, channelOrGuildId: string) {
184+
const colorString = this.getColorString(userId, channelOrGuildId);
157185

158-
return (
159-
<span style={{
160-
color: role?.colorString,
161-
fontWeight: "unset",
162-
letterSpacing: ".05em"
163-
}}>
164-
{title ?? label} &mdash; {count}
165-
</span>
166-
);
167-
}, { noop: true }),
168-
169-
getVoiceProps({ user: { id: userId }, guildId }: { user: { id: string; }; guildId: string; }) {
170-
return {
171-
style: {
172-
color: this.getColor(userId, { guildId })
173-
}
186+
return colorString && {
187+
color: colorString
174188
};
175189
},
176190

177191
useMessageColor(message: any) {
178192
try {
179193
const { messageSaturation } = settings.use(["messageSaturation"]);
180194
const author = useMessageAuthor(message);
181-
if (author.colorString !== undefined && messageSaturation !== 0)
195+
196+
if (author.colorString != null && messageSaturation !== 0) {
182197
return `color-mix(in oklab, ${author.colorString} ${messageSaturation}%, var(--text-normal))`;
198+
}
183199
} catch (e) {
184-
console.error("[RCE] failed to get message color", e);
200+
new Logger("RoleColorEverywhere").error("Failed to get message color", e);
185201
}
186-
return undefined;
202+
203+
return null;
187204
},
205+
206+
useMessageColorStyle(message: any) {
207+
const color = this.useMessageColor(message);
208+
209+
return color && {
210+
color
211+
};
212+
},
213+
214+
RoleGroupColor: ErrorBoundary.wrap(({ id, count, title, guildId, label }: { id: string; count: number; title: string; guildId: string; label: string; }) => {
215+
const role = GuildStore.getRole(guildId, id);
216+
217+
return role != null && (
218+
<span style={{
219+
color: role.colorString,
220+
fontWeight: "unset",
221+
letterSpacing: ".05em"
222+
}}>
223+
{title ?? label} &mdash; {count}
224+
</span>
225+
);
226+
}, { noop: true })
188227
});

src/utils/constants.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,10 @@ export const Devs = /* #__PURE__*/ Object.freeze({
579579
name: "SomeAspy",
580580
id: 516750892372852754n,
581581
},
582+
jamesbt365: {
583+
name: "jamesbt365",
584+
id: 158567567487795200n,
585+
},
582586
} satisfies Record<string, Dev>);
583587

584588
// iife so #__PURE__ works correctly

0 commit comments

Comments
 (0)