Skip to content
This repository was archived by the owner on Oct 21, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion ModularBot-Core/build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
dependencies {
compile 'net.dv8tion:JDA:3.6.0_375'

testImplementation project(':modularbot-command')
testImplementation project(':modularbot-logger')
testImplementation project(':modularbot-command')
testImplementation project(':modularbot-night-config-wrapper')
testImplementation project(':modularbot-nashorn-support')
testImplementation project(':modularbot-nashorn-command-support')
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,18 +108,28 @@ public void onReady(ReadyEvent event) {
* Triggered when a shard is ready.
*/
private void onReady() {
if (receivedReady.get() == shardsTotal)
if (receivedReady.get() == shardsTotal) {
logger.info("Shards ready !");
moduleManager.finalizeInitialization(this);
}
}

/**
* {@inheritDoc}
*/
@Override
protected ScheduledExecutorService createExecutor(ThreadFactory threadFactory) {
logger.debug("Creating a new executor.");
return Executors.newScheduledThreadPool(corePoolSize, r -> {
Thread t = threadFactory.newThread(r);
t.setPriority(Thread.NORM_PRIORITY + 1);

t.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread t, Throwable e) {
logger.error("Uncaught exception !", e);
}
});
return t;
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,13 +188,29 @@ public ModularBotBuilder autoLoadBaseModules() {

// Try night config module
try {
Class<? extends BaseModule> nightConfigModule = (Class<? extends BaseModule>) Class.forName("com.jesus_crie.modularbot_nightconfigwrapper.NightConfigWrapperModule");
Class<? extends BaseModule> nightConfigModule = (Class<? extends BaseModule>) Class.forName("com.jesus_crie.modularbot_night_config_wrapper.NightConfigWrapperModule");
moduleManager.registerModules(this, nightConfigModule);
} catch (ClassNotFoundException e) {
LOG.debug("Failed to autoload night config module.");
}

// TODO 16/06/18 renember to complete with new modules
// Try nashorn module
try {
Class<? extends BaseModule> nashornModule = (Class<? extends BaseModule>) Class.forName("com.jesus_crie.modularbot_nashorn_support.NashornSupportModule");
moduleManager.registerModules(this, nashornModule);
} catch (ClassNotFoundException e) {
LOG.debug("Failed to autoload nashorn module.");
}

// Try nashorn command module
try {
Class<? extends BaseModule> nashornCommandModule = (Class<? extends BaseModule>) Class.forName("com.jesus_crie.modularbot_nashorn_command_support.NashornCommandSupportModule");
moduleManager.registerModules(this, nashornCommandModule);
} catch (ClassNotFoundException e) {
LOG.debug("Failed to autoload nashorn command module.");
}

// TODO 16/06/18 remember to complete with new modules

return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public abstract class BaseModule implements Lifecycle {
*/
Lifecycle.State state = State.STOPPED;

protected final ModuleInfo info;
protected ModuleInfo info;

/**
* Reference to the instance of {@link ModularBot}.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.jesus_crie.modularbot;

import com.electronwill.nightconfig.core.file.FileConfig;
import com.jesus_crie.modularbot.module.BaseModule;
import com.jesus_crie.modularbot_command.AccessLevel;
import com.jesus_crie.modularbot_command.Command;
Expand All @@ -10,14 +9,16 @@
import com.jesus_crie.modularbot_command.annotations.RegisterPattern;
import com.jesus_crie.modularbot_command.processing.Option;
import com.jesus_crie.modularbot_command.processing.Options;
import com.jesus_crie.modularbot_nightconfigwrapper.NightConfigWrapperModule;
import com.jesus_crie.modularbot_logger.ConsoleLoggerModule;
import com.jesus_crie.modularbot_nashorn_command_support.NashornCommandSupportModule;
import com.jesus_crie.modularbot_nashorn_support.NashornSupportModule;
import com.jesus_crie.modularbot_nashorn_support.module.JavaScriptModule;
import com.jesus_crie.modularbot_night_config_wrapper.NightConfigWrapperModule;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Nonnull;
import javax.security.auth.login.LoginException;
import java.io.File;
import java.util.List;
import java.util.Optional;

public class ModularTestRun extends BaseModule {
Expand All @@ -26,30 +27,38 @@ public class ModularTestRun extends BaseModule {

public static void main(String[] args) {
final ModularBot bot = new ModularBotBuilder(args[0])
.autoLoadBaseModules()
//.autoLoadBaseModules()
.registerModules(
new ConsoleLoggerModule(),
new CommandModule(),
new NightConfigWrapperModule("./example/config.json"),
new NashornSupportModule("./example/scripts/"),
new NashornCommandSupportModule()
)
.useShutdownNow()
.build();

// ConsoleLoggerModule.MIN_LEVEL = ModularLog.Level.TRACE;

/// Commands

CommandModule cmd = bot.getModuleManager().getModule(CommandModule.class);
//cmd.setCreatorId(182547138729869314L);
cmd.registerCommands(new StopCommand());

/// Config

NightConfigWrapperModule config = bot.getModuleManager().getModule(NightConfigWrapperModule.class);
Optional<Long> startCount = config.getPrimaryConfig().getOptional("start_count");
long count = startCount.orElse(0L);
count++;
config.getPrimaryConfig().set("start_count", count);

config.loadConfigGroup("testConfigs", new File("./configs/"), true, "^.+\\.json$");
//config.addSecondaryConfigToGroup("testConfigs", "./configs/user.json");

List<FileConfig> userCfgs = config.getConfigGroup("testConfigs");
LOG.info(String.valueOf(userCfgs));
for (FileConfig cfg : userCfgs) {
cfg.set("hey", count);
}
/// JS

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

try {
bot.login();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ public void onLoad(@Nonnull final ModuleManager moduleManager, @Nullable Modular
System.out.println(message);
} else {
System.err.println(message);
if (log.error != null && log.level.getLevel() == ModularLog.Level.ERROR.getLevel())
log.error.printStackTrace();
}
}
});
Expand Down
5 changes: 5 additions & 0 deletions ModularBot-NashornCommandSupport/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
dependencies {
compile project(':modularbot-core')
compile project(':modularbot-command')
compile project(':modularbot-nashorn-support')
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.jesus_crie.modularbot_nashorn_command_support;

import com.jesus_crie.modularbot_command.AccessLevel;
import com.jesus_crie.modularbot_command.processing.CommandPattern;
import com.jesus_crie.modularbot_command.processing.Option;
import jdk.nashorn.api.scripting.ScriptObjectMirror;

/**
* A class that represent a command that can be extended in JS and wrapped into a {@link JavaScriptCommandWrapper JavaScriptCommandWrapper}
* to be registered in the command module.
*/
public class JavaScriptCommand {

public String[] aliases;
public String description = "No description.";
public String shortDescription = "No description.";

public AccessLevel accessLevel = AccessLevel.EVERYONE;
public Option[] options = new Option[0];

public CommandPattern[] patterns = new CommandPattern[0];

/**
* Create a {@link JavaScriptCommand JavaScriptCommand} with the given JS object.
* This method must be called from a JS script.
*
* @param mirror The JS object provided.
* @return A new instance of {@link JavaScriptCommand JavaScriptCommand} that holds the information provided.
*/
public static JavaScriptCommand from(ScriptObjectMirror mirror) {
final JavaScriptCommand command = new JavaScriptCommand();
command.aliases = ((ScriptObjectMirror) mirror.getMember("aliases")).to(String[].class);
command.description = (String) mirror.getMember("description");
command.shortDescription = (String) mirror.getMember("shortDescription");
command.accessLevel = (AccessLevel) mirror.getMember("accessLevel");
command.options = ((ScriptObjectMirror) mirror.getMember("options")).to(Option[].class);
command.patterns = ((ScriptObjectMirror) mirror.getMember("patterns")).to(CommandPattern[].class);

return command;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.jesus_crie.modularbot_nashorn_command_support;

import com.jesus_crie.modularbot_command.Command;

import javax.annotation.Nonnull;
import java.util.Collections;

/**
* Class that map the content of a {@link JavaScriptCommand JavaScriptCommand} into a real {@link Command Command}
* usable by the command module.
* <p>
* The command can't be modified after being wrapped.
*/
public class JavaScriptCommandWrapper extends Command {

public JavaScriptCommandWrapper(@Nonnull final JavaScriptCommand command) {
super(command.aliases[0], command.accessLevel, command.shortDescription, command.description);

aliases.clear();
Collections.addAll(aliases, command.aliases);
Collections.addAll(options, command.options);
Collections.addAll(patterns, command.patterns);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.jesus_crie.modularbot_nashorn_command_support;

import com.jesus_crie.modularbot.ModularBotBuilder;
import com.jesus_crie.modularbot.module.BaseModule;
import com.jesus_crie.modularbot.module.ModuleManager;
import com.jesus_crie.modularbot_command.CommandModule;
import com.jesus_crie.modularbot_nashorn_support.NashornSupportModule;
import com.jesus_crie.modularbot_nashorn_support.module.JavaScriptModule;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Nonnull;
import javax.script.ScriptException;

public class NashornCommandSupportModule extends BaseModule {

private static final Logger LOG = LoggerFactory.getLogger("NashornCommandSupportModule");

private static final ModuleInfo INFO = new ModuleInfo("JS Nashorn Command Support", "Jesus-Crie",
"https://github.com/JesusCrie/ModularBot", "1.0", 1);

public NashornCommandSupportModule() {
super(INFO);
}

@SuppressWarnings("ConstantConditions")
@Override
public void onLoad(@Nonnull ModuleManager moduleManager, @Nonnull ModularBotBuilder builder) {
final NashornSupportModule nashornModule = moduleManager.getModule(NashornSupportModule.class);
final CommandModule commandModule = moduleManager.getModule(CommandModule.class);

for (JavaScriptModule module : nashornModule.getModules()) {
try {
final JavaScriptCommand[] commands = (JavaScriptCommand[]) module.getEngine().invokeFunction("getCommands");

if (commands == null)
continue;

for (JavaScriptCommand command : commands) {
final JavaScriptCommandWrapper wrapper = new JavaScriptCommandWrapper(command);
commandModule.registerCommands(wrapper);
}

} catch (ScriptException | ClassCastException e) {
LOG.error("Failed to load commands from JS module: " + module.getJsModule().getInfo().getName(), e);
} catch (NoSuchMethodException ignore) {
// The module have no command to register.
}
}
}
}
3 changes: 3 additions & 0 deletions ModularBot-NashornSupport/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
dependencies {
compile project(':modularbot-core')
}
Loading