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

Commit c574f48

Browse files
committed
Release 2.5.0: LastFm
2 parents b926bfd + 49700af commit c574f48

File tree

9 files changed

+624
-30
lines changed

9 files changed

+624
-30
lines changed

build.gradle

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,31 @@
11
buildscript {
22
repositories {
33
jcenter()
4+
maven {
5+
url "https://plugins.gradle.org/m2/"
6+
}
47
}
58
dependencies {
69
classpath 'com.github.jengelman.gradle.plugins:shadow:4.0.3'
10+
classpath "gradle.plugin.com.github.spotbugs:spotbugs-gradle-plugin:1.6.6"
711
}
812
}
913

1014
apply plugin: 'java'
1115
apply plugin: 'application'
1216
apply plugin: 'com.github.johnrengelman.shadow'
17+
apply plugin: "com.github.spotbugs"
18+
19+
spotbugs {
20+
ignoreFailures = true
21+
}
1322

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

19-
version = 2.4
28+
version = '2.5.0'
2029
sourceCompatibility = 1.8
2130
mainClassName = 'de.nikos410.discordbot.DiscordBot'
2231

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

3949
repositories {
4050
jcenter()
4151
mavenCentral()
42-
}
52+
}

src/main/java/de/nikos410/discordbot/DiscordBot.java

+28-9
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public class DiscordBot {
4545

4646
private final List<String> unloadedModules = new ArrayList<>();
4747
private final Map<String, Object> loadedModules = new HashMap<>();
48+
private final List<String> failedModules = new ArrayList<>();
4849

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

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

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

203-
LOG.info("Successfully loaded module \"{}\".", moduleName);
202+
loadedModules.remove(moduleName);
203+
return;
204+
}
205+
206+
// Register EventListener if needed
207+
if (!moduleAnnotation.commandOnly()) {
208+
final EventDispatcher dispatcher = this.client.getDispatcher();
209+
dispatcher.registerListener(moduleObject);
204210
}
211+
212+
loadedModules.put(moduleName, moduleObject);
213+
failedModules.remove(moduleName);
214+
LOG.info("Successfully loaded module \"{}\".", moduleName);
205215
}
206216

