From eaf62d8c1cd8ea7997e6efde04317e5b4700afbe Mon Sep 17 00:00:00 2001 From: Kyuuhachi <1547062+Kyuuhachi@users.noreply.github.com> Date: Sun, 22 Sep 2024 09:11:07 +0200 Subject: [PATCH] RoleColorEverywhere: Add coloring to message contents (#2893) --- src/plugins/roleColorEverywhere/index.tsx | 40 ++++++++++++++++++++--- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/src/plugins/roleColorEverywhere/index.tsx b/src/plugins/roleColorEverywhere/index.tsx index b5f66c098..e3b22fd6a 100644 --- a/src/plugins/roleColorEverywhere/index.tsx +++ b/src/plugins/roleColorEverywhere/index.tsx @@ -18,10 +18,14 @@ import { definePluginSettings } from "@api/Settings"; import ErrorBoundary from "@components/ErrorBoundary"; +import { makeRange } from "@components/PluginSettings/components"; import { Devs } from "@utils/constants"; import definePlugin, { OptionType } from "@utils/types"; +import { findByCodeLazy } from "@webpack"; import { ChannelStore, GuildMemberStore, GuildStore } from "@webpack/common"; +const useMessageAuthor = findByCodeLazy('"Result cannot be null because the message is not null"'); + const settings = definePluginSettings({ chatMentions: { type: OptionType.BOOLEAN, @@ -46,13 +50,21 @@ const settings = definePluginSettings({ default: true, description: "Show role colors in the reactors list", restartNeeded: true - } + }, + messageSaturation: { + type: OptionType.SLIDER, + description: "Intensity of message coloring. 0 to disable.", + markers: makeRange(0, 100, 10), + default: 30, + // This is called only once at startup, but late enough that the store is initialized. + get restartNeeded() { return settings.store.messageSaturation === 0; } + }, }); export default definePlugin({ name: "RoleColorEverywhere", - authors: [Devs.KingFish, Devs.lewisakura, Devs.AutumnVN], + authors: [Devs.KingFish, Devs.lewisakura, Devs.AutumnVN, Devs.Kyuuhachi], description: "Adds the top role color anywhere possible", patches: [ // Chat Mentions @@ -114,7 +126,15 @@ export default definePlugin({ replace: "$&,style:{color:$self.getColor($2?.id,$1)}" }, predicate: () => settings.store.reactorsList, - } + }, + { + find: '.Messages.MESSAGE_EDITED,")"', + replacement: { + match: /(?<=isUnsupported\]:(\i)\.isUnsupported\}\),)(?=children:\[)/, + replace: "style:{color:$self.useMessageColor($1)}," + }, + predicate: () => settings.store.messageSaturation !== 0, + }, ], settings, @@ -148,5 +168,17 @@ export default definePlugin({ color: this.getColor(userId, { guildId }) } }; - } + }, + + useMessageColor(message: any) { + try { + const { messageSaturation } = settings.use(["messageSaturation"]); + const author = useMessageAuthor(message); + if (author.colorString !== undefined && messageSaturation !== 0) + return `color-mix(in oklab, ${author.colorString} ${messageSaturation}%, var(--text-normal))`; + } catch(e) { + console.error("[RCE] failed to get message color", e); + } + return undefined; + }, });