Fixed blacklist command where database command would overlap each other
This commit is contained in:
1 parent
340376a52a
commit
29a9d1bc46
3 files changed
+73
-64
No files matched your search
@@ -16,23 +16,21 @@ public class ThreadManager {
|
||||
public static final ExecutorService executor = Executors.newSingleThreadExecutor();
|
||||
public static ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
|
||||
|
||||
|
||||
|
||||
public static void shutdownExecutor() {
|
||||
try {
|
||||
System.out.println("attempt to shutdown executor");
|
||||
System.out.println("[Executor] Shutting down...");
|
||||
executor.shutdown();
|
||||
executor.awaitTermination(5, TimeUnit.SECONDS);
|
||||
}
|
||||
catch (InterruptedException e) {
|
||||
System.err.println("tasks interrupted");
|
||||
System.err.println("[Executor] Running tasks interrupted!");
|
||||
}
|
||||
finally {
|
||||
if (!executor.isTerminated()) {
|
||||
System.err.println("cancel non-finished tasks");
|
||||
System.err.println("[Executor] Forcing shutdown of running tasks..");
|
||||
}
|
||||
executor.shutdownNow();
|
||||
System.out.println("shutdown finished");
|
||||
System.out.println("[Executor] Shutdown finished!");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -64,15 +64,12 @@ public class BlacklistCommand extends AbstractCommand {
|
||||
|
||||
try {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
((DBBlacklist) DBController.get("blacklist")).getBlacklistFromServer(event.getGuild().getId(), new Consumer<ArrayList<MBlacklist>>() {
|
||||
@Override
|
||||
public void accept(ArrayList<MBlacklist> mBlacklists) {
|
||||
for (MBlacklist bl : mBlacklists) {
|
||||
sb.append(bl.getWord()).append(" -> ").append(bl.isEnabled() ? "enabled" : "disabled").append("\n");
|
||||
}
|
||||
|
||||
t.editMessage("List of blacklisted words for this server [" + event.getGuild().getName() + "] :\n" + (mBlacklists.isEmpty() ? "No word found" : sb.toString())).queue();
|
||||
((DBBlacklist) DBController.get("blacklist")).getBlacklistFromServer(event.getGuild().getId(), mBlacklists -> {
|
||||
for (MBlacklist bl : mBlacklists) {
|
||||
sb.append(bl.getWord()).append(" -> ").append(bl.isEnabled() ? "enabled" : "disabled").append("\n");
|
||||
}
|
||||
|
||||
t.editMessage("List of blacklisted words for this server [" + event.getGuild().getName() + "] :\n" + (mBlacklists.isEmpty() ? "No word found" : sb.toString())).queue();
|
||||
});
|
||||
|
||||
} catch (RuntimeException e) {
|
||||
@@ -86,49 +83,48 @@ public class BlacklistCommand extends AbstractCommand {
|
||||
|
||||
case "add":
|
||||
|
||||
try {
|
||||
((DBBlacklist) DBController.get("blacklist")).add(new MBlacklist(word, true, event.getAuthor().getId(), event.getGuild().getId()));
|
||||
((DBBlacklist) DBController.get("blacklist")).add(new MBlacklist(word, true, event.getAuthor().getId(), event.getGuild().getId()), response -> {
|
||||
if (response) {
|
||||
event.getTextChannel().sendMessage("Added the word \"" + word + "\" to the blacklist.").queue();
|
||||
} catch (RuntimeException e) {
|
||||
} else {
|
||||
event.getTextChannel().sendMessage("Error adding \"" + word + "\" to the blacklist. It's already blacklisted!").queue();
|
||||
LogHandler.log(e);
|
||||
}
|
||||
|
||||
});
|
||||
break;
|
||||
|
||||
case "remove":
|
||||
|
||||
try {
|
||||
((DBBlacklist) DBController.get("blacklist")).delete(word, event.getGuild().getId());
|
||||
((DBBlacklist) DBController.get("blacklist")).delete(word, event.getGuild().getId(), success -> {
|
||||
if (success) {
|
||||
event.getTextChannel().sendMessage("Removed the word \"" + word + "\" from the blacklist.").queue();
|
||||
} catch (RuntimeException e) {
|
||||
LogHandler.log(e);
|
||||
} else {
|
||||
event.getTextChannel().sendMessage("Error removing \"" + word + "\" from the blacklist").queue();
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
break;
|
||||
|
||||
case "disable":
|
||||
|
||||
try {
|
||||
((DBBlacklist) DBController.get("blacklist")).disable(word, event.getGuild().getId());
|
||||
event.getTextChannel().sendMessage("Succesfully disabled the word \"" + word + "\" from the blacklist!").queue();
|
||||
} catch (NullPointerException e) {
|
||||
LogHandler.log(e);
|
||||
event.getTextChannel().sendMessage("Error disabling \"" + word + "\", it's not blacklisted!").queue();
|
||||
|
||||
((DBBlacklist) DBController.get("blacklist")).disable(word, event.getGuild().getId(), success -> {
|
||||
if (success) {
|
||||
event.getTextChannel().sendMessage("Removed the word \"" + word + "\" from the blacklist.").queue();
|
||||
} else {
|
||||
event.getTextChannel().sendMessage("Error removing \"" + word + "\" from the blacklist").queue();
|
||||
}
|
||||
});
|
||||
|
||||
break;
|
||||
|
||||
case "enable":
|
||||
|
||||
try {
|
||||
((DBBlacklist) DBController.get("blacklist")).enable(word, event.getGuild().getId());
|
||||
event.getTextChannel().sendMessage("Succesfully enabled the word \"" + word + "\" from the blacklist!").queue();
|
||||
} catch (NullPointerException e) {
|
||||
event.getTextChannel().sendMessage("Error enabling \"" + word + "\", it's not blacklisted!").queue();
|
||||
LogHandler.log(e);
|
||||
|
||||
((DBBlacklist) DBController.get("blacklist")).enable(word, event.getGuild().getId(), success -> {
|
||||
if (success) {
|
||||
event.getTextChannel().sendMessage("Removed the word \"" + word + "\" from the blacklist.").queue();
|
||||
} else {
|
||||
event.getTextChannel().sendMessage("Error removing \"" + word + "\" from the blacklist").queue();
|
||||
}
|
||||
});
|
||||
|
||||
break;
|
||||
|
||||
|
||||
@@ -27,39 +27,53 @@ public class DBBlacklist extends AbstractController {
|
||||
return database;
|
||||
}
|
||||
|
||||
public void add(MBlacklist obj) {
|
||||
if (!isAlreadyBlacklisted(obj.getWord(), obj.getServerId())) {
|
||||
database.insert("INSERT INTO blacklist VALUES(?,?,?,?)", obj.getWord(), obj.getServerId(), obj.isEnabled(), obj.getCreatorId());
|
||||
} else {
|
||||
throw new RuntimeException("Word is already blacklisted");
|
||||
}
|
||||
public void add(MBlacklist obj, Consumer<Boolean> success) {
|
||||
ThreadManager.executor.submit(() -> {
|
||||
if (!isAlreadyBlacklisted(obj.getWord(), obj.getServerId())) {
|
||||
database.insert("INSERT INTO blacklist VALUES(?,?,?,?)", obj.getWord(), obj.getServerId(), obj.isEnabled(), obj.getCreatorId());
|
||||
success.accept(true);
|
||||
} else {
|
||||
success.accept(false);
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
|
||||
}
|
||||
|
||||
public void delete(String word, String serverId) {
|
||||
if (isAlreadyBlacklisted(word, serverId)) {
|
||||
database.delete("DELETE FROM blacklist WHERE word = ? AND serverId = ?", word, serverId);
|
||||
} else {
|
||||
throw new RuntimeException("Is not blacklisted");
|
||||
}
|
||||
}
|
||||
|
||||
public void enable(String word, String serverId) {
|
||||
if (isAlreadyBlacklisted(word, serverId)) {
|
||||
database.update("UPDATE blacklist SET isEnabled = 1 WHERE word = ? AND serverId = ?", word, serverId);
|
||||
} else {
|
||||
throw new RuntimeException("Is not blacklisted");
|
||||
}
|
||||
public void delete(String word, String serverId, Consumer<Boolean> success) {
|
||||
ThreadManager.executor.submit(() -> {
|
||||
if (isAlreadyBlacklisted(word, serverId)) {
|
||||
System.out.println("true");
|
||||
database.delete("DELETE FROM blacklist WHERE word = ? AND serverId = ?", word, serverId);
|
||||
success.accept(true);
|
||||
} else {
|
||||
success.accept(false);
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
public void disable(String word, String serverId) {
|
||||
if (isAlreadyBlacklisted(word, serverId)) {
|
||||
database.update("UPDATE blacklist SET isEnabled = 0 WHERE word = ? AND serverId = ?", word, serverId);
|
||||
} else {
|
||||
throw new RuntimeException("Is not blacklisted");
|
||||
}
|
||||
public void enable(String word, String serverId, Consumer<Boolean> success) {
|
||||
ThreadManager.executor.submit(() -> {
|
||||
if (isAlreadyBlacklisted(word, serverId)) {
|
||||
database.update("UPDATE blacklist SET isEnabled = 1 WHERE word = ? AND serverId = ?", word, serverId);
|
||||
success.accept(true);
|
||||
} else {
|
||||
success.accept(false);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void disable(String word, String serverId, Consumer<Boolean> success) {
|
||||
ThreadManager.executor.submit(() -> {
|
||||
if (isAlreadyBlacklisted(word, serverId)) {
|
||||
database.update("UPDATE blacklist SET isEnabled = 0 WHERE word = ? AND serverId = ?", word, serverId);
|
||||
success.accept(true);
|
||||
} else {
|
||||
success.accept(false);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void getBlacklistForWord(String word, String serverId, Consumer<MBlacklist> success) {
|
||||
@@ -108,6 +122,7 @@ public class DBBlacklist extends AbstractController {
|
||||
|
||||
public boolean isAlreadyBlacklisted(String word, String serverId) {
|
||||
ResultSet rs = database.prepareAndExecute("SELECT COUNT(*) FROM blacklist WHERE word = ? AND serverId = ?", word, serverId);
|
||||
|
||||
try {
|
||||
if (rs.next()) {
|
||||
return rs.getInt(1) != 0;
|
||||
|
||||
Reference in new issue
Block a user