207217
/**
@@ -618,18 +628,27 @@ public List<String> getUnloadedModules() {
618628
return unloadedModules;
619629
}
620630

631+
/**
632+
* Returns the list containing the names of the unloaded modules
633+
*
634+
* @return The list containing the names of the unloaded modules
635+
*/
636+
public List<String> getFailedModules() {
637+
return failedModules;
638+
}
639+
621640
/**
622641
* Activate a module so the bot loads it
623642
*
624643
* @param moduleName The name of the module
625644
* @return true if everything went fine, false if the module does not exist or is already actived
626645
*/
627646
public boolean activateModule(final String moduleName) {
628-
if (!this.unloadedModules.contains(moduleName)) {
647+
648+
if (!unloadedModules.contains(moduleName) && !failedModules.contains(moduleName)) {
629649
// Module either doesn't exist or is already loaded
630650
return false;
631651
}
632-
633652
LOG.info("Activating module \"{}\".", moduleName);
634653

635654
LOG.debug("Removing module from unloaded list");

src/main/java/de/nikos410/discordbot/modules/BotSetup.java

+55-12
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.lang.reflect.Method;
44
import java.util.EnumSet;
5+
import java.util.List;
56
import java.util.Map;
67

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

188189
@CommandSubscriber(command = "modules", help = "Alle Module anzeigen")
189190
public void command_listModules(final IMessage message) {
191+
final EmbedBuilder embedBuilder = new EmbedBuilder();
192+
190193
// List loaded modules
191194
final StringBuilder loadedBuilder = new StringBuilder();
192195
for (final String key : bot.getLoadedModules().keySet()) {
193196
loadedBuilder.append(key);
194197
loadedBuilder.append('\n');
195198
}
196199
final String loadedModulesString = loadedBuilder.toString().isEmpty() ? "_keine_" : loadedBuilder.toString();
200+
embedBuilder.appendField("Aktivierte Module", loadedModulesString, true);
197201

198202
// List unloaded modules
199203
final StringBuilder unloadedBuilder = new StringBuilder();
@@ -202,28 +206,51 @@ public void command_listModules(final IMessage message) {
202206
unloadedBuilder.append('\n');
203207
}
204208
final String unloadedModulesString = unloadedBuilder.toString().isEmpty() ? "_keine_" : unloadedBuilder.toString();
205-
206-
// Build embed
207-
final EmbedBuilder embedBuilder = new EmbedBuilder();
208-
embedBuilder.appendField("Aktivierte Module", loadedModulesString, true);
209209
embedBuilder.appendField("Deaktivierte Module", unloadedModulesString, true);
210210

211+
// Add failed modules, if present
212+
final List<String> failedModules = bot.getFailedModules();
213+
if (!failedModules.isEmpty()) {
214+
final StringBuilder failedBuilder = new StringBuilder();
215+
for (final String module : failedModules) {
216+
failedBuilder.append(module);
217+
failedBuilder.append('\n');
218+
}
219+
embedBuilder.appendField("Folgende Module konnten nicht geladen werden:", failedBuilder.toString(), true);
220+
}
221+
211222
DiscordIO.sendEmbed(message.getChannel(), embedBuilder.build());
212223
}
213224

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

218-
if (result) {
219-
// Method returned true, everything went fine
230+
if(result && failedModules.isEmpty()) {
231+
// Method returned true and there are no failed modules, everything went fine
220232
message.addReaction(ReactionEmoji.of("✅")); // :white_check_mark:
233+
return;
221234
}
222-
else {
235+
236+
if(!result) {
223237
// Method returned false, module doesn't exist or is already activated
224238
DiscordIO.sendMessage(message.getChannel(),
225239
String.format("Fehler! Modul `%s` ist bereits aktiviert oder existiert nicht.", moduleName));
226240
}
241+
242+
if (!failedModules.isEmpty()) {
243+
// At least one module could not be initialized
244+
final StringBuilder failedBuilder = new StringBuilder();
245+
failedBuilder.append("__There was a problem while reloading modules. Following modules could not be initialized:__\n");
246+
247+
for (String module : failedModules) {
248+
failedBuilder.append(module);
249+
failedBuilder.append('\n');
250+
}
251+
252+
DiscordIO.sendMessage(message.getChannel(), failedBuilder.toString());
253+
}
227254
}
228255

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

236-
final boolean result = bot.deactivateModule(moduleName);
263+
final boolean result = bot.activateModule(moduleName);
264+
final List<String> failedModules = bot.getFailedModules();
237265

238-
if (result) {
239-
// Method returned true, everything went fine
266+
if(result && failedModules.isEmpty()) {
267+
// Method returned true and there are no failed modules, everything went fine
240268
message.addReaction(ReactionEmoji.of("✅")); // :white_check_mark:
269+
return;
241270
}
242-
else {
243-
// Method returned false, module doesn't exist or is already deactivated
271+
272+
if(!result) {
273+
// Method returned false, module doesn't exist or is already activated
244274
DiscordIO.sendMessage(message.getChannel(),
245275
String.format("Fehler! Modul `%s` ist bereits deaktiviert oder existiert nicht.", moduleName));
246276
}
277+
278+
if (!failedModules.isEmpty()) {
279+
// At least one module could not be initialized
280+
final StringBuilder failedBuilder = new StringBuilder();
281+
failedBuilder.append("__There was a problem while reloading modules. Following modules could not be initialized:__\n");
282+
283+
for (String module : failedModules) {
284+
failedBuilder.append(module);
285+
failedBuilder.append('\n');
286+
}
287+
288+
DiscordIO.sendMessage(message.getChannel(), failedBuilder.toString());
289+
}
247290
}
248291
}

src/main/java/de/nikos410/discordbot/modules/GameStats.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ private List<String> findSimilarKeys (final String inputKey, final IGuild guild)
196196
.collect(Collectors.toList());
197197
}
198198

199-
private class GameFuzzyScore implements Comparable {
199+
private static class GameFuzzyScore implements Comparable {
200200
private final String name;
201201
private final int score;
202202

@@ -228,6 +228,11 @@ public boolean equals(Object other) {
228228
final GameFuzzyScore otherScore = (GameFuzzyScore)other;
229229
return otherScore.getScore() == score;
230230
}
231+
232+
@Override
233+
public int hashCode() {
234+
return Objects.hash(name, score);
235+
}
231236
}
232237

233238
@EventSubscriber

0 commit comments

Comments
 (0)