Skip to content
This repository has been archived by the owner on Oct 1, 2019. It is now read-only.

Commit

Permalink
Release 2.5.0: LastFm
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikos410 committed Jan 16, 2019
2 parents b926bfd + 49700af commit c574f48
Show file tree
Hide file tree
Showing 9 changed files with 624 additions and 30 deletions.
14 changes: 12 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
buildscript {
repositories {
jcenter()
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath 'com.github.jengelman.gradle.plugins:shadow:4.0.3'
classpath "gradle.plugin.com.github.spotbugs:spotbugs-gradle-plugin:1.6.6"
}
}

apply plugin: 'java'
apply plugin: 'application'
apply plugin: 'com.github.johnrengelman.shadow'
apply plugin: "com.github.spotbugs"

spotbugs {
ignoreFailures = true
}

// Hack to correctly apply system file encoding
tasks.withType(JavaCompile) {
options.encoding = "UTF-8"
}

version = 2.4
version = '2.5.0'
sourceCompatibility = 1.8
mainClassName = 'de.nikos410.discordbot.DiscordBot'

Expand All @@ -34,9 +43,10 @@ dependencies {
compile group: 'ch.qos.logback', name: 'logback-classic', version: '1.2.3'
compile group: 'org.apache.commons', name: 'commons-text', version: '1.6'
compile group: 'io.sentry', name: 'sentry-logback', version: '1.7.15'
compile group: 'de.u-mass', name: 'lastfm-java', version: '0.1.2'
}

