RoleColorEverywhere: Add coloring to message contents (#2893)

This commit is contained in:
Kyuuhachi 2024-09-22 09:11:07 +02:00 committed by GitHub
parent 22a5b18bfa
commit eaf62d8c1c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

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