@@ -2,6 +2,20 @@ const { Events } = require('discord.js');
22const { loadConfigVar, loadChnlMap } = require ( './../backend/loadvar.js' ) ;
33const { readFile } = require ( 'fs/promises' ) ;
44const 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
620module . 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