Equicord/src/equicordplugins/cleanChannelName/index.ts

82 lines
2.7 KiB
TypeScript
Raw Normal View History

2024-04-17 14:29:47 -04:00
/*
* Vencord, a Discord client mod
* Copyright (c) 2023 Vendicated and contributors
* SPDX-License-Identifier: GPL-3.0-or-later
*/
import { Devs } from "@utils/constants";
import definePlugin from "@utils/types";
2025-07-10 19:40:07 -04:00
import { Channel } from "@vencord/discord-types";
import { ChannelStore } from "@webpack/common";
const SMALL_CAPS: Record<string, string> = {
"ᴀ": "a", "ʙ": "b", "ᴄ": "c", "ᴅ": "d", "ᴇ": "e", "ꜰ": "f", "ɢ": "g", "ʜ": "h", "ɪ": "i", "ᴊ": "j",
"ᴋ": "k", "ʟ": "l", "ᴍ": "m", "ɴ": "n", "ᴏ": "o", "ᴘ": "p", "ǫ": "q", "ʀ": "r", "ꜱ": "s", "ᴛ": "t",
"ᴜ": "u", "ᴠ": "v", "ᴡ": "w", "x": "x", "ʏ": "y", "ᴢ": "z",
};
const ORIGINAL_NAME = Symbol("cleanChannelName.original");
let editingChannelId: string | null = null;
function computeClean(name: string, type: number): string {
const cleaned = name
.normalize("NFKC")
.replace(/[ᴀʙᴄᴅᴇꜰɢʜɪᴊᴋʟᴍɴᴏᴘǫʀꜱᴛᴜᴠᴡxʏᴢ]/g, m => SMALL_CAPS[m])
.replace(/[^ -~]?\p{Extended_Pictographic}[^ -~]?/ug, "")
.replace(/-?[^\p{Letter} -~]-?/ug, [2, 4].includes(type) ? " " : "-")
.replace(/(^-|-$)/g, "")
.replace(/-+/g, "-");
return cleaned || name;
}
2024-04-17 14:29:47 -04:00
export default definePlugin({
name: "CleanChannelName",
authors: [Devs.AutumnVN],
description: "Remove emoji and decoration from channel names. Reverts to the original while you're editing the channel.",
2026-04-16 13:45:58 -04:00
tags: ["Appearance", "Customisation", "Chat", "Emotes", "Servers"],
2024-04-17 14:29:47 -04:00
patches: [
{
find: "loadAllGuildAndPrivateChannelsFromDisk(){",
replacement: {
match: /(?<=getChannel\(\i\)\{if\(null!=\i\)return )\i\(\i\)/,
replace: "$self.cleanChannelName($&)",
},
},
2024-04-17 14:29:47 -04:00
],
flux: {
CHANNEL_SETTINGS_INIT({ channelId }: { channelId: string; }) {
editingChannelId = channelId;
(ChannelStore as any).emitChange?.();
},
CHANNEL_SETTINGS_CLOSE() {
editingChannelId = null;
(ChannelStore as any).emitChange?.();
},
},
2024-04-17 14:29:47 -04:00
cleanChannelName(channel?: Channel) {
if (channel == null) return channel;
const c = channel as any;
if (c[ORIGINAL_NAME] !== undefined) return channel;
c[ORIGINAL_NAME] = channel.name;
Object.defineProperty(channel, "name", {
configurable: true,
enumerable: true,
get() {
if (editingChannelId === channel.id) return c[ORIGINAL_NAME];
return computeClean(c[ORIGINAL_NAME], channel.type);
},
set(value: string) {
c[ORIGINAL_NAME] = value;
},
});
2024-04-17 14:29:47 -04:00
return channel;
},
2024-04-17 14:29:47 -04:00
});