use of de.nikos410.discordbot.framework.CommandModule in project de-DiscordBot by DACH-Discord.
the class DiscordBot method command_help.
private void command_help(final IMessage message) {
final EmbedBuilder embedBuilder = new EmbedBuilder();
for (final String key : this.loadedModules.keySet()) {
Object module = this.loadedModules.get(key);
String moduleHelp = "";
for (Method method : module.getClass().getMethods()) {
if (method.isAnnotationPresent(CommandSubscriber.class)) {
final CommandSubscriber annotation = method.getDeclaredAnnotationsByType(CommandSubscriber.class)[0];
if (this.getUserPermissionLevel(message.getAuthor(), message.getGuild()) >= annotation.permissionLevel()) {
final String command = annotation.command();
final String help = annotation.help();
moduleHelp = moduleHelp + "`" + command + "` " + help + '\n';
}
}
}
final CommandModule[] annotations = module.getClass().getDeclaredAnnotationsByType(CommandModule.class);
final String moduleName = annotations[0].moduleName();
if (!moduleHelp.isEmpty()) {
embedBuilder.appendField(moduleName, moduleHelp, false);
}
}
final EmbedObject embedObject = embedBuilder.build();
Util.sendEmbed(message.getAuthor().getOrCreatePMChannel(), embedObject);
if (!message.getChannel().isPrivate()) {
Util.sendMessage(message.getChannel(), ":mailbox_with_mail:");
}
}
use of de.nikos410.discordbot.framework.CommandModule in project de-DiscordBot by DACH-Discord.
the class DiscordBot method discoverModules.
/**
* Finds all classes in the package {@link de.nikos410.discordbot.modules} that extend {@link CommandModule}
* and populates the module map using the module names as keys.
*/
private void discoverModules() {
// Search in package 'de.nikos410.discordbot.modules'
final Reflections reflections = new Reflections("de.nikos410.discordbot.modules");
// Find classes that are subtypes of CommandModule
final Set<Class<? extends CommandModule>> foundModuleClasses = reflections.getSubTypesOf(CommandModule.class);
for (Class<? extends CommandModule> currentModuleClass : foundModuleClasses) {
final ModuleWrapper module = new ModuleWrapper(currentModuleClass);
modules.put(module.getName(), module);
}
}
use of de.nikos410.discordbot.framework.CommandModule in project de-DiscordBot by DACH-Discord.
the class DiscordBot method loadModules.
/**
* Loads modules from classes that are located in the package 'de.nikos410.discordbot.modules' and are annotated
* with @CommandModule.
*/
private void loadModules() {
LOG.debug("Loading modules.");
LOG.info("Found {} total module(s).", this.modules.size());
// Load modules from all found classes
for (ModuleWrapper wrapper : this.modules.values()) {
try {
loadModule(wrapper);
} catch (Exception e) {
LOG.error("Failed to load module " + wrapper.getName() + ".", e);
wrapper.setStatus(ModuleStatus.FAILED);
}
}
// Wait until the bot is ready to run initializations
LOG.debug("Waiting until the bot is ready.");
while (!client.isReady()) {
try {
TimeUnit.MILLISECONDS.sleep(100);
} catch (InterruptedException e) {
LOG.warn("Sleep was interrupted");
Thread.currentThread().interrupt();
}
}
LOG.debug("Bot is ready. Running Inits.");
for (ModuleWrapper wrapper : this.modules.values()) {
final CommandModule module = wrapper.getInstance();
if (wrapper.getStatus().equals(ModuleStatus.ACTIVE)) {
module.initWhenReady();
}
}
// Create command map
this.makeCommandMap();
LOG.info("{} module(s) with {} command(s) active.", this.modules.size(), this.commands.size());
}
use of de.nikos410.discordbot.framework.CommandModule in project de-DiscordBot by DACH-Discord.
the class DiscordBot method loadModule.
public String loadModule(final String moduleName) throws NullPointerException {
if (moduleName.isEmpty()) {
return "Fehler! Kein Modul angegeben.";
}
if (!this.unloadedModules.containsKey(moduleName)) {
return "Fehler! Modul `" + moduleName + "` ist bereits aktiviert oder existiert nicht.";
}
// Modul in andere Map übertragen und entfernen
final Object module = this.unloadedModules.get(moduleName);
this.loadedModules.put(moduleName, module);
this.unloadedModules.remove(moduleName);
this.makeCommandMap();
// Modul aus JSON-Array entfernen
final JSONArray jsonUnloadedModules = this.configJSON.getJSONArray("unloadedModules");
for (int i = 0; i < jsonUnloadedModules.length(); i++) {
final String unloadedModuleName = jsonUnloadedModules.getString(i);
if (unloadedModuleName.equals(moduleName)) {
jsonUnloadedModules.remove(i);
}
}
this.saveJSON();
// EventListener aktivieren
final CommandModule moduleAnnotation = module.getClass().getDeclaredAnnotationsByType(CommandModule.class)[0];
if (!moduleAnnotation.commandOnly()) {
try {
this.client.getDispatcher().registerListener(module);
} catch (NullPointerException e) {
System.err.println("[Error] Could not get EventDispatcher!");
throw e;
}
}
return ":white_check_mark: Modul `" + moduleName + "` aktiviert.";
}
use of de.nikos410.discordbot.framework.CommandModule in project de-DiscordBot by DACH-Discord.
the class DiscordBot method addModule.
/**
* Ein Modul zum Bot hinzufügen
*
* @param module Das Modul
*/
private void addModule(final Object module) {
if (!module.getClass().isAnnotationPresent(CommandModule.class)) {
System.err.println("Error: Class \"" + module.getClass().getName() + "\" is not a command module!");
return;
}
final CommandModule moduleAnnotation = module.getClass().getDeclaredAnnotationsByType(CommandModule.class)[0];
final String moduleName = moduleAnnotation.moduleName();
final JSONArray jsonUnloadedModules = this.configJSON.getJSONArray("unloadedModules");
for (int i = 0; i < jsonUnloadedModules.length(); i++) {
final String unloadedModuleName = jsonUnloadedModules.getString(i);
if (moduleName.equals(unloadedModuleName)) {
// Modul ist in der Liste der deaktivierten Module enthalten -> ist deaktiviert
this.unloadedModules.put(moduleName, module);
return;
}
}
// Modul ist nicht deaktiviert
this.loadedModules.put(moduleName, module);
}
Aggregations