11package com .javadiscord .javabot ;
22
3+ import com .jagrosh .jdautilities .command .Command ;
34import com .jagrosh .jdautilities .command .CommandClientBuilder ;
45import com .jagrosh .jdautilities .commons .waiter .EventWaiter ;
5- import com .javadiscord .javabot .commands .configuation .Config ;
6- import com .javadiscord .javabot .commands .configuation .WelcomeImage ;
7- import com .javadiscord .javabot .commands .custom_commands .CustomCommands ;
8- import com .javadiscord .javabot .commands .moderation .*;
9- 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 ;
13- import com .javadiscord .javabot .commands .other .qotw .ClearQOTW ;
14- import com .javadiscord .javabot .commands .other .qotw .Correct ;
15- import com .javadiscord .javabot .commands .other .qotw .Leaderboard ;
16- import com .javadiscord .javabot .commands .other .suggestions .Accept ;
17- import com .javadiscord .javabot .commands .other .suggestions .Clear ;
18- import com .javadiscord .javabot .commands .other .suggestions .Decline ;
19- import com .javadiscord .javabot .commands .other .suggestions .Response ;
20- import com .javadiscord .javabot .commands .other .testing .*;
21- import com .javadiscord .javabot .commands .reaction_roles .ReactionRoles ;
22- import com .javadiscord .javabot .commands .user_commands .*;
236import com .javadiscord .javabot .events .*;
247import com .javadiscord .javabot .properties .MultiProperties ;
258import net .dv8tion .jda .api .JDA ;
2811import net .dv8tion .jda .api .requests .GatewayIntent ;
2912import net .dv8tion .jda .api .utils .MemberCachePolicy ;
3013import net .dv8tion .jda .api .utils .cache .CacheFlag ;
14+ import org .reflections .Reflections ;
3115
3216import java .nio .file .Path ;
17+ import java .util .Objects ;
3318import java .util .Properties ;
3419
3520
@@ -49,70 +34,7 @@ public static void main(String[] args) throws Exception {
4934 .setPrefix ("!" )
5035 .setEmojis ("✅" , "⚠️" , "❌" )
5136 .useHelpBuilder (false )
52- .addCommands (
53-
54- //UserCommands
55- new Avatar (),
56- new BotInfo (),
57- new ChangeMyMind (),
58- new Help (),
59- new IDCalc (),
60- new Lmgtfy (),
61- new Ping (),
62- new Profile (),
63- new ServerInfo (),
64- new Uptime (),
65-
66- //ReactionRoles
67- new ReactionRoles (),
68-
69- //CustomCommands
70- new CustomCommands (),
71-
72- //Other
73- new Question (),
74- new Shutdown (),
75- new Version (),
76- new GuildConfig (),
77-
78- //Other.Testing
79- new SampleSuggestion (),
80- new RefreshCategory (),
81- new MongoDBAddUser (),
82- new Image (),
83- new AddConfigFile (),
84- new UpdateUserFiles (),
85-
86- //Other.Suggestions
87- new Accept (),
88- new Clear (),
89- new Decline (),
90- new Response (),
91-
92- //Other.QOTW
93- new ClearQOTW (),
94- new Correct (),
95- new Leaderboard (),
96-
97- //Commands.Moderation
98- new Ban (),
99- new ClearWarns (),
100- new EditEmbed (),
101- new Embed (),
102- new Kick (),
103- new Mute (),
104- new Mutelist (),
105- new Purge (),
106- new Report (),
107- new Unban (),
108- new Unmute (),
109- new Warn (),
110- new Warns (),
111-
112- //Commands.Configuration
113- new Config (),
114- new WelcomeImage ()
115- );
37+ .addCommands (discoverCommands ());
11638
11739
11840 jda = JDABuilder .createDefault (properties .getProperty ("token" , "null" ))
@@ -159,5 +81,29 @@ public static String getProperty(String key) {
15981 public static String getProperty (String key , String defaultValue ) {
16082 return properties .getProperty (key , defaultValue );
16183 }
84+
85+ /**
86+ * Discovers and instantiates all commands found in the bot's "commands"
87+ * package. This uses the reflections API to find all classes in that
88+ * package which extend from the base {@link Command} class.
89+ * <p>
90+ * <strong>All command classes MUST have a no-args constructor.</strong>
91+ * </p>
92+ * @return The array of commands.
93+ */
94+ private static Command [] discoverCommands () {
95+ Reflections reflections = new Reflections ("com.javadiscord.javabot.commands" );
96+ return reflections .getSubTypesOf (Command .class ).stream ()
97+ .map (type -> {
98+ try {
99+ return (Command ) type .getDeclaredConstructor ().newInstance ();
100+ } catch (Exception e ) {
101+ e .printStackTrace ();
102+ return null ;
103+ }
104+ })
105+ .filter (Objects ::nonNull )
106+ .toArray (Command []::new );
107+ }
162108}
163109
0 commit comments