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