To create a complementary plugin that translates "Meowspeak" (as implemented by your Meowspeak plugin) back to regular text for messages sent by other users, you can develop a new plugin called MeowspeakDecoder. This plugin will listen to incoming messages, detect if they're written in Meowspeak, decode them back to their original form, and display the translated text alongside the original message in the Discord client.
Here's how you can implement this:
1. Understanding the Meowspeak Encoding
Your Meowspeak plugin replaces each alphabet character with a specific cat-related string. To decode the messages, you'll need to reverse this process:
- Encoding Mapping (
charMap): Maps each character a-z to its Meowspeak equivalent.
- Decoding Mapping (
reverseCharMap): The inverse of charMap, mapping each Meowspeak string back to the original character.
2. Implementing the MeowspeakDecoder Plugin
Below is the implementation of the MeowspeakDecoder plugin. This plugin listens to incoming messages, decodes them if they are in Meowspeak, and displays the decoded text beneath the original message.
/*
* Vencord, a Discord client mod
* Copyright (c) 2024 Vendicated and contributors
* SPDX-License-Identifier: GPL-3.0-or-later
*/
import {
addMessageListener,
MessageObject
} from "@api/MessageEvents";
import { Devs } from "@utils/constants";
import definePlugin from "@utils/types";
// Original charMap from Meowspeak plugin
const charMap: Record<string, string> = {
a: "meow\u200B",
b: "meow\u200Cnya",
c: "purr\u200D",
d: "mrow\u200B",
e: "nyan\u200C",
f: "hiss\u200D",
g: "mew\u200B",
h: "nya\u200C",
i: "miao\u200D",
j: "nyaa\u200B",
k: "mrrreow\u200Cmeow",
l: "purrr\u200D",
m: "miaow\u200B",
n: "nyan\u200Cnyan",
o: "meoww\u200D",
p: "paw\u200B",
q: "mrrp\u200C",
r: "meowers\u200D",
s: "sss\u200B",
t: "pssss\u200C",
u: "uwu\u200D",
v: "nyaaa\u200B",
w: "whisker\u200C",
x: "mrrrow\u200D",
y: "meowowo\u200B",
z: "zzz\u200C"
};
// Create reverse mapping for decoding
const reverseCharMap: Record<string, string> = {};
for (const [key, value] of Object.entries(charMap)) {
reverseCharMap[value] = key;
}
// Sort the keys by length in descending order to handle overlapping strings
const sortedReverseKeys = Object.keys(reverseCharMap).sort((a, b) => b.length - a.length);
/**
* Decodes a Meowspeak string back to normal text.
* @param text The Meowspeak encoded text.
* @returns The decoded normal text.
*/
function decodeMeowspeak(text: string): string {
let decoded = text;
for (const key of sortedReverseKeys) {
const regex = new RegExp(key, 'gi');
decoded = decoded.replace(regex, reverseCharMap[key]);
}
return decoded;
}
export default definePlugin({
name: "MeowspeakDecoder",
description: "Decodes messages written in Meowspeak back to normal text.",
authors: [Devs.Ven],
dependencies: ["MessageEventsAPI"],
onMessage(msg: MessageObject) {
// Attempt to decode the message content
const decodedContent = decodeMeowspeak(msg.content);
// If decoding results in a meaningful change, display the decoded text
if (decodedContent !== msg.content) {
// Create a DOM element to display the decoded message
// This example appends the decoded text below the original message
// You may need to adjust the selectors based on the Discord client's DOM structure
// Find the message element in the DOM
const messageElement = document.querySelector(`[data-message-id="${msg.id}"]`);
if (messageElement) {
// Check if the decoded message is already displayed
if (!messageElement.querySelector('.decoded-meowspeak')) {
// Create a new element for the decoded text
const decodedElement = document.createElement('div');
decodedElement.className = 'decoded-meowspeak';
decodedElement.style.color = 'gray';
decodedElement.style.fontStyle = 'italic';
decodedElement.innerText = `Decoded: ${decodedContent}`;
// Append the decoded text below the original message
messageElement.appendChild(decodedElement);
}
}
}
},
start() {
this.messageListener = addMessageListener((msg) => this.onMessage(msg));
},
stop() {
if (this.messageListener) {
this.messageListener();
this.messageListener = null;
}
},
});
3. Explanation of the Decoder Plugin
-
Reverse Mapping Creation: The reverseCharMap is created by inversing the original charMap. This allows the plugin to map Meowspeak strings back to their corresponding characters.
-
Sorting Reverse Keys: To handle overlapping or nested Meowspeak strings, the reverseCharMap keys are sorted by length in descending order. This ensures that longer strings are matched first during decoding.
-
Decoding Function: The decodeMeowspeak function iterates over the sorted keys and replaces each occurrence of the Meowspeak string with the corresponding character. Regular expressions with the gi flags are used to ensure case-insensitive and global replacements.
-
Message Listener: The onMessage function listens to incoming messages. If a message's content changes after decoding (indicating it was in Meowspeak), it searches for the corresponding message element in the DOM and appends the decoded text beneath it.
-
DOM Manipulation: The plugin creates a new div element with the class decoded-meowspeak to display the decoded message. Styling is applied to differentiate it from the original message.
4. Integration Considerations
-
Selectors: The selector [data-message-id="${msg.id}"] is used to locate the specific message in the DOM. Depending on the Discord client's actual DOM structure, you might need to adjust this selector to accurately target message elements.
-
Styling: The decoded message is styled with gray color and italic font to distinguish it from regular messages. You can customize the styling as per your preference.
-
Performance: Ensure that the decoding process is efficient, especially in servers with high message traffic, to prevent performance issues.
-
Edge Cases: Consider handling edge cases where the Meowspeak encoding might not perfectly translate back, such as messages containing mixed regular and Meowspeak text.
5. Enhancements
-
Toggle Visibility: Add a toggle option to show or hide decoded messages based on user preference.
-
Notification System: Instead of displaying the decoded text beneath each message, you could implement a hover tooltip or a separate panel that shows the decoded version when requested.
-
Bulk Decoding: For servers where many messages are in Meowspeak, provide an option to automatically decode all incoming messages without the need to individually process each one.
6. Final Notes
Ensure that both the Meowspeak and MeowspeakDecoder plugins are active simultaneously for the encoding and decoding processes to work seamlessly. Testing in a controlled environment before deploying to a live server is recommended to fine-tune the functionality and handle any unforeseen issues.
By implementing the MeowspeakDecoder plugin as described, you'll provide users with the ability to effortlessly decode and understand messages written in Meowspeak, enhancing the overall user experience within your Discord client mod.