Skip to content
This repository was archived by the owner on Oct 21, 2022. It is now read-only.

Commit 9dc9a78

Browse files
author
Lucas Malandrino
authored
Merge pull request #9 from JesusCrie/dev
v2.2.0
2 parents 58a61b9 + 126b343 commit 9dc9a78

File tree

171 files changed

+6544
-785
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

171 files changed

+6544
-785
lines changed

ModularBot-Core/build.gradle

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
dependencies {
22
compile 'net.dv8tion:JDA:3.6.0_375'
33

4-
testImplementation project(':modularbot-command')
54
testImplementation project(':modularbot-logger')
5+
testImplementation project(':modularbot-command')
66
testImplementation project(':modularbot-night-config-wrapper')
7+
testImplementation project(':modularbot-nashorn-support')
8+
testImplementation project(':modularbot-nashorn-command-support')
79
}

ModularBot-Core/src/main/java/com/jesus_crie/modularbot/ModularBot.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,18 +108,28 @@ public void onReady(ReadyEvent event) {
108108
* Triggered when a shard is ready.
109109
*/
110110
private void onReady() {
111-
if (receivedReady.get() == shardsTotal)
111+
if (receivedReady.get() == shardsTotal) {
112+
logger.info("Shards ready !");
112113
moduleManager.finalizeInitialization(this);
114+
}
113115
}
114116

115117
/**
116118
* {@inheritDoc}
117119
*/
118120
@Override
119121
protected ScheduledExecutorService createExecutor(ThreadFactory threadFactory) {
122+
logger.debug("Creating a new executor.");
120123
return Executors.newScheduledThreadPool(corePoolSize, r -> {
121124
Thread t = threadFactory.newThread(r);
122125
t.setPriority(Thread.NORM_PRIORITY + 1);
126+
127+
t.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
128+
@Override
129+
public void uncaughtException(Thread t, Throwable e) {
130+
logger.error("Uncaught exception !", e);
131+
}
132+
});
123133
return t;
124134
});
125135
}

ModularBot-Core/src/main/java/com/jesus_crie/modularbot/ModularBotBuilder.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,13 +188,29 @@ public ModularBotBuilder autoLoadBaseModules() {
188188

189189
// Try night config module
190190
try {
191-
Class<? extends BaseModule> nightConfigModule = (Class<? extends BaseModule>) Class.forName("com.jesus_crie.modularbot_nightconfigwrapper.NightConfigWrapperModule");
191+
Class<? extends BaseModule> nightConfigModule = (Class<? extends BaseModule>) Class.forName("com.jesus_crie.modularbot_night_config_wrapper.NightConfigWrapperModule");
192192
moduleManager.registerModules(this, nightConfigModule);
193193
} catch (ClassNotFoundException e) {
194194
LOG.debug("Failed to autoload night config module.");
195195
}
196196

197-
// TODO 16/06/18 renember to complete with new modules
197+
// Try nashorn module
198+
try {
199+
Class<? extends BaseModule> nashornModule = (Class<? extends BaseModule>) Class.forName("com.jesus_crie.modularbot_nashorn_support.NashornSupportModule");
200+
moduleManager.registerModules(this, nashornModule);
201+
} catch (ClassNotFoundException e) {
202+
LOG.debug("Failed to autoload nashorn module.");
203+
}
204+
205+
// Try nashorn command module
206+
try {
207+
Class<? extends BaseModule> nashornCommandModule = (Class<? extends BaseModule>) Class.forName("com.jesus_crie.modularbot_nashorn_command_support.NashornCommandSupportModule");
208+
moduleManager.registerModules(this, nashornCommandModule);
209+
} catch (ClassNotFoundException e) {
210+
LOG.debug("Failed to autoload nashorn command module.");
211+
}
212+
213+
// TODO 16/06/18 remember to complete with new modules
198214

199215
return this;
200216
}

