From a91e528462d9d6c8b22265f5a1c56405f36e205b Mon Sep 17 00:00:00 2001 From: Slingexe Date: Tue, 21 Jan 2025 17:10:35 +0000 Subject: [PATCH] + Better formatting to stay clear from discord's acsi rendering limit per message --- backend/fetchNewMessages.js | 24 ++- commands/debug/debug.js | 16 +- .../formattertest-v1.js | 19 +- formatter-test-scripts/formattertest-v2.js | 165 ++++++++++++++++++ 4 files changed, 200 insertions(+), 24 deletions(-) rename formattertest.js => formatter-test-scripts/formattertest-v1.js (90%) create mode 100644 formatter-test-scripts/formattertest-v2.js diff --git a/backend/fetchNewMessages.js b/backend/fetchNewMessages.js index df9997b..6a38efd 100644 --- a/backend/fetchNewMessages.js +++ b/backend/fetchNewMessages.js @@ -70,17 +70,37 @@ const hackmudToDiscordColors = { function Formatter(message) { function convertHackmudColors(text) { + const originalMsg = text + let colorcount = 0 + let firstcolor = null + // Regex to detect backtick-wrapped strings with a leading color code const regex = /`([a-zA-Z0-9])([^`]*)`/g; - return text.replace(regex, (match, code, content) => { + + // The original regex run + let regexrun = text.replace(regex, (match, code, content) => { // Map the color code to the corresponding Discord color if it exists const discordColor = hackmudToDiscordColors[code]; + if (discordColor) { - return `${discordColor}${content}${hackmudToDiscordColors.reset}`; + if (firstcolor == null) {firstcolor = discordColor;} + colorcount++ + return `${discordColor}${content}${hackmudToDiscordColors.reset}` } // If no color is found, return the original match return match; }); + + // This is basically the regex above without any of the formatting stuff + let regexlimrun = text.replace(regex, (match, code, content) => { + return content + }) + + if (colorcount >= 15) { + return `${firstcolor}${regexlimrun}${hackmudToDiscordColors.reset}` + } else { + return regexrun + } } // Extract timestamp and format it diff --git a/commands/debug/debug.js b/commands/debug/debug.js index 5d3acd4..b541689 100644 --- a/commands/debug/debug.js +++ b/commands/debug/debug.js @@ -5,6 +5,10 @@ const path = require('path'); const { readFile } = require('fs/promises'); const dbgconfigPath = path.resolve(__dirname, '../../dbgconf.json'); +async function freply(cmd, val) { + await interaction.reply({content: `Set ${cmd} **${val ? 'Enabled' : 'Disabled'}**`, flags: MessageFlags.Ephemeral }); +} + module.exports = { category: 'utility', data: new SlashCommandBuilder() @@ -24,24 +28,28 @@ module.exports = { async execute(interaction) { const getsubcmd = interaction.options.getSubcommand(); - if (getsubcmd == "Log Everything") { + if (getsubcmd == "logeverything") { const val = interaction.options.getBoolean('val'); try { const config = JSON.parse(fs.readFileSync(dbgconfigPath, 'utf-8')); + if (!config) { + config = {} + } + if (val) { config.dbg = true } else { config.dbg = false } - fs.writeFileSync(configPath, JSON.stringify(config, null, 4), 'utf-8'); + fs.writeFileSync(dbgconfigPath, JSON.stringify(config, null, 4), 'utf-8'); - await interaction.reply({}); + freply(getsubcmd, val) } catch (error) { console.error(error); - await interaction.reply('An error occurred while managing the user settings. Check console for details.'); + await interaction.reply({content: 'An error occurred while managing the user settings. Check console for details.', flags: MessageFlags.Ephemeral }); } } diff --git a/formattertest.js b/formatter-test-scripts/formattertest-v1.js similarity index 90% rename from formattertest.js rename to formatter-test-scripts/formattertest-v1.js index 0d07e35..cbf96e9 100644 --- a/formattertest.js +++ b/formatter-test-scripts/formattertest-v1.js @@ -1,10 +1,3 @@ -const { readFile } = require('fs/promises'); -const path = require('node:path'); -const fs = require('node:fs'); - - -const logpath = path.resolve(__dirname, 'formattest-log.txt'); - // Full Hackmud-to-Discord color mapping //  = U+001B const hackmudToDiscordColors = { 'reset': '', // Reset text formatting @@ -140,16 +133,6 @@ const message3 = { "from_user": "com", "msg": "psst, this is a tell" }; -const messagespam = { - "id": "3598b8a024e394b691559a8c", - "t": 1515984655.629, - "from_user": "sans_comedy", - "msg": "`2T``3h``2i``3s``2 ``3i``2s``3 ``2a``3 ``2c``3o``2l``3o``2r``3f``2u``3l``2 ``3m``2e``3s``2s``3a``2g``3e``5!`", - "channel": "0000" -}; // Test the Formatter -console.log('Formatted Message:\n' + Formatter(message1) + '\n' + Formatter(message2) + '\n' + Formatter(message3)+ '\n' + Formatter(messagespam)); - -// Write to log -fs.writeFileSync(logpath, Formatter(messagespam), null, 4); \ No newline at end of file +console.log('Formatted Messages:\n' + Formatter(message1) + '\n' + Formatter(message2) + '\n' + Formatter(message3)); \ No newline at end of file diff --git a/formatter-test-scripts/formattertest-v2.js b/formatter-test-scripts/formattertest-v2.js new file mode 100644 index 0000000..126235b --- /dev/null +++ b/formatter-test-scripts/formattertest-v2.js @@ -0,0 +1,165 @@ +// Full Hackmud-to-Discord color mapping //  = U+001B +const hackmudToDiscordColors = { + 'reset': '', // Reset text formatting + '0': '', // Hackmud: #9B9B9B | Discord: Gray + '1': '', // Hackmud: #FFFFFF | Discord: White + '2': '', // Hackmud: #1EFF00 | Discord: Green + '3': '', // Hackmud: #0070DD | Discord: Blue + '4': '', // Hackmud: #B035EE | Discord: Pink + '5': '', // Hackmud: #FF8000 | Discord: Yellow + '6': '', // Hackmud: #FF8000 | Discord: Yellow + '7': '', // Hackmud: #FF8000 | Discord: Yellow + '8': '', // Hackmud: #FF8000 | Discord: Yellow + '9': '', // Hackmud: #FF8000 | Discord: Yellow + 'a': '', // Hackmud: #000000 | Discord: Gray + 'b': '', // Hackmud: #3F3F3F | Discord: Gray + 'c': '', // Hackmud: #676767 | Discord: Gray + 'd': '', // Hackmud: #7D0000 | Discord: Red + 'e': '', // Hackmud: #8E3434 | Discord: Red + 'f': '', // Hackmud: #A34F00 | Discord: Yellow + 'g': '', // Hackmud: #725437 | Discord: Yellow + 'h': '', // Hackmud: #A88600 | Discord: Yellow + 'i': '', // Hackmud: #B2934A | Discord: Yellow + 'j': '', // Hackmud: #939500 | Discord: Green + 'k': '', // Hackmud: #495225 | Discord: Green + 'l': '', // Hackmud: #299400 | Discord: Green + 'm': '', // Hackmud: #23381B | Discord: Gray + 'n': '', // Hackmud: #00535B | Discord: Cyan + 'o': '', // Hackmud: #324A4C | Discord: Cyan + 'p': '', // Hackmud: #0073A6 | Discord: Blue + 'q': '', // Hackmud: #385A6C | Discord: Blue + 'r': '', // Hackmud: #010067 | Discord: Blue + 's': '', // Hackmud: #507AA1 | Discord: Blue + 't': '', // Hackmud: #601C81 | Discord: Pink + 'u': '', // Hackmud: #43314C | Discord: Gray + 'v': '', // Hackmud: #8C0069 | Discord: Pink + 'w': '', // Hackmud: #973984 | Discord: Pink + 'x': '', // Hackmud: #880024 | Discord: Red + 'y': '', // Hackmud: #762E4A | Discord: Red + 'z': '', // Hackmud: #101215 | Discord: Gray + 'A': '', // Hackmud: #FFFFFF | Discord: White + 'B': '', // Hackmud: #CACACA | Discord: White + 'C': '', // Hackmud: #9B9B9B | Discord: Gray + 'D': '', // Hackmud: #FF0000 | Discord: Red + 'E': '', // Hackmud: #FF8383 | Discord: Red + 'F': '', // Hackmud: #FF8000 | Discord: Yellow + 'G': '', // Hackmud: #F3AA6F | Discord: Yellow + 'H': '', // Hackmud: #FBC803 | Discord: Yellow + 'I': '', // Hackmud: #FFD863 | Discord: Yellow + 'J': '', // Hackmud: #FFF404 | Discord: Yellow + 'K': '', // Hackmud: #F3F998 | Discord: Green + 'L': '', // Hackmud: #1EFF00 | Discord: Green + 'M': '', // Hackmud: #B3FF9B | Discord: Green + 'N': '', // Hackmud: #00FFFF | Discord: Cyan + 'O': '', // Hackmud: #8FE6FF | Discord: Cyan + 'P': '', // Hackmud: #0070DD | Discord: Blue + 'Q': '', // Hackmud: #A4E3FF | Discord: Blue + 'R': '', // Hackmud: #0000FF | Discord: Blue + 'S': '', // Hackmud: #7AB2F4 | Discord: Blue + 'T': '', // Hackmud: #B035EE | Discord: Pink + 'U': '', // Hackmud: #E6C4FF | Discord: Pink + 'V': '', // Hackmud: #FF00EC | Discord: Pink + 'W': '', // Hackmud: #FF96E0 | Discord: Pink + 'X': '', // Hackmud: #FF0070 | Discord: Red + 'Y': '', // Hackmud: #FF6A98 | Discord: Red + 'Z': '', // Hackmud: #0C112B | Discord: Gray +}; + +function Formatter(message) { + function convertHackmudColors(text) { + const originalMsg = text + let colorcount = 0 + let firstcolor = null + + // Regex to detect backtick-wrapped strings with a leading color code + const regex = /`([a-zA-Z0-9])([^`]*)`/g; + + // The original regex run + let regexrun = text.replace(regex, (match, code, content) => { + // Map the color code to the corresponding Discord color if it exists + const discordColor = hackmudToDiscordColors[code]; + + if (discordColor) { + if (firstcolor == null) {firstcolor = discordColor;} + colorcount++ + return `${discordColor}${content}${hackmudToDiscordColors.reset}` + } + // If no color is found, return the original match + return match; + }); + + // This is basically the regex above without any of the formatting stuff + let regexlimrun = text.replace(regex, (match, code, content) => { + return content + }) + + if (colorcount >= 15) { + return `${firstcolor}${regexlimrun}${hackmudToDiscordColors.reset}` + } else { + return regexrun + } + } + + // Extract timestamp and format it + const timestamp = new Date(message.t * 1000); + const hours = timestamp.getHours().toString().padStart(2, '0'); + const minutes = timestamp.getMinutes().toString().padStart(2, '0'); + const formattedTime = `${hours}${minutes}`; + + // Format user, channel, and other elements + const formattedUser = `${message.from_user}`; + const formattedChnlBlue = `${message.channel}`; + const formattedChnlPink = `${message.channel}`; + const formattedTell = `tell`; + const messageBord = `:::`; + + let formattedMessage = ""; + + // Apply color conversion to the message text + const convertedMessage = convertHackmudColors(message.msg); + + if (message.is_join) { + formattedMessage = `\`\`\`ansi\n${formattedTime} ${formattedChnlBlue} ${formattedUser} ${messageBord} ${convertedMessage} ${messageBord}\n\`\`\``; + } else { + if (!message.channel) { + formattedMessage = `\`\`\`ansi\n${formattedTime} ${formattedTell} ${formattedUser} ${messageBord} ${convertedMessage} ${messageBord}\n\`\`\``; + } else { + formattedMessage = `\`\`\`ansi\n${formattedTime} ${formattedChnlPink} ${formattedUser} ${messageBord} ${convertedMessage} ${messageBord}\n\`\`\``; + } + } + + return formattedMessage; +} + +// Example messages +const message1 = { + "id": "3598b8a024e394b691559a8c", + "t": 1515984655.629, + "from_user": "sans_comedy", + "msg": "`pThis is a ``Xlovely``p test message`", + "channel": "0000" +}; +const message2 = { + "id": "3598b8a024e394b691559a8c", + "t": 1515984653.345, + "from_user": "sans_comedy", + "msg": "user joined channel", + "is_join": true, + "channel": "0000" +}; +const message3 = { + "id": "8393b5e73a021ac084090aad", + "t": 1515984660.132, + "from_user": "com", + "msg": "psst, this is a tell" +}; +const messagespam = { + "id": "3598b8a024e394b691559a8c", + "t": 1515984655.629, + "from_user": "sans_comedy", + "msg": "`2T``3h``2i``3s``2 ``3i``2s``3 ``2a``3 ``2c``3o``2l``3o``2r``3f``2u``3l``2 ``3m``2e``3s``2s``3a``2g``3e``5!`", + "channel": "0000" +}; + +// Test the Formatter +console.log('Formatted Messages:\n' + Formatter(message1) + '\n' + Formatter(message2) + '\n' + Formatter(message3) + '\n' + Formatter(messagespam)); \ No newline at end of file