Skip to content

Commit 0024310

Browse files
author
Slingexe
authored
Made the script more docker friendly
1 parent 37c6017 commit 0024310

12 files changed

Lines changed: 141 additions & 65 deletions

File tree

Dockerfile

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ FROM node:alpine
77
WORKDIR /bot
88
COPY --from=builder /bot/node_modules ./node_modules
99
COPY . .
10-
RUN echo {} >> config.json
11-
RUN echo {} >> channelMappings.json
1210

1311

1412
CMD ["node", "index.js"]

READEME-DOCKER.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@ Create a discord server (Its not recommended to have this bot in a public server
2424
Pull the latest release from [DockerHub](https://hub.docker.com/r/slingexe/hackmud-chat-client)
2525
`docker pull slingexe/hackmud-chat-client:latest`
2626

27-
Run the docker with the following command
28-
`docker run -e TOKEN=token123 -e CLIENTID=12345 -e GUILDID=12345 slingexe/hackmud-chat-client`
29-
`docker run -e TOKEN=token123 -e CLIENTID=12345 -e GUILDID=12345 -d slingexe/hackmud-chat-client` - For detatched mode
27+
Run the docker with the following command (Make sure to replace LOCALFOLDER to a location on your computer ex: /mnt/user/appdata/hackmud-chat-client)
28+
`docker run -v LOCALFOLDER:/config -e TOKEN=token123 -e CLIENTID=12345 -e GUILDID=12345 slingexe/hackmud-chat-client`
29+
`docker run -v LOCALFOLDER:/config -e TOKEN=token123 -e CLIENTID=12345 -e GUILDID=12345 -d slingexe/hackmud-chat-client` - For detatched mode
3030
Note: Once you run the docker once with the Token, ClientId and GuildId arguments you don't have to pass them again unless you want to change them
31+
If you already have a hackmud token you can pass `-e MUDTOKEN=token` if you don't keep following the steps
3132

3233
To get the three arguments follow these steps
3334
Copy the bot's discord token (Application > Bot > Token)

backend/docker/dockerstartup.js

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,24 @@ const isDocker = require('is-docker')
55

66
const env = process.env
77

8-
const configPath = path.resolve(__dirname, '../../config.json')
8+
let configPath
9+
if (isDocker() && !process.env.OVERRIDE) {
10+
try{
11+
configPath = '/config/config.json';
12+
} catch (error) {
13+
console.log("Error loading config files in Docker");
14+
console.log(error)
15+
}
16+
} else {
17+
configPath = path.resolve(__dirname, '../../config.json');
18+
}
19+
20+
// Ensure config.json exists
21+
if (!fs.existsSync(configPath)) {
22+
const defaultConfig = { "token": "", "clientId": "", "guildId": "" };
23+
fs.writeFileSync(configPath, JSON.stringify(defaultConfig, null, 4));
24+
console.log("Could not find config.json, created file with defaults");
25+
}
926

1027
async function dockerstartup() {
1128
if (isDocker() && !env.OVERRIDE) {
@@ -20,16 +37,20 @@ async function dockerstartup() {
2037

2138
if (env.TOKEN) {
2239
config.token = env.TOKEN
23-
console.log(`Found TOKEN: ${env.TOKEN}`)
40+
console.log(`Found TOKEN`)
2441
} else {console.log("env.TOKEN not found")}
2542
if (env.CLIENTID) {
2643
config.clientId = env.CLIENTID
27-
console.log(`Found CLIENTID: ${env.CLIENTID}`)
44+
console.log(`Found CLIENTID`)
2845
} else {console.log("env.CLIENTID not found")}
2946
if (env.GUILDID) {
3047
config.guildId = env.GUILDID
31-
console.log(`Found GUILDID: ${env.GUILDID}`)
48+
console.log(`Found GUILDID`)
3249
} else {console.log("env.GUILDID not found")}
50+
if (env.MUDTOKEN) {
51+
config.mudtoken = env.MUDTOKEN
52+
console.log(`Found MUDTOKEN`)
53+
} else {console.log("env.MUDTOKEN not found")}
3354

3455
fs.writeFileSync(configPath, JSON.stringify(config, null, 4));
3556

backend/fetchNewMessages.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,9 @@ let lastTimestamp = fiveMinutesAgoToRubyTS();
163163

164164
async function fetchNewMessages(client) {
165165
const channelMappings = await loadChnlMap();
166+
167+
if (!channelMappings) {console.log(`No channel mappings found, can't send messages`); return}
168+
166169
const pullusers = await loadConfigVar("pullusers");
167170
const mudtoken = await loadConfigVar("mudtoken");
168171
const apiUrl = 'https://www.hackmud.com/mobile/chats.json';

backend/fix.js

Lines changed: 0 additions & 26 deletions
This file was deleted.

backend/loadvar.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,24 @@
11
const fs = require('fs');
22
const { readFile } = require('fs/promises');
33
const path = require('path');
4+
const isDocker = require('is-docker')
5+
6+
let configPath
7+
let channelMappingsPath
8+
9+
if (isDocker() && !process.env.OVERRIDE) {
10+
try{
11+
configPath = '/config/config.json';
12+
channelMappingsPath = '/config/channelMappings.json';
13+
} catch (error) {
14+
console.log("Error loading config files in Docker");
15+
console.log(error)
16+
}
17+
} else {
18+
configPath = path.resolve(__dirname, './../config.json');
19+
channelMappingsPath = path.resolve(__dirname, './../channelMappings.json');
20+
}
421

5-
const configPath = path.resolve(__dirname, './../config.json');
6-
const channelMappingsPath = path.resolve(__dirname, './../channelMappings.json');
722

823
// Ensure config.json exists
924
if (!fs.existsSync(configPath)) {

commands/mud/settings.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,21 @@ const fetch = require('node-fetch');
44
const fs = require('node:fs');
55
const path = require('path');
66
const { create } = require('node:domain');
7-
const configPath = path.resolve(__dirname, '../../config.json');
7+
const isDocker = require('is-docker')
8+
let configPath
9+
let mappingsPath
10+
if (isDocker() && !process.env.OVERRIDE) {
11+
try{
12+
configPath = '/config/config.json';
13+
mappingsPath = '/config/channelMappings.json';
14+
} catch (error) {
15+
console.log("Error loading config files in Docker");
16+
console.log(error)
17+
}
18+
} else {
19+
configPath = path.resolve(__dirname, './../config.json');
20+
mappingsPath = path.resolve(__dirname, '../../channelMappings.json');
21+
}
822

923
async function createChannel(guild, name, categoryid) {
1024
const discordChannelName = name;
@@ -180,10 +194,6 @@ module.exports = {
180194
let chnlid = await createChannel(guild, user, chatCategory.id)
181195
channelMapping[user] = chnlid;
182196
}
183-
184-
185-
186-
const mappingsPath = path.resolve(__dirname, '../../channelMappings.json');
187197

188198
if (!fs.existsSync(mappingsPath)) {
189199
fs.writeFileSync(mappingsPath, JSON.stringify({}, null, 4)); // Create an empty JSON file

commands/utility/info.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,31 @@ const fetch = require('node-fetch');
44
const fs = require('node:fs');
55
const path = require('path');
66
const { json } = require('node:stream/consumers');
7-
const configPath = path.resolve(__dirname, '../../config.json');
8-
const chnlMapPath = path.resolve(__dirname, '../../channelMappings.json')
7+
8+
const isDocker = require('is-docker')
9+
let configPath
10+
let chnlMapPath
11+
if (isDocker() && !process.env.OVERRIDE) {
12+
try{
13+
configPath = '/config/config.json';
14+
chnlMapPath = '/config/channelMappings.json';
15+
} catch (error) {
16+
console.log("Error loading config files in Docker");
17+
console.log(error)
18+
}
19+
} else {
20+
configPath = path.resolve(__dirname, '../../config.json');
21+
chnlMapPath = path.resolve(__dirname, '../../channelMappings.json')
22+
}
23+
924

1025
module.exports = {
1126
category: 'utility',
1227
data: new SlashCommandBuilder()
1328
.setName('info')
1429
.setDescription('Gets some info from the bot'),
1530
async execute(interaction, client) {
16-
const config = JSON.parse(fs.readFileSync(configPath, 'utf-8'));
31+
const config = json.parse(fs.readFileSync(configPath, 'utf-8'));
1732
const chnmap = json.parse(fs.readFileSync(chnlMapPath, 'utf-8'));
1833
let info = {}
1934

deploy-commands.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
11
// This script was taken from the discord.js documentation
2+
const isDocker = require('is-docker')
3+
let configPath
4+
if (isDocker() && !process.env.OVERRIDE) {
5+
try{
6+
configPath = '/config/config.json';
7+
} catch (error) {
8+
console.log("Error loading config files in Docker");
9+
console.log(error)
10+
}
11+
} else {
12+
configPath = path.resolve(__dirname, './../config.json');
13+
}
14+
const { token, clientId, guildId } = require(configPath);
215

316
const { REST, Routes } = require('discord.js');
4-
const { clientId, guildId, token } = require('./config.json');
517
const fs = require('node:fs');
618
const path = require('node:path');
719

events/messageCreate.js

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,20 @@ const { Events } = require('discord.js');
22
const { loadConfigVar, loadChnlMap } = require('./../backend/loadvar.js');
33
const { readFile } = require('fs/promises');
44
const fs = require('node:fs');
5+
const path = require('path');
6+
const isDocker = require('is-docker')
7+
let configPath
8+
if (isDocker() && !process.env.OVERRIDE) {
9+
try{
10+
configPath = '/config/config.json';
11+
} catch (error) {
12+
console.log("Error loading config files in Docker");
13+
console.log(error)
14+
}
15+
} else {
16+
configPath = path.resolve(__dirname, './../config.json');
17+
}
18+
519

620
module.exports = {
721
name: Events.MessageCreate,
@@ -14,26 +28,28 @@ module.exports = {
1428
key => channelMappings[key] === message.channel.id
1529
);
1630
if (channelName == ".sanitize") {return}
17-
1831
if (channelName) {
1932
if (message.content.startsWith('%')) {
2033
const remainingText = message.content.slice(1).trim();
2134

2235
if (remainingText) {
23-
const channel = remainingText;
24-
25-
const configPath = './config.json';
26-
let config = {};
36+
try {
37+
const channel = remainingText;
38+
39+
let config = {};
40+
41+
if (fs.existsSync(configPath)) {
42+
const fileData = fs.readFileSync(configPath, 'utf-8');
43+
config = JSON.parse(fileData);
44+
}
2745

28-
if (fs.existsSync(configPath)) {
29-
const fileData = fs.readFileSync(configPath, 'utf-8');
30-
config = JSON.parse(fileData);
46+
config.setchannel = channel;
47+
fs.writeFileSync(configPath, JSON.stringify(config, null, 4));
48+
message.react('✅');
49+
} catch (error) {
50+
console.log(error);
51+
message.react('❌');
3152
}
32-
33-
config.setchannel = channel;
34-
fs.writeFileSync(configPath, JSON.stringify(config, null, 4));
35-
//console.log(`Updated config channel: ${remainingText}`);
36-
message.react('✅');
3753
}
3854
} else {
3955
const mudToken = await loadConfigVar("mudtoken");
@@ -57,19 +73,17 @@ module.exports = {
5773
headers: { 'Content-Type': 'application/json' },
5874
body: JSON.stringify(payload),
5975
});
60-
6176
const result = await response.json();
62-
6377
if (result.ok === true) {
6478
message.react('✅');
6579
//console.log(`Message sent to ${setChannel} successfully. Message: ${finalMessage}`);
6680
} else {
6781
message.react('❌');
68-
//console.log(`Failed tp send message. Server response: ${result.msg || 'Unknown error'}`);
82+
console.log(`Failed to send message. Server response: ${result.msg || 'Unknown error'}`);
6983
}
7084
} catch (error) {
7185
message.react('❌');
72-
console.error(error);
86+
console.log(error);
7387
}
7488
}
7589
setTimeout(function(){

0 commit comments

Comments
 (0)