feat: ip filter reason
This commit is contained in:
@@ -1 +1 @@
|
||||
1500
|
||||
1502
|
||||
@@ -14,6 +14,7 @@ import net.kyori.adventure.text.format.NamedTextColor;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class IPFilterCommand extends Command {
|
||||
public IPFilterCommand() {
|
||||
@@ -21,7 +22,7 @@ public class IPFilterCommand extends Command {
|
||||
"ipfilter",
|
||||
"Filters IPs",
|
||||
new String[] {
|
||||
"add <ip>",
|
||||
"add <ip> [reason]",
|
||||
"remove <index>",
|
||||
"clear",
|
||||
"list"
|
||||
@@ -41,9 +42,10 @@ public class IPFilterCommand extends Command {
|
||||
|
||||
switch (action) {
|
||||
case "add" -> {
|
||||
final String ip = context.getString(true, true);
|
||||
final String ip = context.getString(false, true);
|
||||
final String reason = context.getString(true, false);
|
||||
|
||||
if (IPFilterPlugin.localList.contains(ip)) {
|
||||
if (IPFilterPlugin.localList.containsKey(ip)) {
|
||||
throw new CommandException(
|
||||
Component.translatable(
|
||||
"The IP %s is already in the filters",
|
||||
@@ -52,18 +54,27 @@ public class IPFilterCommand extends Command {
|
||||
);
|
||||
}
|
||||
|
||||
DatabasePlugin.executorService.submit(() -> bot.ipFilter.add(ip));
|
||||
return Component.translatable(
|
||||
"Added %s to the filters",
|
||||
Component.text(ip).color(ColorUtilities.getColorByString(bot.config.colorPalette.username))
|
||||
).color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor));
|
||||
DatabasePlugin.executorService.submit(() -> bot.ipFilter.add(ip, reason));
|
||||
|
||||
if (reason.isEmpty()) {
|
||||
return Component.translatable(
|
||||
"Added %s to the filters",
|
||||
Component.text(ip).color(ColorUtilities.getColorByString(bot.config.colorPalette.number))
|
||||
).color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor));
|
||||
} else {
|
||||
return Component.translatable(
|
||||
"Added %s to the filters with reason %s",
|
||||
Component.text(ip).color(ColorUtilities.getColorByString(bot.config.colorPalette.number)),
|
||||
Component.text(reason).color(ColorUtilities.getColorByString(bot.config.colorPalette.string))
|
||||
).color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor));
|
||||
}
|
||||
}
|
||||
case "remove" -> {
|
||||
context.checkOverloadArgs(2);
|
||||
|
||||
final int index = context.getInteger(true);
|
||||
|
||||
final String targetIP = IPFilterPlugin.localList.get(index);
|
||||
final String targetIP = new ArrayList<>(IPFilterPlugin.localList.keySet()).get(index);
|
||||
|
||||
if (targetIP == null) throw new CommandException(Component.text("Invalid index"));
|
||||
|
||||
@@ -71,7 +82,7 @@ public class IPFilterCommand extends Command {
|
||||
|
||||
return Component.translatable(
|
||||
"Removed %s from the filters",
|
||||
Component.text(targetIP).color(ColorUtilities.getColorByString(bot.config.colorPalette.username))
|
||||
Component.text(targetIP).color(ColorUtilities.getColorByString(bot.config.colorPalette.number))
|
||||
).color(ColorUtilities.getColorByString(bot.config.colorPalette.defaultColor));
|
||||
}
|
||||
case "clear" -> {
|
||||
@@ -86,12 +97,30 @@ public class IPFilterCommand extends Command {
|
||||
final List<Component> filtersComponents = new ArrayList<>();
|
||||
|
||||
int index = 0;
|
||||
for (String ip : IPFilterPlugin.localList) {
|
||||
for (Map.Entry<String, String> entry : IPFilterPlugin.localList.entrySet()) {
|
||||
final String ip = entry.getKey();
|
||||
final String reason = entry.getValue();
|
||||
|
||||
Component reasonComponent = Component.empty().color(NamedTextColor.DARK_GRAY);
|
||||
|
||||
if (!reason.isEmpty()) {
|
||||
reasonComponent = reasonComponent
|
||||
.append(Component.text("("))
|
||||
.append(Component.text("reason: ").color(NamedTextColor.GRAY))
|
||||
.append(
|
||||
Component
|
||||
.text(reason)
|
||||
.color(ColorUtilities.getColorByString(bot.config.colorPalette.string))
|
||||
)
|
||||
.append(Component.text(")"));
|
||||
}
|
||||
|
||||
filtersComponents.add(
|
||||
Component.translatable(
|
||||
"%s › %s",
|
||||
"%s › %s %s",
|
||||
Component.text(index).color(ColorUtilities.getColorByString(bot.config.colorPalette.number)),
|
||||
Component.text(ip).color(ColorUtilities.getColorByString(bot.config.colorPalette.username))
|
||||
Component.text(ip).color(ColorUtilities.getColorByString(bot.config.colorPalette.username)),
|
||||
reasonComponent
|
||||
).color(NamedTextColor.DARK_GRAY)
|
||||
);
|
||||
|
||||
|
||||
@@ -8,19 +8,19 @@ import me.chayapak1.chomens_bot.util.LoggerUtilities;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class IPFilterPlugin extends PlayersPlugin.Listener {
|
||||
private static final String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS ipFilters (ip VARCHAR(255) PRIMARY KEY);";
|
||||
private static final String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS ipFilters (ip VARCHAR(255) PRIMARY KEY, reason VARCHAR(255));";
|
||||
private static final String LIST_FILTERS = "SELECT * FROM ipFilters;";
|
||||
private static final String INSERT_FILTER = "INSERT INTO ipFilters (ip) VALUES (?);";
|
||||
private static final String INSERT_FILTER = "INSERT INTO ipFilters (ip, reason) VALUES (?, ?);";
|
||||
private static final String REMOVE_FILTER = "DELETE FROM ipFilters WHERE ip = ?;";
|
||||
private static final String CLEAR_FILTER = "DELETE FROM ipFilters;";
|
||||
|
||||
public static List<String> localList = new ArrayList<>();
|
||||
public static Map<String, String> localList = new LinkedHashMap<>();
|
||||
|
||||
static {
|
||||
if (Main.database != null) {
|
||||
@@ -78,13 +78,15 @@ public class IPFilterPlugin extends PlayersPlugin.Listener {
|
||||
});
|
||||
}
|
||||
|
||||
public static List<String> list () {
|
||||
final List<String> output = new ArrayList<>();
|
||||
public static Map<String, String> list () {
|
||||
final Map<String, String> output = new LinkedHashMap<>();
|
||||
|
||||
try (ResultSet result = Main.database.query(LIST_FILTERS)) {
|
||||
if (result == null) return output;
|
||||
|
||||
while (result.next()) output.add(result.getString("ip"));
|
||||
while (result.next()) {
|
||||
output.put(result.getString("ip"), result.getString("reason"));
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
LoggerUtilities.error(e);
|
||||
}
|
||||
@@ -94,11 +96,12 @@ public class IPFilterPlugin extends PlayersPlugin.Listener {
|
||||
return output;
|
||||
}
|
||||
|
||||
public void add (String ip) {
|
||||
public void add (String ip, String reason) {
|
||||
try {
|
||||
final PreparedStatement statement = bot.database.connection.prepareStatement(INSERT_FILTER);
|
||||
|
||||
statement.setString(1, ip);
|
||||
statement.setString(2, reason);
|
||||
|
||||
statement.executeUpdate();
|
||||
|
||||
@@ -143,12 +146,15 @@ public class IPFilterPlugin extends PlayersPlugin.Listener {
|
||||
}
|
||||
|
||||
private void handleFilterManager (String ip, PlayerEntry entry) {
|
||||
for (String eachIP : localList) {
|
||||
for (Map.Entry<String, String> ipEntry : localList.entrySet()) {
|
||||
final String eachIP = ipEntry.getKey();
|
||||
final String reason = ipEntry.getValue();
|
||||
|
||||
if (!eachIP.equals(ip)) continue;
|
||||
|
||||
if (entry.profile.equals(bot.profile)) continue;
|
||||
|
||||
bot.filterManager.add(entry, "");
|
||||
bot.filterManager.add(entry, reason);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user