diff --git a/src/plugins/ircColors/index.ts b/src/plugins/ircColors/index.ts index d5cc5f3e4..208b327e9 100644 --- a/src/plugins/ircColors/index.ts +++ b/src/plugins/ircColors/index.ts @@ -17,33 +17,22 @@ */ import { definePluginSettings } from "@api/Settings"; +import { hash as h64 } from "@intrnl/xxhash64"; import { Devs } from "@utils/constants"; import definePlugin, { OptionType } from "@utils/types"; - -// Compute a 64-bit FNV-1a hash of the passed data -function hash(id: bigint) { - const fnvPrime = 1099511628211n; - const offsetBasis = 14695981039346656037n; - - let result = offsetBasis; - for (let i = 7n; i >= 0n; i--) { - result ^= (id >> (8n * i)) & 0xffn; - result = (result * fnvPrime) % 2n ** 32n; - } - - return result; -} +import { useMemo } from "@webpack/common"; // Calculate a CSS color string based on the user ID -function calculateNameColorForUser(id: bigint) { - const idHash = hash(id); +function calculateNameColorForUser(id: string) { + const { lightness } = settings.use(["lightness"]); + const idHash = useMemo(() => h64(id), [id]); - return `hsl(${idHash % 360n}, 100%, ${settings.store.lightness}%)`; + return `hsl(${idHash % 360n}, 100%, ${lightness}%)`; } const settings = definePluginSettings({ lightness: { - description: "Lightness, in %. Change if the colors are too light or too dark. Reopen the chat to apply.", + description: "Lightness, in %. Change if the colors are too light or too dark", type: OptionType.NUMBER, default: 70, }, @@ -51,44 +40,46 @@ const settings = definePluginSettings({ description: "Replace role colors in the member list", restartNeeded: true, type: OptionType.BOOLEAN, - default: true, - }, + default: true + } }); export default definePlugin({ name: "IrcColors", description: "Makes username colors in chat unique, like in IRC clients", authors: [Devs.Grzesiek11], + settings, + patches: [ { find: '="SYSTEM_TAG"', replacement: { match: /(?<=className:\i\.username,style:.{0,50}:void 0,)/, - replace: "style:{color:$self.calculateNameColorForMessageContext(arguments[0])},", - }, + replace: "style:{color:$self.calculateNameColorForMessageContext(arguments[0])}," + } }, { - find: ".NameWithRole,{roleName:", + find: "#{intl::GUILD_OWNER}),children:", replacement: { - match: /(?<=color:)null!=.{0,50}?(?=,)/, - replace: "$self.calculateNameColorForListContext(arguments[0])", + match: /(?<=\.MEMBER_LIST}\),\[\]\),)(.+?color:)null!=.{0,50}?(?=,)/, + replace: (_, rest) => `ircColor=$self.calculateNameColorForListContext(arguments[0]),${rest}ircColor` }, - predicate: () => settings.store.memberListColors, - }, + predicate: () => settings.store.memberListColors + } ], - settings, + calculateNameColorForMessageContext(context: any) { const id = context?.message?.author?.id; if (id == null) { return null; } - return calculateNameColorForUser(BigInt(id)); + return calculateNameColorForUser(id); }, calculateNameColorForListContext(context: any) { const id = context?.user?.id; if (id == null) { return null; } - return calculateNameColorForUser(BigInt(id)); - }, + return calculateNameColorForUser(id); + } });