repositories {
jcenter()
mavenCentral()
}
}
37 changes: 28 additions & 9 deletions src/main/java/de/nikos410/discordbot/DiscordBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public class DiscordBot {

private final List<String> unloadedModules = new ArrayList<>();
private final Map<String, Object> loadedModules = new HashMap<>();
private final List<String> failedModules = new ArrayList<>();

private final Map<String, Command> commands = new HashMap<>();

Expand Down Expand Up @@ -191,17 +192,26 @@ private void loadModule(final Class<?> moduleClass) {
LOG.debug("Loading module \"{}\".", moduleName);
// Create an instance of the class
final Object moduleObject = makeModuleObject(moduleClass);
if (moduleObject != null) {
this.loadedModules.put(moduleName, moduleObject);

// Register EventListener if needed
if (!moduleAnnotation.commandOnly()) {
final EventDispatcher dispatcher = this.client.getDispatcher();
dispatcher.registerListener(moduleObject);
if (moduleObject == null) {
// Module could not be created -> Add to failed modules
if (!failedModules.contains(moduleName)) {
failedModules.add(moduleName);
}

LOG.info("Successfully loaded module \"{}\".", moduleName);
loadedModules.remove(moduleName);
return;
}

// Register EventListener if needed
if (!moduleAnnotation.commandOnly()) {
final EventDispatcher dispatcher = this.client.getDispatcher();
dispatcher.registerListener(moduleObject);
}

loadedModules.put(moduleName, moduleObject);
failedModules.remove(moduleName);
LOG.info("Successfully loaded module \"{}\".", moduleName);
}

/**
Expand Down Expand Up @@ -618,18 +628,27 @@ public List<String> getUnloadedModules() {
return unloadedModules;
}

/**
* Returns the list containing the names of the unloaded modules
*
* @return The list containing the names of the unloaded modules
*/
public List<String> getFailedModules() {
return failedModules;
}

/**
* Activate a module so the bot loads it
*
* @param moduleName The name of the module
* @return true if everything went fine, false if the module does not exist or is already actived
*/
public boolean activateModule(final String moduleName) {
if (!this.unloadedModules.contains(moduleName)) {

if (!unloadedModules.contains(moduleName) && !failedModules.contains(moduleName)) {
// Module either doesn't exist or is already loaded
return false;
}

LOG.info("Activating module \"{}\".", moduleName);

LOG.debug("Removing module from unloaded list");
Expand Down
67 changes: 55 additions & 12 deletions src/main/java/de/nikos410/discordbot/modules/BotSetup.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.lang.reflect.Method;
import java.util.EnumSet;
import java.util.List;
import java.util.Map;

import de.nikos410.discordbot.DiscordBot;
Expand Down Expand Up @@ -187,13 +188,16 @@ public void command_setUsername(final IMessage message, final String newUserName

@CommandSubscriber(command = "modules", help = "Alle Module anzeigen")
public void command_listModules(final IMessage message) {
final EmbedBuilder embedBuilder = new EmbedBuilder();

// List loaded modules
final StringBuilder loadedBuilder = new StringBuilder();
for (final String key : bot.getLoadedModules().keySet()) {
loadedBuilder.append(key);
loadedBuilder.append('\n');
}
final String loadedModulesString = loadedBuilder.toString().isEmpty() ? "_keine_" : loadedBuilder.toString();
embedBuilder.appendField("Aktivierte Module", loadedModulesString, true);

// List unloaded modules
final StringBuilder unloadedBuilder = new StringBuilder();
Expand All @@ -202,28 +206,51 @@ public void command_listModules(final IMessage message) {
unloadedBuilder.append('\n');
}
final String unloadedModulesString = unloadedBuilder.toString().isEmpty() ? "_keine_" : unloadedBuilder.toString();

// Build embed
final EmbedBuilder embedBuilder = new EmbedBuilder();
embedBuilder.appendField("Aktivierte Module", loadedModulesString, true);
embedBuilder.appendField("Deaktivierte Module", unloadedModulesString, true);

// Add failed modules, if present
final List<String> failedModules = bot.getFailedModules();
if (!failedModules.isEmpty()) {
final StringBuilder failedBuilder = new StringBuilder();
for (final String module : failedModules) {
failedBuilder.append(module);
failedBuilder.append('\n');
}
embedBuilder.appendField("Folgende Module konnten nicht geladen werden:", failedBuilder.toString(), true);
}

DiscordIO.sendEmbed(message.getChannel(), embedBuilder.build());
}

@CommandSubscriber(command = "loadmodule", help = "Ein Modul aktivieren", permissionLevel = PermissionLevel.ADMIN)
public void command_loadModule(final IMessage message, final String moduleName) {
final boolean result = bot.activateModule(moduleName);
final List<String> failedModules = bot.getFailedModules();

if (result) {
// Method returned true, everything went fine
if(result && failedModules.isEmpty()) {
// Method returned true and there are no failed modules, everything went fine
message.addReaction(ReactionEmoji.of("✅")); // :white_check_mark:
return;
}
else {

if(!result) {
// Method returned false, module doesn't exist or is already activated
DiscordIO.sendMessage(message.getChannel(),
String.format("Fehler! Modul `%s` ist bereits aktiviert oder existiert nicht.", moduleName));
}

if (!failedModules.isEmpty()) {
// At least one module could not be initialized
final StringBuilder failedBuilder = new StringBuilder();
failedBuilder.append("__There was a problem while reloading modules. Following modules could not be initialized:__\n");

for (String module : failedModules) {
failedBuilder.append(module);
failedBuilder.append('\n');
}

DiscordIO.sendMessage(message.getChannel(), failedBuilder.toString());
}
}

@CommandSubscriber(command = "unloadmodule", help = "Ein Modul deaktivieren", permissionLevel = PermissionLevel.ADMIN)
Expand All @@ -233,16 +260,32 @@ public void command_unloadModule(final IMessage message, final String moduleName
return;
}

final boolean result = bot.deactivateModule(moduleName);
final boolean result = bot.activateModule(moduleName);
final List<String> failedModules = bot.getFailedModules();

if (result) {
// Method returned true, everything went fine
if(result && failedModules.isEmpty()) {
// Method returned true and there are no failed modules, everything went fine
message.addReaction(ReactionEmoji.of("✅")); // :white_check_mark:
return;
}
else {
// Method returned false, module doesn't exist or is already deactivated

if(!result) {
// Method returned false, module doesn't exist or is already activated
DiscordIO.sendMessage(message.getChannel(),
String.format("Fehler! Modul `%s` ist bereits deaktiviert oder existiert nicht.", moduleName));
}

if (!failedModules.isEmpty()) {
// At least one module could not be initialized
final StringBuilder failedBuilder = new StringBuilder();
failedBuilder.append("__There was a problem while reloading modules. Following modules could not be initialized:__\n");

for (String module : failedModules) {
failedBuilder.append(module);
failedBuilder.append('\n');
}

DiscordIO.sendMessage(message.getChannel(), failedBuilder.toString());
}
}
}
7 changes: 6 additions & 1 deletion src/main/java/de/nikos410/discordbot/modules/GameStats.java
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ private List<String> findSimilarKeys (final String inputKey, final IGuild guild)
.collect(Collectors.toList());
}

private class GameFuzzyScore implements Comparable {
private static class GameFuzzyScore implements Comparable {
private final String name;
private final int score;

Expand Down Expand Up @@ -228,6 +228,11 @@ public boolean equals(Object other) {
final GameFuzzyScore otherScore = (GameFuzzyScore)other;
return otherScore.getScore() == score;
}

@Override
public int hashCode() {
return Objects.hash(name, score);
}
}

@EventSubscriber
Expand Down
Loading

0 comments on commit c574f48

Please sign in to comment.