@@ -14,29 +14,173 @@ public LoadJson(ILogger<CustomCommands> Logger)
1414 this . Logger = Logger ;
1515 }
1616
17- /// <summary>
18- /// Load the commands from the JSON file
19- /// </summary>
20- /// <returns>Returns the Commands as a List</returns>
21- public List < Commands > ? LoadCommandsFromJson ( string path )
17+ public List < Commands > GetCommandsFromJsonFiles ( string path )
2218 {
23- string jsonPath = Path . Combine ( path , "Commands.json" ) ;
24- if ( File . Exists ( jsonPath ) )
19+ var comms = new List < Commands > ( ) ;
20+
21+ CheckForExampleFile ( path ) ;
22+
23+ var pathofcommands = Path . Combine ( path , "Commands" ) ;
24+ var defaultconfigpath = Path . Combine ( path , "Commands.json" ) ;
25+
26+ var files = new List < string > ( ) ;
27+
28+ if ( Directory . Exists ( pathofcommands ) )
29+ files . AddRange ( Directory . GetFiles ( pathofcommands , "*.json" , SearchOption . AllDirectories ) ) ;
30+
31+ // Check if the default config file exists in plugins/CustomCommands
32+ if ( File . Exists ( defaultconfigpath ) )
33+ {
34+ files . Add ( defaultconfigpath ) ;
35+ Logger . LogInformation ( "Found default config file." ) ;
36+ }
37+ //
38+ else if ( ! File . Exists ( defaultconfigpath ) && files . Count == 0 )
39+ {
40+ Logger . LogWarning ( "No Config file found. Please create plugins/CustomCommands/Commands.json or in plugins/CustomCommands/Commands/<name>.json" ) ;
41+ return comms ;
42+ }
43+
44+ foreach ( var file in files )
45+ {
46+ var json = File . ReadAllText ( file ) ;
47+
48+ // Validate the JSON file
49+ if ( ! IsValidJsonSyntax ( file ) )
50+ continue ;
51+
52+ var commands = JsonSerializer . Deserialize < List < Commands > > ( json ) ;
53+ if ( ValidateObject ( commands , file ) )
54+ comms . AddRange ( commands ! ) ;
55+ }
56+ return comms ;
57+ }
58+ // Check if the Command.json file exists. If not replace it with the example file
59+ public void CheckForExampleFile ( string path )
60+ {
61+ var defaultconfigpath = Path . Combine ( path , "Commands.json" ) ;
62+ var exampleconfigpath = Path . Combine ( path , "Commands.example.json" ) ;
63+ if ( ! File . Exists ( defaultconfigpath ) )
64+ {
65+ File . Copy ( exampleconfigpath , defaultconfigpath ) ;
66+ Logger . LogInformation ( "Created default config file." ) ;
67+ }
68+
69+ }
70+ public bool IsValidJsonSyntax ( string path )
71+ {
72+ try
73+ {
74+ var json = File . ReadAllText ( path ) ;
75+ var document = JsonDocument . Parse ( json ) ;
76+ return true ;
77+ }
78+ catch ( JsonException ex )
2579 {
26- var json = File . ReadAllText ( jsonPath ) ;
27- return JsonSerializer . Deserialize < List < Commands > > ( json ) ;
80+ Logger . LogError ( $ "Invalid JSON syntax in { path } . Please check the docs on how to create a valid JSON file") ;
81+ Logger . LogError ( ex . Message ) ;
82+ return false ;
2883 }
29- else if ( File . Exists ( Path . Combine ( path , "Commands.example.json" ) ) )
84+ }
85+ public bool ValidateObject ( List < Commands > ? comms , string path )
86+ {
87+ if ( comms == null )
88+ {
89+ Logger . LogError ( $ "Invalid JSON format in { path } . Please check the docs on how to create a valid JSON file") ;
90+ return false ;
91+ }
92+ bool commandstatus = true ;
93+ for ( int i = 0 ; i < comms . Count ; i ++ )
94+ {
95+ commandstatus = true ;
96+ // Title
97+ if ( string . IsNullOrEmpty ( comms [ i ] . Title ) )
98+ {
99+ Logger . LogWarning ( $ "Title not set in { path } . Title is not required but recommended") ;
100+ commandstatus = false ;
101+ }
102+ // Description
103+ if ( string . IsNullOrEmpty ( comms [ i ] . Description ) )
104+ {
105+ Logger . LogWarning ( $ "Description not set in { path } . Description is not required but recommended. This will be shown in the help command") ;
106+ commandstatus = false ;
107+ }
108+ // Command
109+ if ( string . IsNullOrEmpty ( comms [ i ] . Command ) )
110+ {
111+ Logger . LogError ( $ "Command not set in { path } ") ;
112+ commandstatus = false ;
113+ }
114+ if ( ! PrintToCheck ( comms [ i ] ) )
115+ commandstatus = false ;
116+
117+ if ( ! commandstatus )
118+ {
119+ Logger . LogError ( $ "Command { comms [ i ] . Command } will not be loaded. Index: { i } ") ;
120+ LogCommandDetails ( comms [ i ] ) ;
121+ }
122+ }
123+ if ( ! commandstatus )
124+ return false ;
125+ return true ;
126+ }
127+
128+ public void LogCommandDetails ( Commands comms )
129+ {
130+ Logger . LogInformation ( $ "-- Title: { comms . Title } ") ;
131+ Logger . LogInformation ( $ "-- Description: { comms . Description } ") ;
132+ Logger . LogInformation ( $ "-- Command: { comms . Command } ") ;
133+ Logger . LogInformation ( $ "-- Message: { comms . Message } ") ;
134+ Logger . LogInformation ( $ "-- CenterMessage: { comms . CenterMessage . Message } ") ;
135+ Logger . LogInformation ( $ "-- CenterMessageTime: { comms . CenterMessage . Time } ") ;
136+ Logger . LogInformation ( $ "-- PrintTo: { comms . PrintTo } ") ;
137+ Logger . LogInformation ( $ "-- ServerCommands: { JsonSerializer . Serialize ( comms . ServerCommands ) } ") ;
138+ Logger . LogInformation ( $ "-- PermissionList: { JsonSerializer . Serialize ( comms . Permission ) } ") ;
139+ Logger . LogInformation ( "--------------------------------------------------" ) ;
140+ }
141+
142+ public bool PrintToCheck ( Commands comms )
143+ {
144+ if ( comms . PrintTo == Sender . ClientChat || comms . PrintTo == Sender . AllChat )
30145 {
31- Logger . LogWarning ( "No Config file found. Please rename Commands.example.json to Commands.json" ) ;
32- return null ;
146+
147+ if ( ! ValidateMessage ( comms . Message ) )
148+ {
149+ Logger . LogError ( $ "Message not set but needs to be set because PrintTo is set to { comms . PrintTo } ") ;
150+ return false ;
151+ }
33152 }
153+ else if ( comms . PrintTo == Sender . ClientCenter || comms . PrintTo == Sender . AllCenter )
154+ {
155+ if ( string . IsNullOrEmpty ( comms . CenterMessage . Message ) )
156+ {
157+ Logger . LogError ( $ "CenterMessage is not set but needs to be set because PrintTo is set to { comms . PrintTo } ") ;
158+ return false ;
159+ }
160+ }
34161 else
35162 {
36- Logger . LogWarning ( "No Config file found. Please create one" ) ;
37- return null ;
163+ if ( ! ValidateMessage ( comms . Message ) && string . IsNullOrEmpty ( comms . CenterMessage . Message ) )
164+ {
165+ Logger . LogError ( $ "Message and CenterMessage are not set but needs to be set because PrintTo is set to { comms . PrintTo } ") ;
166+ return false ;
167+ }
38168 }
169+ return true ;
39170 }
40171
41-
172+ public bool ValidateMessage ( dynamic message )
173+ {
174+ if ( message is JsonElement jsonElement )
175+ {
176+ if ( jsonElement . ValueKind == JsonValueKind . String )
177+ return true ;
178+
179+ if ( jsonElement . ValueKind == JsonValueKind . Array )
180+ return true ;
181+
182+ return false ;
183+ }
184+ return false ;
185+ }
42186}
0 commit comments