Skip to content

Commit a91e528

Browse files
author
Slingexe
authored
+ Better formatting to stay clear from discord's acsi rendering limit per message
1 parent 7b09f62 commit a91e528

4 files changed

Lines changed: 200 additions & 24 deletions

File tree

backend/fetchNewMessages.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,37 @@ const hackmudToDiscordColors = {
7070

7171
function Formatter(message) {
7272
function convertHackmudColors(text) {
73+
const originalMsg = text
74+
let colorcount = 0
75+
let firstcolor = null
76+
7377
// Regex to detect backtick-wrapped strings with a leading color code
7478
const regex = /`([a-zA-Z0-9])([^`]*)`/g;
75-
return text.replace(regex, (match, code, content) => {
79+
80+
// The original regex run
81+
let regexrun = text.replace(regex, (match, code, content) => {
7682
// Map the color code to the corresponding Discord color if it exists
7783
const discordColor = hackmudToDiscordColors[code];
84+
7885
if (discordColor) {
79-
return `${discordColor}${content}${hackmudToDiscordColors.reset}`;
86+
if (firstcolor == null) {firstcolor = discordColor;}
87+
colorcount++
88+
return `${discordColor}${content}${hackmudToDiscordColors.reset}`
8089
}
8190
// If no color is found, return the original match
8291
return match;
8392
});
93+
94+
// This is basically the regex above without any of the formatting stuff
95+
let regexlimrun = text.replace(regex, (match, code, content) => {
96+
return content
97+
})
98+
99+
if (colorcount >= 15) {
100+
return `${firstcolor}${regexlimrun}${hackmudToDiscordColors.reset}`
101+
} else {
102+
return regexrun
103+
}
84104
}
85105

86106
// Extract timestamp and format it

commands/debug/debug.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ const path = require('path');
55
const { readFile } = require('fs/promises');
66
const dbgconfigPath = path.resolve(__dirname, '../../dbgconf.json');
77

8+
async function freply(cmd, val) {
9+
await interaction.reply({content: `Set ${cmd} **${val ? 'Enabled' : 'Disabled'}**`, flags: MessageFlags.Ephemeral });
10+
}
11+
812
module.exports = {
913
category: 'utility',
1014
data: new SlashCommandBuilder()
@@ -24,24 +28,28 @@ module.exports = {
2428
async execute(interaction) {
2529
const getsubcmd = interaction.options.getSubcommand();
2630

27-
if (getsubcmd == "Log Everything") {
31+
if (getsubcmd == "logeverything") {
2832
const val = interaction.options.getBoolean('val');
2933

3034
try {
3135
const config = JSON.parse(fs.readFileSync(dbgconfigPath, 'utf-8'));
3236

37+
if (!config) {
38+
config = {}
39+
}
40+
3341
if (val) {
3442
config.dbg = true
3543
} else {
3644
config.dbg = false
3745
}
3846

39-
fs.writeFileSync(configPath, JSON.stringify(config, null, 4), 'utf-8');
47+
fs.writeFileSync(dbgconfigPath, JSON.stringify(config, null, 4), 'utf-8');
4048

41-
await interaction.reply({});
49+
freply(getsubcmd, val)
4250
} catch (error) {
4351
console.error(error);
44-
await interaction.reply('An error occurred while managing the user settings. Check console for details.');
52+
await interaction.reply({content: 'An error occurred while managing the user settings. Check console for details.', flags: MessageFlags.Ephemeral });
4553
}
4654
}
4755

formattertest.js renamed to formatter-test-scripts/formattertest-v1.js

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
const { readFile } = require('fs/promises');
2-
const path = require('node:path');
3-
const fs = require('node:fs');
4-
5-
6-
const logpath = path.resolve(__dirname, 'formattest-log.txt');
7-
81
// Full Hackmud-to-Discord color mapping //  = U+001B
92
const hackmudToDiscordColors = {
103
'reset': '', // Reset text formatting
@@ -140,16 +133,6 @@ const message3 = {
140133
"from_user": "com",
141134
"msg": "psst, this is a tell"
142135
};
143-
const messagespam = {
144-
"id": "3598b8a024e394b691559a8c",
145-
"t": 1515984655.629,
146-
"from_user": "sans_comedy",
147-
"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!`",
148-
"channel": "0000"
149-
};
150136