ModularBot-Core/src/main/java/com/jesus_crie/modularbot/module/BaseModule.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public abstract class BaseModule implements Lifecycle {
1212
*/
1313
Lifecycle.State state = State.STOPPED;
1414

15-
protected final ModuleInfo info;
15+
protected ModuleInfo info;
1616

1717
/**
1818
* Reference to the instance of {@link ModularBot}.

ModularBot-Core/src/test/java/com/jesus_crie/modularbot/ModularTestRun.java

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.jesus_crie.modularbot;
22

3-
import com.electronwill.nightconfig.core.file.FileConfig;
43
import com.jesus_crie.modularbot.module.BaseModule;
54
import com.jesus_crie.modularbot_command.AccessLevel;
65
import com.jesus_crie.modularbot_command.Command;
@@ -10,14 +9,16 @@
109
import com.jesus_crie.modularbot_command.annotations.RegisterPattern;
1110
import com.jesus_crie.modularbot_command.processing.Option;
1211
import com.jesus_crie.modularbot_command.processing.Options;
13-
import com.jesus_crie.modularbot_nightconfigwrapper.NightConfigWrapperModule;
12+
import com.jesus_crie.modularbot_logger.ConsoleLoggerModule;
13+
import com.jesus_crie.modularbot_nashorn_command_support.NashornCommandSupportModule;
14+
import com.jesus_crie.modularbot_nashorn_support.NashornSupportModule;
15+
import com.jesus_crie.modularbot_nashorn_support.module.JavaScriptModule;
16+
import com.jesus_crie.modularbot_night_config_wrapper.NightConfigWrapperModule;
1417
import org.slf4j.Logger;
1518
import org.slf4j.LoggerFactory;
1619

1720
import javax.annotation.Nonnull;
1821
import javax.security.auth.login.LoginException;
19-
import java.io.File;
20-
import java.util.List;
2122
import java.util.Optional;
2223

2324
public class ModularTestRun extends BaseModule {
@@ -26,30 +27,38 @@ public class ModularTestRun extends BaseModule {
2627

2728
public static void main(String[] args) {
2829
final ModularBot bot = new ModularBotBuilder(args[0])
29-
.autoLoadBaseModules()
30+
//.autoLoadBaseModules()
31+
.registerModules(
32+
new ConsoleLoggerModule(),
33+
new CommandModule(),
34+
new NightConfigWrapperModule("./example/config.json"),
35+
new NashornSupportModule("./example/scripts/"),
36+
new NashornCommandSupportModule()
37+
)
3038
.useShutdownNow()
3139
.build();
3240

41+
// ConsoleLoggerModule.MIN_LEVEL = ModularLog.Level.TRACE;
42+
43+
/// Commands
44+
3345
CommandModule cmd = bot.getModuleManager().getModule(CommandModule.class);
3446
//cmd.setCreatorId(182547138729869314L);
3547
cmd.registerCommands(new StopCommand());
3648

49+
/// Config
50+
3751
NightConfigWrapperModule config = bot.getModuleManager().getModule(NightConfigWrapperModule.class);
3852
Optional<Long> startCount = config.getPrimaryConfig().getOptional("start_count");
3953
long count = startCount.orElse(0L);
4054
count++;
4155
config.getPrimaryConfig().set("start_count", count);
4256

43-
config.loadConfigGroup("testConfigs", new File("./configs/"), true, "^.+\\.json$");
44-
//config.addSecondaryConfigToGroup("testConfigs", "./configs/user.json");
45-
46-
List<FileConfig> userCfgs = config.getConfigGroup("testConfigs");
47-
LOG.info(String.valueOf(userCfgs));
48-
for (FileConfig cfg : userCfgs) {
49-
cfg.set("hey", count);
50-
}
57+
/// JS
5158

52-
cmd.addCustomPrefixForGuild(264001800686796800L, "!!");
59+
NashornSupportModule js = bot.getModuleManager().getModule(NashornSupportModule.class);
60+
JavaScriptModule testModule = js.getModuleByName("test");
61+
LOG.info("Test module file: " + testModule.getScriptLocation().getName());
5362

5463
try {
5564
bot.login();

ModularBot-Logger/src/main/java/com/jesus_crie/modularbot_logger/ConsoleLoggerModule.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ public void onLoad(@Nonnull final ModuleManager moduleManager, @Nullable Modular
6161
System.out.println(message);
6262
} else {
6363
System.err.println(message);
64+
if (log.error != null && log.level.getLevel() == ModularLog.Level.ERROR.getLevel())
65+
log.error.printStackTrace();
6466
}
6567
}
6668
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
dependencies {
2+
compile project(':modularbot-core')
3+
compile project(':modularbot-command')
4+
compile project(':modularbot-nashorn-support')
5+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.jesus_crie.modularbot_nashorn_command_support;
2+
3+
import com.jesus_crie.modularbot_command.AccessLevel;
4+
import com.jesus_crie.modularbot_command.processing.CommandPattern;
5+
import com.jesus_crie.modularbot_command.processing.Option;
6+
import jdk.nashorn.api.scripting.ScriptObjectMirror;
7+
8+
/**
9+
* A class that represent a command that can be extended in JS and wrapped into a {@link JavaScriptCommandWrapper JavaScriptCommandWrapper}
10+
* to be registered in the command module.
11+
*/
12+
public class JavaScriptCommand {
13+
14+
public String[] aliases;
15+
public String description = "No description.";
16+
public String shortDescription = "No description.";
17+
18+
public AccessLevel accessLevel = AccessLevel.EVERYONE;
19+
public Option[] options = new Option[0];
20+
21+
public CommandPattern[] patterns = new CommandPattern[0];
22+
23+
/**
24+
* Create a {@link JavaScriptCommand JavaScriptCommand} with the given JS object.
25+
* This method must be called from a JS script.
26+
*
27+
* @param mirror The JS object provided.
28+
* @return A new instance of {@link JavaScriptCommand JavaScriptCommand} that holds the information provided.
29+
*/
30+
public static JavaScriptCommand from(ScriptObjectMirror mirror) {
31+
final JavaScriptCommand command = new JavaScriptCommand();
32+
command.aliases = ((ScriptObjectMirror) mirror.getMember("aliases")).to(String[].class);
33+
command.description = (String) mirror.getMember("description");
34+
command.shortDescription = (String) mirror.getMember("shortDescription");
35+
command.accessLevel = (AccessLevel) mirror.getMember("accessLevel");
36+
command.options = ((ScriptObjectMirror) mirror.getMember("options")).to(Option[].class);
37+
command.patterns = ((ScriptObjectMirror) mirror.getMember("patterns")).to(CommandPattern[].class);
38+
39+
return command;
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.jesus_crie.modularbot_nashorn_command_support;
2+
3+
import com.jesus_crie.modularbot_command.Command;
4+
5+
import javax.annotation.Nonnull;
6+
import java.util.Collections;
7+
8+
/**
9+
* Class that map the content of a {@link JavaScriptCommand JavaScriptCommand} into a real {@link Command Command}
10+
* usable by the command module.
11+
* <p>
12+
* The command can't be modified after being wrapped.
13+
*/
14+
public class JavaScriptCommandWrapper extends Command {
15+
16+
public JavaScriptCommandWrapper(@Nonnull final JavaScriptCommand command) {
17+
super(command.aliases[0], command.accessLevel, command.shortDescription, command.description);
18+
19+
aliases.clear();
20+
Collections.addAll(aliases, command.aliases);
21+
Collections.addAll(options, command.options);
22+
Collections.addAll(patterns, command.patterns);
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.jesus_crie.modularbot_nashorn_command_support;
2+
3+
import com.jesus_crie.modularbot.ModularBotBuilder;
4+
import com.jesus_crie.modularbot.module.BaseModule;
5+
import com.jesus_crie.modularbot.module.ModuleManager;
6+
import com.jesus_crie.modularbot_command.CommandModule;
7+
import com.jesus_crie.modularbot_nashorn_support.NashornSupportModule;
8+
import com.jesus_crie.modularbot_nashorn_support.module.JavaScriptModule;
9+
import org.slf4j.Logger;
10+
import org.slf4j.LoggerFactory;
11+
12+
import javax.annotation.Nonnull;
13+
import javax.script.ScriptException;
14+
15+
public class NashornCommandSupportModule extends BaseModule {
16+
17+
private static final Logger LOG = LoggerFactory.getLogger("NashornCommandSupportModule");
18+
19+
private static final ModuleInfo INFO = new ModuleInfo("JS Nashorn Command Support", "Jesus-Crie",
20+
"https://github.com/JesusCrie/ModularBot", "1.0", 1);
21+
22+
public NashornCommandSupportModule() {
23+
super(INFO);
24+
}
25+
26+
@SuppressWarnings("ConstantConditions")
27+
@Override
28+
public void onLoad(@Nonnull ModuleManager moduleManager, @Nonnull ModularBotBuilder builder) {
29+
final NashornSupportModule nashornModule = moduleManager.getModule(NashornSupportModule.class);
30+
final CommandModule commandModule = moduleManager.getModule(CommandModule.class);
31+
32+
for (JavaScriptModule module : nashornModule.getModules()) {
33+
try {
34+
final JavaScriptCommand[] commands = (JavaScriptCommand[]) module.getEngine().invokeFunction("getCommands");
35+
36+
if (commands == null)
37+
continue;
38+
39+
for (JavaScriptCommand command : commands) {
40+
final JavaScriptCommandWrapper wrapper = new JavaScriptCommandWrapper(command);
41+
commandModule.registerCommands(wrapper);
42+
}
43+
44+
} catch (ScriptException | ClassCastException e) {
45+
LOG.error("Failed to load commands from JS module: " + module.getJsModule().getInfo().getName(), e);
46+
} catch (NoSuchMethodException ignore) {
47+
// The module have no command to register.
48+
}
49+
}
50+
}
51+
}

0 commit comments

Comments
 (0)