Skip to content

Commit 91b0505

Browse files
author
Slingexe
authored
Added a way a new channel to send sanitized messages so you can use the discord search feature
1 parent fdb9b43 commit 91b0505

8 files changed

Lines changed: 356 additions & 50 deletions

File tree

backend/docker/dockerstartup.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@ const env = process.env
88
const configPath = path.resolve(__dirname, '../../config.json')
99

1010
async function dockerstartup() {
11-
if (env.CODESPACE == 1) { // I have this since I use GitHub Codespaces and its a docker container itself :P
12-
console.log("Codespace ENV Variable found. \n Not running dockerstartup");
13-
}
14-
else if (isDocker()) {
11+
if (isDocker() && !env.OVERRIDE) {
1512
console.log('Running inside a Docker container');
1613
const configRaw = await readFile(configPath, 'utf8');
1714
let config

backend/fetchNewMessages.js

Lines changed: 51 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const { EmbedBuilder } = require('discord.js')
12
const { loadConfigVar, loadChnlMap } = require('./loadvar.js');
23

34
// Full Hackmud-to-Discord color mapping //  = U+001B
@@ -67,7 +68,7 @@ const hackmudToDiscordColors = {
6768
'Z': '', // Hackmud: #0C112B | Discord: Gray
6869
};
6970

70-
function Formatter(message) {
71+
function Formatter(message, sanitize) {
7172
function convertHackmudColors(text) {
7273
const originalMsg = text
7374
let colorcount = 0
@@ -101,33 +102,49 @@ function Formatter(message) {
101102
return regexrun
102103
}
103104
}
105+
function sanitizeMsg(text) {
106+
const regex = /`([a-zA-Z0-9])([^`]*)`/g;
107+
let regexrun = text.replace(regex, (match, code, content) => {
108+
return content;
109+
});
110+
return `${regexrun}`
111+
}
104112

105-
// Extract timestamp and format it
106-
const timestamp = new Date(message.t * 1000);
107-
const hours = timestamp.getHours().toString().padStart(2, '0');
108-
const minutes = timestamp.getMinutes().toString().padStart(2, '0');
109-
const formattedTime = `${hours}${minutes}`;
113+
let formattedMessage
110114

111-
// Format user, channel, and other elements
112-
const formattedUser = `${message.from_user}`;
113-
const formattedChnlBlue = `${message.channel}`;
114-
const formattedChnlPink = `${message.channel}`;
115-
const formattedTell = `tell`;
116-
const messageBord = `:::`;
115+
if (!sanitize) {
116+
const timestamp = new Date(message.t * 1000);
117+
const hours = timestamp.getHours().toString().padStart(2, '0');
118+
const minutes = timestamp.getMinutes().toString().padStart(2, '0');
119+
const formattedTime = `${hours}${minutes}`;
117120

118-
let formattedMessage = "";
121+
// Format user, channel, and other elements
122+
const formattedUser = `${message.from_user}`;
123+
const formattedChnlBlue = `${message.channel}`;
124+
const formattedChnlPink = `${message.channel}`;
125+
const formattedTell = `tell`;
126+
const messageBord = `:::`;
119127

120-
// Apply color conversion to the message text
121-
const convertedMessage = convertHackmudColors(message.msg);
128+
// Apply color conversion to the message text
129+
const convertedMessage = convertHackmudColors(message.msg);
122130

123-
if (message.is_join) {
124-
formattedMessage = `\`\`\`ansi\n${formattedTime} ${formattedChnlBlue} ${formattedUser} ${messageBord} ${convertedMessage} ${messageBord}\n\`\`\``;
125-
} else {
126-
if (!message.channel) {
127-
formattedMessage = `\`\`\`ansi\n${formattedTime} ${formattedTell} ${formattedUser} ${messageBord} ${convertedMessage} ${messageBord}\n\`\`\``;
131+
if (message.is_join) {
132+
formattedMessage = `\`\`\`ansi\n${formattedTime} ${formattedChnlBlue} ${formattedUser} ${messageBord} ${convertedMessage} ${messageBord}\n\`\`\``;
128133
} else {
129-
formattedMessage = `\`\`\`ansi\n${formattedTime} ${formattedChnlPink} ${formattedUser} ${messageBord} ${convertedMessage} ${messageBord}\n\`\`\``;
134+
if (!message.channel) {
135+
formattedMessage = `\`\`\`ansi\n${formattedTime} ${formattedTell} ${formattedUser} ${messageBord} ${convertedMessage} ${messageBord}\n\`\`\``;
136+
} else {
137+
formattedMessage = `\`\`\`ansi\n${formattedTime} ${formattedChnlPink} ${formattedUser} ${messageBord} ${convertedMessage} ${messageBord}\n\`\`\``;
138+
}
130139
}
140+
} else {
141+
let data = {}
142+
data.timestamp = `<t:` + Math.trunc(message.t) + `:f>`
143+
data.username = message.from_user
144+
if (message.channel) {data.channel = message.channel} else {data.channel = "tell"}
145+
data.msg = sanitizeMsg(message.msg)
146+
147+
formattedMessage = data
131148
}
132149

133150
return formattedMessage;
@@ -167,6 +184,19 @@ async function fetchNewMessages(client) {
167184
//console.log(`No new messages for user: ${user}`);
168185
} else {
169186
messages.forEach(async (message) => {
187+
const sanchannel = client.channels.cache.get(channelMappings['.sanitize'])
188+
const data = Formatter(message, true)
189+
const sanembed = new EmbedBuilder()
190+
.setTitle('Message')
191+
.setDescription(`${data.msg}\n`)
192+
.addFields(
193+
{ name: 'Info', value: `User: ${data.username}\n Channel: ${data.channel}\n Time: ${data.timestamp}` },
194+
)
195+
.setTimestamp()
196+
.setFooter({ text: 'Hackmud Chat Client' });
197+
198+
sanchannel.send({ embeds: [sanembed] });
199+
170200
const discordChannelId = channelMappings[user]
171201
if (discordChannelId) {
172202
const formattedMessage = Formatter(message)

commands/mud/settings.js

Lines changed: 52 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,26 @@ const { loadConfigVar, loadChnlMap } = require('./../../backend/loadvar.js');
33
const fetch = require('node-fetch');
44
const fs = require('node:fs');
55
const path = require('path');
6+
const { create } = require('node:domain');
67
const configPath = path.resolve(__dirname, '../../config.json');
78

9+
async function createChannel(guild, name, categoryid) {
10+
const discordChannelName = name;
11+
12+
let discordChannel = guild.channels.cache.find(
13+
(channel) => channel.type === 0 && channel.name === discordChannelName
14+
);
15+
16+
if (!discordChannel) {
17+
discordChannel = await guild.channels.create({
18+
name: discordChannelName,
19+
type: 0,
20+
parent: categoryid,
21+
});
22+
console.log(`Channel created: ${discordChannelName}, ID: ${discordChannel.id}`);
23+
}
24+
return discordChannel.id
25+
}
826

927
module.exports = {
1028
category: 'mud',
@@ -55,6 +73,18 @@ module.exports = {
5573
.setRequired(true)
5674
)
5775
),
76+
/* Not Fully Implemented Yet
77+
.addSubcommand(subcommand =>
78+
subcommand
79+
.setName('ping-detection')
80+
.setDescription(`Will ping @everone when one of your ingame users get @'ed inside a message`)
81+
.addBooleanOption((option) =>
82+
option
83+
.setname('value')
84+
.setDescription('value')
85+
.setRequired(true)
86+
)
87+
)*/
5888
async execute(interaction) {
5989
const option = interaction.options.getSubcommand();
6090

@@ -78,7 +108,6 @@ module.exports = {
78108
if (result.ok == true) {
79109
const chatToken = result.chat_token;
80110

81-
const configPath = './config.json';
82111
let config = {};
83112

84113
if (fs.existsSync(configPath)) {
@@ -87,6 +116,7 @@ module.exports = {
87116
}
88117

89118
config.mudtoken = chatToken;
119+
config.mudtokendate =
90120

91121
fs.writeFileSync(configPath, JSON.stringify(config, null, 4));
92122
console.log('New mudtoken has been set:', chatToken)
@@ -144,25 +174,14 @@ module.exports = {
144174
}
145175

146176
const channelMapping = {};
147-
177+
channelMapping['.sanitize'] = await createChannel(guild, 'hmcc-search', chatCategory.id)
148178
for (const user of Object.keys(users)) {
149-
const discordChannelName = user;
150-
151-
let discordChannel = guild.channels.cache.find(
152-
(channel) => channel.type === 0 && channel.name === discordChannelName
153-
);
154-
155-
if (!discordChannel) {
156-
discordChannel = await guild.channels.create({
157-
name: discordChannelName,
158-
type: 0,
159-
parent: chatCategory.id,
160-
});
161-
console.log(`Channel created: ${discordChannelName}, ID: ${discordChannel.id}`);
162-
}
163-
164-
channelMapping[user] = discordChannel.id;
179+
180+
let chnlid = await createChannel(guild, user, chatCategory.id)
181+
channelMapping[user] = chnlid;
165182
}
183+
184+
166185

167186
const mappingsPath = path.resolve(__dirname, '../../channelMappings.json');
168187

@@ -187,7 +206,7 @@ module.exports = {
187206
const pullHistory = interaction.options.getBoolean('pull');
188207

189208
try {
190-
const config = JSON.parse(fs.readFileSync(configPath, 'utf-8'));
209+
let config = JSON.parse(fs.readFileSync(configPath, 'utf-8'));
191210

192211
if (!Array.isArray(config.pullusers)) {
193212
config.pullusers = [];
@@ -215,7 +234,7 @@ module.exports = {
215234
if (option === 'color') {
216235
const cmdcolorval = interaction.options.getString('value');
217236
if (cmdcolorval.match(/^[a-zA-Z0-9]$/) || cmdcolorval == "reset") {
218-
const config = JSON.parse(fs.readFileSync(configPath, 'utf-8'));
237+
let config = JSON.parse(fs.readFileSync(configPath, 'utf-8'));
219238

220239
if (cmdcolorval == "reset") {
221240
config.setcolor = null;
@@ -230,5 +249,18 @@ module.exports = {
230249
await interaction.reply({content: 'Invalid color value. Please use a single alphanumeric character or "reset"', flags: MessageFlags.Ephemeral });
231250
}
232251
}
252+
if (option === 'ping-detection') {
253+
const value = interaction.options.getBoolean('value');
254+
try {
255+
let config = JSON.parse(fs.readFileSync(configPath, 'utf-8'));
256+
config.pd = value
257+
fs.writeFileSync(configPath, JSON.stringify(config, null, 4), 'utf-8');
258+
259+
await interaction.reply({content: `Successfully updated setting. Ping Detection: **${value ? 'Enabled' : 'Disabled'}**`, flags: MessageFlags.Ephemeral });
260+
} catch(error) {
261+
console.error(error);
262+
await interaction.reply({content: 'An error occurred while setting this option. Check console for details.', flags: MessageFlags.Ephemeral });
263+
}
264+
}
233265
}
234266
};

commands/utility/info.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const prettyms = require('pretty-ms')
2+
const { SlashCommandBuilder, MessageFlags } = require("discord.js");
3+
const fetch = require('node-fetch');
4+
const fs = require('node:fs');
5+
const path = require('path');
6+
const { json } = require('node:stream/consumers');
7+
const configPath = path.resolve(__dirname, '../../config.json');
8+
const chnlMapPath = path.resolve(__dirname, '../../channelMappings.json')
9+
10+
module.exports = {
11+
category: 'utility',
12+
data: new SlashCommandBuilder()
13+
.setName('info')
14+
.setDescription('Gets some info from the bot'),
15+
async execute(interaction, client) {
16+
const config = JSON.parse(fs.readFileSync(configPath, 'utf-8'));
17+
const chnmap = json.parse(fs.readFileSync(chnlMapPath, 'utf-8'));
18+
let info = {}
19+
20+
// Displays some required config options
21+
if (config.clientId && config.clientId !== "") {info.cid = true} else {info.cid = false}
22+
if (config.guildId && config.guildId !== "") {info.gid = true} else {info.gid = false}
23+
if (config.mudtoken) {info.mtoken = true} else {info.mtoken = false}
24+
if (config.pullusers) {info.pu = config.pullusers} else {info.pu = false}
25+
26+
// Displays some other config variables
27+
if (config.setcolor) {info.scc = config.setcolor}
28+
if (config.setchannel) {info.sccnl = config.setchannel}
29+
30+
}
31+
}

events/messageCreate.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ module.exports = {
1313
const channelName = Object.keys(channelMappings).find(
1414
key => channelMappings[key] === message.channel.id
1515
);
16+
if (channelName == ".sanitize") {return}
1617

1718
if (channelName) {
1819
if (message.content.startsWith('%')) {

0 commit comments

Comments
 (0)