Skip to content

Commit f19c385

Browse files
author
Slingexe
authored
Added Debug Mode
1 parent a3968d9 commit f19c385

14 files changed

Lines changed: 79 additions & 15 deletions

File tree

READEME-DOCKER.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ The guild the bot is in should now have all of your users set as channels and it
4646
Type in the channels the bot created to send a message to hackmud
4747
Change the channel the bot sends to by doing the same thing you would normally do with the chat box (%n00bz / %0000)
4848

49+
## Debug Mode
50+
In the docker run command add `-e DEBUG=true -e LOG_SENSITIVE_INFO=true`
51+
4952
## Contributions
5053
All contributions are greatly appriciated! I am not great at coding so expect lots of spaghetti code.
5154

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Copy (or rename) the example config "configex.json" to "config.json"
2525
Copy the bot's discord token to "token" (Application > Bot > Token)
2626
Copy the application's clientID to "clientId" (Application > OAuth > ClientID)
2727
Copy the guildID to "guildId" (Turn on dev mode > Right Click guild > CopyID)
28+
Download the dependencies `npm insall --omit=dev`
2829
Run the BOT using `node index.js`
2930

3031
In hackmud run the command `chat_pass` and copy the result
@@ -38,6 +39,10 @@ The guild the bot is in should now have all of your users set as channels and it
3839
Type in the channels the bot created to send a message to hackmud
3940
Change the channel the bot sends to by doing the same thing you would normally do with the chat box (%n00bz / %0000)
4041

42+
## Debug Mode
43+
Linux - `export DEBUG=true` and `export LOG_SENSITIVE_INFO=true`
44+
Windows - `set DEBUG=true` and `set LOG_SENSITIVE_INFO=true`
45+
4146
## Contributions
4247
All contributions are greatly appriciated! I am not great at coding so expect lots of spaghetti code.
4348

backend/debug/log.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const dbgenv = process.env.DEBUG;
2+
3+
async function log() {
4+
if (dbgenv === 'true') {
5+
for (let i = 0; i < arguments.length; i++) {
6+
console.log(arguments[i]);
7+
}
8+
console.log("---- END ----")
9+
return true
10+
}
11+
return false
12+
}
13+
14+
module.exports = {
15+
log
16+
};

backend/docker/dockerstartup.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const fs = require('fs')
22
const { readFile } = require('fs/promises');
33
const path = require('path');
4+
const { log } = require('../debug/log.js');
45
const isDocker = require('is-docker')
56

67
const env = process.env
@@ -51,6 +52,7 @@ async function dockerstartup() {
5152
config.mudtoken = env.MUDTOKEN
5253
console.log(`Found MUDTOKEN`)
5354
} else {console.log("env.MUDTOKEN not found")}
55+
5456

5557
fs.writeFileSync(configPath, JSON.stringify(config, null, 4));
5658

@@ -59,6 +61,7 @@ async function dockerstartup() {
5961
} else {
6062
console.log("Not running inside a dockers")
6163
}
64+
log("---- Docker Startup ----", "-- ENV --", `TOKEN: ${env.LOG_SENSITIVE_INFO === true ? env.TOKEN : "HIDDEN"}`, `CID: ${env.CLIENTID}`, `GID: ${env.GUILDID}`, `MUDTOKEN: ${env.LOG_SENSITIVE_INFO === true ? env.MUDTOKEN : "HIDDEN"}`, `OVERRIDE: ${env.OVERRIDE === true ? "True" : "False"}`, "-- PATHS --", configPath);
6265
}
6366

6467
module.exports.dockerstartup = dockerstartup;

backend/fetchNewMessages.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const { EmbedBuilder } = require('discord.js')
22
const { loadConfigVar, loadChnlMap } = require('./loadvar.js');
3+
const { log } = require('./debug/log.js');
34

45
// Full Hackmud-to-Discord color mapping //  = U+001B
56
const hackmudToDiscordColors = {
@@ -217,6 +218,7 @@ async function fetchNewMessages(client) {
217218
})
218219
// Update the last timestamp
219220
lastTimestamp = NowToRubyTS()+1;
221+
log("---- Fetched Messages ----", payload, result, lastTimestamp);
220222
} else {
221223
console.error('Hackmud API error:', result.msg || 'Unknown error');
222224
}

backend/loadvar.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const fs = require('fs');
22
const { readFile } = require('fs/promises');
33
const path = require('path');
4+
const { log } = require('./debug/log.js');
45
const isDocker = require('is-docker')
56

67
let configPath
@@ -18,7 +19,7 @@ if (isDocker() && !process.env.OVERRIDE) {
1819
configPath = path.resolve(__dirname, './../config.json');
1920
channelMappingsPath = path.resolve(__dirname, './../channelMappings.json');
2021
}
21-
22+
log("---- LoadVar.js Config Paths ----", configPath, channelMappingsPath);
2223