151137
// Test the Formatter
152-
console.log('Formatted Message:\n' + Formatter(message1) + '\n' + Formatter(message2) + '\n' + Formatter(message3)+ '\n' + Formatter(messagespam));
153-
154-
// Write to log
155-
fs.writeFileSync(logpath, Formatter(messagespam), null, 4);
138+
console.log('Formatted Messages:\n' + Formatter(message1) + '\n' + Formatter(message2) + '\n' + Formatter(message3));
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
// Full Hackmud-to-Discord color mapping //  = U+001B
2+
const hackmudToDiscordColors = {
3+
'reset': '', // Reset text formatting
4+
'0': '', // Hackmud: #9B9B9B | Discord: Gray
5+
'1': '', // Hackmud: #FFFFFF | Discord: White
6+
'2': '', // Hackmud: #1EFF00 | Discord: Green
7+
'3': '', // Hackmud: #0070DD | Discord: Blue
8+
'4': '', // Hackmud: #B035EE | Discord: Pink
9+
'5': '', // Hackmud: #FF8000 | Discord: Yellow
10+
'6': '', // Hackmud: #FF8000 | Discord: Yellow
11+
'7': '', // Hackmud: #FF8000 | Discord: Yellow
12+
'8': '', // Hackmud: #FF8000 | Discord: Yellow
13+
'9': '', // Hackmud: #FF8000 | Discord: Yellow
14+
'a': '', // Hackmud: #000000 | Discord: Gray
15+
'b': '', // Hackmud: #3F3F3F | Discord: Gray
16+
'c': '', // Hackmud: #676767 | Discord: Gray
17+
'd': '', // Hackmud: #7D0000 | Discord: Red
18+
'e': '', // Hackmud: #8E3434 | Discord: Red
19+
'f': '', // Hackmud: #A34F00 | Discord: Yellow
20+
'g': '', // Hackmud: #725437 | Discord: Yellow
21+
'h': '', // Hackmud: #A88600 | Discord: Yellow
22+
'i': '', // Hackmud: #B2934A | Discord: Yellow
23+
'j': '', // Hackmud: #939500 | Discord: Green
24+
'k': '', // Hackmud: #495225 | Discord: Green
25+
'l': '', // Hackmud: #299400 | Discord: Green
26+
'm': '', // Hackmud: #23381B | Discord: Gray
27+
'n': '', // Hackmud: #00535B | Discord: Cyan
28+
'o': '', // Hackmud: #324A4C | Discord: Cyan
29+
'p': '', // Hackmud: #0073A6 | Discord: Blue
30+
'q': '', // Hackmud: #385A6C | Discord: Blue
31+
'r': '', // Hackmud: #010067 | Discord: Blue
32+
's': '', // Hackmud: #507AA1 | Discord: Blue
33+
't': '', // Hackmud: #601C81 | Discord: Pink
34+
'u': '', // Hackmud: #43314C | Discord: Gray
35+
'v': '', // Hackmud: #8C0069 | Discord: Pink
36+
'w': '', // Hackmud: #973984 | Discord: Pink
37+
'x': '', // Hackmud: #880024 | Discord: Red
38+
'y': '', // Hackmud: #762E4A | Discord: Red
39+
'z': '', // Hackmud: #101215 | Discord: Gray
40+
'A': '', // Hackmud: #FFFFFF | Discord: White
41+
'B': '', // Hackmud: #CACACA | Discord: White
42+
'C': '', // Hackmud: #9B9B9B | Discord: Gray
43+
'D': '', // Hackmud: #FF0000 | Discord: Red
44+
'E': '', // Hackmud: #FF8383 | Discord: Red
45+
'F': '', // Hackmud: #FF8000 | Discord: Yellow
46+
'G': '', // Hackmud: #F3AA6F | Discord: Yellow
47+
'H': '', // Hackmud: #FBC803 | Discord: Yellow
48+
'I': '', // Hackmud: #FFD863 | Discord: Yellow
49+
'J': '', // Hackmud: #FFF404 | Discord: Yellow
50+
'K': '', // Hackmud: #F3F998 | Discord: Green
51+
'L': '', // Hackmud: #1EFF00 | Discord: Green
52+
'M': '', // Hackmud: #B3FF9B | Discord: Green
53+
'N': '', // Hackmud: #00FFFF | Discord: Cyan
54+
'O': '', // Hackmud: #8FE6FF | Discord: Cyan
55+
'P': '', // Hackmud: #0070DD | Discord: Blue
56+
'Q': '', // Hackmud: #A4E3FF | Discord: Blue
57+
'R': '', // Hackmud: #0000FF | Discord: Blue
58+
'S': '', // Hackmud: #7AB2F4 | Discord: Blue
59+
'T': '', // Hackmud: #B035EE | Discord: Pink
60+
'U': '', // Hackmud: #E6C4FF | Discord: Pink
61+
'V': '', // Hackmud: #FF00EC | Discord: Pink
62+
'W': '', // Hackmud: #FF96E0 | Discord: Pink
63+
'X': '', // Hackmud: #FF0070 | Discord: Red
64+
'Y': '', // Hackmud: #FF6A98 | Discord: Red
65+
'Z': '', // Hackmud: #0C112B | Discord: Gray
66+
};
67+
68+
function Formatter(message) {
69+
function convertHackmudColors(text) {
70+
const originalMsg = text
71+
let colorcount = 0
72+
let firstcolor = null
73+
74+
// Regex to detect backtick-wrapped strings with a leading color code
75+
const regex = /`([a-zA-Z0-9])([^`]*)`/g;
76+
77+
// The original regex run
78+
let regexrun = text.replace(regex, (match, code, content) => {
79+
// Map the color code to the corresponding Discord color if it exists
80+
const discordColor = hackmudToDiscordColors[code];
81+
82+
if (discordColor) {
83+
if (firstcolor == null) {firstcolor = discordColor;}
84+
colorcount++
85+
return `${discordColor}${content}${hackmudToDiscordColors.reset}`
86+
}
87+
// If no color is found, return the original match
88+
return match;
89+
});
90+
91+
// This is basically the regex above without any of the formatting stuff
92+
let regexlimrun = text.replace(regex, (match, code, content) => {
93+
return content
94+
})
95+
96+
if (colorcount >= 15) {
97+
return `${firstcolor}${regexlimrun}${hackmudToDiscordColors.reset}`
98+
} else {
99+
return regexrun
100+
}
101+
}
102+
103+
// Extract timestamp and format it
104+
const timestamp = new Date(message.t * 1000);
105+
const hours = timestamp.getHours().toString().padStart(2, '0');
106+
const minutes = timestamp.getMinutes().toString().padStart(2, '0');
107+
const formattedTime = `${hours}${minutes}`;
108+
109+
// Format user, channel, and other elements
110+
const formattedUser = `${message.from_user}`;
111+
const formattedChnlBlue = `${message.channel}`;
112+
const formattedChnlPink = `${message.channel}`;
113+
const formattedTell = `tell`;
114+
const messageBord = `:::`;
115+
116+
let formattedMessage = "";
117+
118+
// Apply color conversion to the message text
119+
const convertedMessage = convertHackmudColors(message.msg);
120+
121+
if (message.is_join) {
122+
formattedMessage = `\`\`\`ansi\n${formattedTime} ${formattedChnlBlue} ${formattedUser} ${messageBord} ${convertedMessage} ${messageBord}\n\`\`\``;
123+
} else {
124+
if (!message.channel) {
125+
formattedMessage = `\`\`\`ansi\n${formattedTime} ${formattedTell} ${formattedUser} ${messageBord} ${convertedMessage} ${messageBord}\n\`\`\``;
126+
} else {
127+
formattedMessage = `\`\`\`ansi\n${formattedTime} ${formattedChnlPink} ${formattedUser} ${messageBord} ${convertedMessage} ${messageBord}\n\`\`\``;
128+
}
129+
}
130+
131+
return formattedMessage;
132+
}
133+
134+
// Example messages
135+
const message1 = {
136+
"id": "3598b8a024e394b691559a8c",
137+
"t": 1515984655.629,
138+
"from_user": "sans_comedy",
139+
"msg": "`pThis is a ``Xlovely``p test message`",
140+
"channel": "0000"
141+
};
142+
const message2 = {
143+
"id": "3598b8a024e394b691559a8c",
144+
"t": 1515984653.345,
145+
"from_user": "sans_comedy",
146+
"msg": "user joined channel",
147+
"is_join": true,
148+
"channel": "0000"
149+
};
150+
const message3 = {
151+
"id": "8393b5e73a021ac084090aad",
152+
"t": 1515984660.132,
153+
"from_user": "com",
154+
"msg": "psst, this is a tell"
155+
};
156+
const messagespam = {
157+
"id": "3598b8a024e394b691559a8c",
158+
"t": 1515984655.629,
159+
"from_user": "sans_comedy",
160+
"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!`",
161+
"channel": "0000"
162+
};
163+
164+
// Test the Formatter
165+
console.log('Formatted Messages:\n' + Formatter(message1) + '\n' + Formatter(message2) + '\n' + Formatter(message3) + '\n' + Formatter(messagespam));

0 commit comments

Comments
 (0)