Skip to content

Commit 0ae55d9

Browse files
author
Dynxsty
authored
Merge pull request #4 from Java-Discord/andrew/properties
Added MultiProperties to Clean Up Bot Configuration
2 parents 5b8b013 + b5f146b commit 0ae55d9

File tree

5 files changed

+76
-94
lines changed

5 files changed

+76
-94
lines changed

src/main/java/com/javadiscord/javabot/Bot.java

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,46 @@
11
package com.javadiscord.javabot;
22

3+
import com.jagrosh.jdautilities.command.CommandClientBuilder;
4+
import com.jagrosh.jdautilities.commons.waiter.EventWaiter;
35
import com.javadiscord.javabot.commands.configuation.Config;
46
import com.javadiscord.javabot.commands.configuation.WelcomeImage;
57
import com.javadiscord.javabot.commands.custom_commands.CustomCommands;
68
import com.javadiscord.javabot.commands.moderation.*;
79
import com.javadiscord.javabot.commands.other.GuildConfig;
10+
import com.javadiscord.javabot.commands.other.Question;
11+
import com.javadiscord.javabot.commands.other.Shutdown;
12+
import com.javadiscord.javabot.commands.other.Version;
813
import com.javadiscord.javabot.commands.other.qotw.ClearQOTW;
914
import com.javadiscord.javabot.commands.other.qotw.Correct;
1015
import com.javadiscord.javabot.commands.other.qotw.Leaderboard;
11-
import com.javadiscord.javabot.commands.other.Question;
12-
import com.javadiscord.javabot.commands.other.Shutdown;
1316
import com.javadiscord.javabot.commands.other.suggestions.Accept;
1417
import com.javadiscord.javabot.commands.other.suggestions.Clear;
1518
import com.javadiscord.javabot.commands.other.suggestions.Decline;
1619
import com.javadiscord.javabot.commands.other.suggestions.Response;
1720
import com.javadiscord.javabot.commands.other.testing.*;
18-
import com.javadiscord.javabot.commands.other.Version;
1921
import com.javadiscord.javabot.commands.reaction_roles.ReactionRoles;
2022
import com.javadiscord.javabot.commands.user_commands.*;
2123
import com.javadiscord.javabot.events.*;
22-
import com.javadiscord.javabot.properties.ConfigString;
23-
import com.jagrosh.jdautilities.command.CommandClientBuilder;
24-
import com.jagrosh.jdautilities.commons.waiter.EventWaiter;
24+
import com.javadiscord.javabot.properties.MultiProperties;
2525
import net.dv8tion.jda.api.JDA;
2626
import net.dv8tion.jda.api.JDABuilder;
2727
import net.dv8tion.jda.api.OnlineStatus;
2828
import net.dv8tion.jda.api.requests.GatewayIntent;
2929
import net.dv8tion.jda.api.utils.MemberCachePolicy;
3030
import net.dv8tion.jda.api.utils.cache.CacheFlag;
3131

32+
import java.nio.file.Path;
33+
import java.util.Properties;
34+
3235