2324
// Ensure config.json exists
2425
if (!fs.existsSync(configPath)) {
@@ -37,6 +38,7 @@ async function loadConfigVar(key) {
3738
try {
3839
const configRaw = await readFile(configPath, 'utf8');
3940
const config = JSON.parse(configRaw);
41+
log(`LoadConfigVar was called, Input: ${key}, Output: ${config[key]}`);
4042
return config[key] ?? null; // Return null if key doesn't exist
4143
} catch (error) {
4244
console.error(`Error loading config variable "${key}":`, error.message);
@@ -47,6 +49,7 @@ async function loadConfigVar(key) {
4749
async function loadChnlMap() {
4850
try {
4951
const mapRaw = await readFile(channelMappingsPath, 'utf8');
52+
log(`LoadChnlMap was called, Output: ${mapRaw}`);
5053
return JSON.parse(mapRaw);
5154
} catch (error) {
5255
console.error("Error loading channel mappings:", error.message);

commands/mud/client.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const { SlashCommandBuilder, MessageFlags, ActivityType } = require('discord.js');
22
const { fetchNewMessages } = require('./../../backend/fetchNewMessages.js')
33
const { setChatPullInterval, clearChatPullInterval, getChatPullInterval } = require('./../../backend/pullInterval');
4+
const { log } = require('./../../backend/debug/log.js');
45

56
module.exports = {
67
category: 'mud',
@@ -21,10 +22,12 @@ module.exports = {
2122
const option = interaction.options.getSubcommand();
2223
if (option === 'start') {
2324
if (getChatPullInterval()) {
25+
log("Someone tried to start the chat pull loop, but it's already running.");
2426
await interaction.reply({content: 'Chat pull loop is already running.', flags: MessageFlags.Ephemeral });
2527
return;
2628
}
27-
29+
30+
log("Chat pull loop started.");
2831
await interaction.client.user.setStatus('online');
2932
await interaction.client.user.setActivity({ type: ActivityType.Custom, name: "custom", state: "Listening for new messages..." });
3033

@@ -35,13 +38,15 @@ module.exports = {
3538
}
3639
if (option === 'stop') {
3740
if (getChatPullInterval()) {
41+
log("Chat pull loop stopped.");
3842
clearChatPullInterval();
3943

4044
await interaction.client.user.setStatus('idle');
4145
await interaction.client.user.setActivity(' for new messages...', { type: ActivityType.Custom, name: "custom", state: "Bot Idle..." });
4246

4347
await interaction.reply({content: 'Chat pull loop has been stopped.', flags: MessageFlags.Ephemeral });
4448
} else {
49+
log("Someone tried to stop the chat pull loop, but it's not running.");
4550
await interaction.reply({content: 'The chat pull loop is not running.', flags: MessageFlags.Ephemeral });
4651
}
4752
}

commands/mud/settings.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
const { SlashCommandBuilder, MessageFlags } = require('discord.js');
22
const { loadConfigVar, loadChnlMap } = require('./../../backend/loadvar.js');
3+
const { log } = require('./../../backend/debug/log.js');
34
const fetch = require('node-fetch');
45
const fs = require('node:fs');
56
const path = require('path');
6-
const { create } = require('node:domain');
7-
const isDocker = require('is-docker')
7+
8+
const isDocker = require('is-docker');
89
let configPath
910
let mappingsPath
1011
if (isDocker() && !process.env.OVERRIDE) {
@@ -20,6 +21,8 @@ if (isDocker() && !process.env.OVERRIDE) {
2021
mappingsPath = path.resolve(__dirname, '../../channelMappings.json');
2122
}
2223

24+
log("---- Settings.js Config Paths ----", configPath, mappingsPath);
25+
2326
async function createChannel(guild, name, categoryid) {
2427
const discordChannelName = name;
2528

@@ -130,12 +133,14 @@ module.exports = {
130133
}
131134

132135
config.mudtoken = chatToken;
133-
config.mudtokendate =
136+
// config.mudtokendate =
134137

135138
fs.writeFileSync(configPath, JSON.stringify(config, null, 4));
136-
console.log('New mudtoken has been set:', chatToken)
139+
console.log('New mudtoken has been set');
140+
log("---- Settings.js - Auth ----", `New mudtoken has been set: ${env.LOG_SENSITIVE_INFO === 'true' ? chatToken : "HIDDEN"}`, payload, `${env.LOG_SENSITIVE_INFO === true ? result : "HIDDEN"}`, config, configPath);
137141
await interaction.reply({content: `Config updated successfully! Token has been set.`, flags: MessageFlags.Ephemeral });
138142
} else {
143+
log("---- Settings.js - Auth ----", 'Failed to update mudtoken', payload, result);
139144
console.error(result)
140145
await interaction.reply({content: `Failed to update config. Server response: ${result.msg || 'Unknown error'}`, flags: MessageFlags.Ephemeral });
141146
}
@@ -200,9 +205,11 @@ module.exports = {
200205
}
201206

202207
fs.writeFileSync(mappingsPath, JSON.stringify(channelMapping, null, 4));
203-
208+
209+
log("---- Settings.js - Setup ----", 'Sucessfully ran setup', `${env.LOG_SENSITIVE_INFO === true ? payload : "HIDDEN"}`, result, channelMapping, mappingsPath);
204210
await interaction.reply({content: 'Server has been set up successfully, and user channels have been created or reused under the "chat" category.', flags: MessageFlags.Ephemeral });
205211
} else {
212+
log("---- Settings.js - Setup ----", 'Failed to run setup', `${env.LOG_SENSITIVE_INFO === true ? payload : "HIDDEN"}`, result);
206213
console.error(result);
207214
await interaction.reply({content: `Failed to run setup. Server response: ${result.msg || 'Unknown error'}`, flags: MessageFlags.Ephemeral });
208215
}
@@ -234,7 +241,8 @@ module.exports = {
234241
}
235242

236243
fs.writeFileSync(configPath, JSON.stringify(config, null, 4), 'utf-8');
237-
244+
245+
log("---- Settings.js - Manage Users ----", 'Successfully updated settings', username, pullHistory, config, configPath);
238246
await interaction.reply({content: `Successfully updated settings for user **${username}**. Pull history: **${pullHistory ? 'Enabled' : 'Disabled'}**`, flags: MessageFlags.Ephemeral });
239247
} catch (error) {
240248
console.error(error);
@@ -255,7 +263,9 @@ module.exports = {
255263
}
256264

257265
fs.writeFileSync(configPath, JSON.stringify(config, null, 4), 'utf-8');
266+
log("---- Settings.js - Color ----", 'Successfully updated setting', cmdcolorval, config, configPath);
258267
} else {
268+
log("---- Settings.js - Color ----", 'Invalid color value', cmdcolorval);
259269
await interaction.reply({content: 'Invalid color value. Please use a single alphanumeric character or "reset"', flags: MessageFlags.Ephemeral });
260270
}
261271
}
@@ -267,6 +277,7 @@ module.exports = {
267277
fs.writeFileSync(configPath, JSON.stringify(config, null, 4), 'utf-8');
268278

269279
await interaction.reply({content: `Successfully updated setting. Ping Detection: **${value ? 'Enabled' : 'Disabled'}**`, flags: MessageFlags.Ephemeral });
280+
log("---- Settings.js - Ping Detection ----", 'Successfully updated setting', value, config, configPath);
270281
} catch(error) {
271282
console.error(error);
272283
await interaction.reply({content: 'An error occurred while setting this option. Check console for details.', flags: MessageFlags.Ephemeral });

commands/mud/tell.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const { SlashCommandBuilder, MessageFlags } = require('discord.js');
22
const { loadConfigVar, loadChnlMap } = require('./../../backend/loadvar.js');
33
const fetch = require('node-fetch');
4+
const { log } = require('./../../backend/debug/log.js');
45

56
module.exports = {
67
category: 'mud',
@@ -50,7 +51,7 @@ module.exports = {
5051

5152
const result = await response.json();
5253
if (result.ok === true) {
53-
console.log(`Message sent to ${tellusr} successfully. Message: ${tellmsg}`);
54+
log(`---- tell.js ----`, `TellUsr: ${tellusr}`, `TellMsg: ${tellmsg}`, `ChannelName: ${channelName}`, `ChannelId: ${interaction.channelId}`, `Result: ${result.ok}`, `Msg: ${result.msg}`, channelMappings);
5455
interaction.reply({content: `Message sent to ${tellusr} successfully. Message: ${tellmsg}`, flags: MessageFlags.Ephemeral });
5556
} else {
5657
console.log(`Failed to send message. Server response: ${result.msg || 'Unknown error'}`);

deploy-commands.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
// This script was taken from the discord.js documentation
1+
// This script was taken from the discord.js documentation and modified by Slingo to fit the needs of the project.
2+
const { log } = require('./backend/debug/log.js');
3+
24
const isDocker = require('is-docker')
35
let configPath
46
if (isDocker() && !process.env.OVERRIDE) {
@@ -11,6 +13,8 @@ if (isDocker() && !process.env.OVERRIDE) {
1113
} else {
1214
configPath = path.resolve(__dirname, './../config.json');
1315
}
16+
log("---- deploy-commands.js Config Paths ----", configPath);
17+
1418
const { token, clientId, guildId } = require(configPath);
1519

1620
const { REST, Routes } = require('discord.js');

0 commit comments

Comments
 (0)