Skip to content
This repository has been archived by the owner on Aug 23, 2021. It is now read-only.

Commit

Permalink
1.2.0 PermissionHandler rework; Properties config handler
Browse files Browse the repository at this point in the history
  • Loading branch information
MJaroslav committed Aug 31, 2018
1 parent 00a9fc6 commit f25e591
Show file tree
Hide file tree
Showing 23 changed files with 969 additions and 276 deletions.
12 changes: 1 addition & 11 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
apply plugin: 'java'

apply plugin: "application"
mainClassName = "mjaroslav.bots.amadeustest.AmadeusTest"
archivesBaseName = "AmadeusCore"
version = "1.0.1"
version = "1.2.0"

compileJava.options.encoding = 'UTF-8'

Expand All @@ -12,9 +11,6 @@ tasks.withType(JavaCompile) {
}

task fatJar(type: Jar) {
manifest {
attributes "Main-Class": mainClassName
}
baseName = project.name + "-all"
from {
configurations.compile.collect {
Expand All @@ -24,12 +20,6 @@ task fatJar(type: Jar) {
with jar
}

jar {
manifest {
attributes "Main-Class": mainClassName
}
}

repositories {
jcenter()
maven {
Expand Down
96 changes: 63 additions & 33 deletions src/main/java/mjaroslav/bots/core/amadeus/AmadeusCore.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package mjaroslav.bots.core.amadeus;

import java.io.File;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
Expand All @@ -13,16 +12,19 @@
import org.apache.logging.log4j.Logger;

import mjaroslav.bots.core.amadeus.auth.AuthHandler;
import mjaroslav.bots.core.amadeus.auth.DefaultAuthHandler;
import mjaroslav.bots.core.amadeus.auth.DefaultUserHomeAuthHandler;
import mjaroslav.bots.core.amadeus.commands.BaseCommand;
import mjaroslav.bots.core.amadeus.commands.CommandHandler;
import mjaroslav.bots.core.amadeus.commands.DefaultCommandHandler;
import mjaroslav.bots.core.amadeus.config.ConfigurationHandler;
import mjaroslav.bots.core.amadeus.config.DefaultConfiguration;
import mjaroslav.bots.core.amadeus.lang.DefaultLangHandler;
import mjaroslav.bots.core.amadeus.lang.LangHandler;
import mjaroslav.bots.core.amadeus.permissions.DefaultPermissionHandler;
import mjaroslav.bots.core.amadeus.permissions.PermissionHandler;
import mjaroslav.bots.core.amadeus.utils.AmadeusUtils;
import mjaroslav.bots.core.amadeus.utils.AmadeusUtils.Action;
import mjaroslav.bots.core.amadeus.utils.JSONUtils;
import sx.blah.discord.api.ClientBuilder;
import sx.blah.discord.api.IDiscordClient;
import sx.blah.discord.api.events.EventSubscriber;
Expand All @@ -40,26 +42,46 @@ public abstract class AmadeusCore {
private final HashMap<String, CommandHandler> commands = new HashMap<String, CommandHandler>();
private final HashMap<String, ConfigurationHandler> configs = new HashMap<String, ConfigurationHandler>();
private LangHandler langs;
private PermissionHandler permissions;

//
// Other
//
public final Logger log = LogManager.getLogger(getName());
public final Logger log;
private IDiscordClient client;
private boolean isReady = false;
public boolean devMode = false;
public boolean hideInvite = true;

//
// Defaults
//
public final DefaultConfiguration DEFAULTCONFIG = new DefaultConfiguration(this);

// Bot (program) name, NO DISCORD APP NAME.
public abstract String getName();
// Dir for bot files (configs, langs, etc).
public abstract long getDevId();
// Developer's discord ID.
public abstract File getFolder();

public final DefaultConfiguration DEFAULTCONFIG;

public final BotInfo info;

public AmadeusCore() throws Exception {
info = JSONUtils.fromJson(getClass().getResourceAsStream("/botinfo.json"), BotInfo.class);
if (!info.valid())
throw new IllegalArgumentException("Error in 'name', 'dev_ids' or 'folder' field!");
info.core = this;
log = LogManager.getLogger(info.getName());
DEFAULTCONFIG = new DefaultConfiguration(this);
}

/**
* Main bot object
*
* @param name Bot (program) name, NO DISCORD APP NAME.
* @param devId Developer's discord ID.
* @param folder Dir for bot files (configs, langs, etc).
*/
public AmadeusCore(String name, long[] devIds, String folder) {
info = new BotInfo(this, name, devIds, folder);
log = LogManager.getLogger(name);
DEFAULTCONFIG = new DefaultConfiguration(this);
}

/**
* Just call it when your bot will be ready.
*
Expand All @@ -68,25 +90,25 @@ public abstract class AmadeusCore {
public boolean startBot() {
try {
client = new ClientBuilder().withToken(getAuthHandler().loadToken()).login();
client.getDispatcher().registerListener(new EventHandler(this));
AmadeusUtils.waitAction(3000, 20, new Action() {
@Override
public boolean done() {
return isReady;
}
});
if (isReady) {
registerConfigurationHandlers();
registerCommandHandlers();
registerCommands();
loadAll();
log.info(translate("bot.ready"));
}
return isReady;
} catch (Exception e) {
e.printStackTrace();
return false;
}
client.getDispatcher().registerListener(new EventHandler(this));
AmadeusUtils.waitAction(360000L, new Action() {
@Override
public synchronized boolean done() {
return isReady;
}
});
if (isReady) {
registerConfigurationHandlers();
registerCommandHandlers();
registerCommands();
loadAll();
log.info(translate("bot.ready"));
}
return false;
return isReady;
}

//
Expand Down Expand Up @@ -122,9 +144,7 @@ public void loadOthers() {
}

public void loadPerms() {
for (CommandHandler handler : listOfCommandHandlers())
if (handler.hasPermissionHandller())
handler.getPermissionHandler().loadPermissions();
getPermissionHandler().loadPermissions();
}

public void loadNames() {
Expand All @@ -148,6 +168,16 @@ public List<String> getPermissionsList(String handlerName) {
return result;
}

public void setPermissionHandler(PermissionHandler handler) {
permissions = handler;
}

public PermissionHandler getPermissionHandler() {
if (permissions == null)
permissions = new DefaultPermissionHandler(this);
return permissions;
}

public List<String> getPermissionsList() {
ArrayList<String> result = new ArrayList<String>();
result.add("*"); // Admin
Expand All @@ -162,7 +192,7 @@ public List<String> getPermissionsList() {
}

//
// Ñonfigurations
// Configurations
//
public void addConfigurationHandler(ConfigurationHandler handler) {
configs.put(handler.name, handler);
Expand Down Expand Up @@ -226,7 +256,7 @@ public String translate(String key, Object... objects) {
//
public AuthHandler getAuthHandler() {
if (authHandler == null)
authHandler = new DefaultAuthHandler(this);
authHandler = new DefaultUserHomeAuthHandler(this);
return authHandler;
}

Expand Down
6 changes: 6 additions & 0 deletions src/main/java/mjaroslav/bots/core/amadeus/AmadeusInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package mjaroslav.bots.core.amadeus;

public class AmadeusInfo {
public static final String NAME = "AmadeusCore";
public static final String VERSION = "1.2.0";
}
142 changes: 142 additions & 0 deletions src/main/java/mjaroslav/bots/core/amadeus/BotInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package mjaroslav.bots.core.amadeus;

import java.io.File;

import com.google.gson.annotations.SerializedName;

import mjaroslav.bots.core.amadeus.utils.AmadeusUtils;
import sx.blah.discord.handle.obj.IUser;
import sx.blah.discord.util.EmbedBuilder;

public class BotInfo {
public AmadeusCore core;

@SerializedName("name")
private String name;
@SerializedName("dev_ids")
private long[] devIds;
@SerializedName("folder")
private String folder;
@SerializedName("version")
private String version;
@SerializedName("source_url")
private String sourceUrl;
@SerializedName("logo_url")
private String logoUrl;
@SerializedName("bot_site")
private String botSite;
@SerializedName("credits")
private String credits;
@SerializedName("description")
private String desc;
@SerializedName("invite")
private String invite;

public BotInfo() {
}

public BotInfo(AmadeusCore core, String name, long[] devIds, String folder) {
this.core = core;
this.name = name;
this.devIds = devIds;
this.folder = folder;
}

public String getName() {
return name;
}

public long[] getDevIds() {
return devIds;
}

public File getFolder() {
return new File(folder);
}

public String getSourceUrl() {
return sourceUrl;
}

public boolean hasSourceUrl() {
return AmadeusUtils.stringIsNotEmpty(sourceUrl);
}

public String getDescription() {
return desc;
}

public boolean hasDescription() {
return AmadeusUtils.stringIsNotEmpty(desc);
}

public String getCredits() {
return credits;
}

public boolean hasCredits() {
return AmadeusUtils.stringIsNotEmpty(credits);
}

public String getInvite() {
return invite;
}

public boolean hasInvite() {
return AmadeusUtils.stringIsNotEmpty(invite);
}

public String getLogoUrl() {
return logoUrl;
}

public boolean hasLogoUrl() {
return AmadeusUtils.stringIsNotEmpty(logoUrl);
}

public String getBotSite() {
return botSite;
}

public boolean hasBotSite() {
return AmadeusUtils.stringIsNotEmpty(botSite);
}

public String getVersion() {
return version;
}

public boolean hasVersion() {
return AmadeusUtils.stringIsNotEmpty(version);
}

public EmbedBuilder toEmbedBuilder() {
EmbedBuilder builder = new EmbedBuilder();
builder.withAuthorName(AmadeusInfo.NAME + " > " + getName());
builder.appendField(core.translate("core.amadeusversion"), AmadeusInfo.VERSION, true);
if (hasVersion())
builder.appendField(core.translate("core.botversion"), getVersion(), true);
if (hasDescription())
builder.appendField(core.translate("core.botdescription"), getDescription(), true);
if (hasCredits())
builder.appendField(core.translate("core.botcredits"), getCredits(), true);
if (hasSourceUrl())
builder.appendField(core.translate("core.botsource"), getSourceUrl(), true);
if (hasBotSite())
builder.appendField(core.translate("core.botsite"), getBotSite(), true);
StringBuilder sbuilder = new StringBuilder();
for (long id : devIds) {
IUser user = core.getClient().getUserByID(id);
if (user != null)
sbuilder.append(user.mention(true) + " ");
}
builder.appendField(core.translate("core.devs"), sbuilder.toString().trim(), false);
builder.withThumbnail(
hasLogoUrl() ? getLogoUrl() : core.getClient().getOurUser().getAvatarURL().replace("webp", "png"));
return builder;
}

public boolean valid() {
return AmadeusUtils.stringIsNotEmpty(name) && devIds != null && devIds.length > 0 && folder != null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@
import mjaroslav.bots.core.amadeus.AmadeusCore;

public abstract class AuthHandler {
private final AmadeusCore core;
public final AmadeusCore core;

public AuthHandler(AmadeusCore core) {
this.core = core;
}

public abstract boolean saveToken(String token);
public abstract boolean saveToken(String token) throws Exception;

public abstract String loadToken();
public abstract String loadToken() throws Exception;

public abstract File getFolder();

public final File getFile() {
return getFolder().toPath().resolve(core.getName() + ".json").toFile();
return getFolder().toPath().resolve(core.info.getName() + ".json").toFile();
}
}
Loading

0 comments on commit f25e591

Please sign in to comment.