3336
public class Bot {
3437

3538
public static JDA jda;
3639
public static EventWaiter waiter;
3740

41+
private static final Properties properties = new MultiProperties(Path.of("bot.props"));
3842

3943
public static void main(String[] args) throws Exception {
40-
41-
ConfigString token = new ConfigString("token", "null");
4244
waiter = new EventWaiter();
4345

4446
CommandClientBuilder client = new CommandClientBuilder()
@@ -113,7 +115,7 @@ public static void main(String[] args) throws Exception {
113115
);
114116

115117

116-
jda = JDABuilder.createDefault(token.getValue())
118+
jda = JDABuilder.createDefault(properties.getProperty("token", "null"))
117119
.setStatus(OnlineStatus.DO_NOT_DISTURB)
118120
.setMemberCachePolicy(MemberCachePolicy.ALL)
119121
.enableCache(CacheFlag.ACTIVITY)
@@ -136,5 +138,26 @@ public static void main(String[] args) throws Exception {
136138
jda.addEventListener(new SlashCommands());
137139
//jda.addEventListener(new StarboardListener());
138140
}
141+
142+
/**
143+
* Gets the value of a property from the bot's loaded properties.
144+
* @see Properties#getProperty(String)
145+
* @param key The name of the property to get.
146+
* @return The value of the property, or <code>null</code> if none was found.
147+
*/
148+
public static String getProperty(String key) {
149+
return properties.getProperty(key);
150+
}
151+
152+
/**
153+
* Gets the value of a property from the bot's loaded properties.
154+
* @see Properties#getProperty(String, String)
155+
* @param key The name of the property to get.
156+
* @param defaultValue The value to return if no property was found.
157+
* @return The value of the property, or the default value.
158+
*/
159+
public static String getProperty(String key, String defaultValue) {
160+
return properties.getProperty(key, defaultValue);
161+
}
139162
}
140163

src/main/java/com/javadiscord/javabot/events/Startup.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package com.javadiscord.javabot.events;
22

3+
import com.javadiscord.javabot.Bot;
34
import com.javadiscord.javabot.commands.other.Version;
45
import com.javadiscord.javabot.other.Misc;
5-
import com.javadiscord.javabot.properties.ConfigString;
66
import com.mongodb.MongoClient;
77
import com.mongodb.MongoClientURI;
88
import net.dv8tion.jda.api.entities.SelfUser;
@@ -28,9 +28,7 @@ public void onReady(ReadyEvent event){
2828
bot = event.getJDA().getSelfUser();
2929

3030
try {
31-
32-
ConfigString login = new ConfigString("mongologin", "default");
33-
MongoClientURI uri = new MongoClientURI(login.getValue());
31+
MongoClientURI uri = new MongoClientURI(Bot.getProperty("mongologin", "default"));
3432
mongoClient = new MongoClient(uri);
3533

3634
LoggerFactory.getLogger(Startup.class).info("* Successfully connected to Database!");

src/main/java/com/javadiscord/javabot/properties/ConfigElement.java

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

src/main/java/com/javadiscord/javabot/properties/ConfigString.java

Lines changed: 0 additions & 40 deletions
This file was deleted.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.javadiscord.javabot.properties;
2+
3+
import java.io.FileInputStream;
4+
import java.io.IOException;
5+
import java.nio.file.Path;
6+
import java.util.Properties;
7+
8+
/**
9+
* This class is an extension of the standard Java properties, and supports
10+
* loading properties from multiple sources.
11+
*/
12+
public class MultiProperties extends Properties {
13+
/**
14+
* Initializes the properties from the list of paths, where each path should
15+
* point to a properties file. <strong>Properties in paths specified last
16+
* take precedence over those specified first.</strong>
17+
* <p>
18+
* For example, suppose this object is constructed from two properties
19+
* files, in this order:
20+
* <ol>
21+
* <li>props1.properties</li>
22+
* <li>props2.properties</li>
23+
* </ol>
24+
* If props1 contains <code>x=a</code> and props2 contains <code>x=b</code>,
25+
* then calling <code>getProperty("x")</code> on an instance of this
26+
* class with both those files given will return <code>"b"</code>,
27+
* because the later file overrides the earlier one.
28+
* </p>
29+
* @param paths The list of paths to read properties from.
30+
*/
31+
public MultiProperties(Path... paths) {
32+
for (Path path : paths) {
33+
Properties props = new Properties();
34+
try {
35+
props.load(new FileInputStream(path.toFile()));
36+
} catch (IOException e) {
37+
System.err.println("Could not load properties from path: " + path + ", Exception: " + e.getMessage());
38+
}
39+
this.putAll(props);
40+
}
41+
}
42+
}

0 commit comments

Comments
 (0)