Add evidence folder integration with Nextcloud support
Implemented an "Evidence" utility to manage evidence folders in Nextcloud and updated related classes to include evidence links in Discord ban messages. Refactored logging for better error handling and added unit tests for evidence folder creation.
This commit is contained in:
@@ -195,6 +195,38 @@ public final class Config {
|
||||
}
|
||||
}
|
||||
|
||||
public static class WORDPRESS_DB {
|
||||
public static String DRIVERS = "mysql";
|
||||
public static String IP = "localhost";
|
||||
public static String PORT = "3306";
|
||||
public static String DATABASE_NAME = "wordpress";
|
||||
public static String USERNAME = "root";
|
||||
public static String PASSWORD = "root";
|
||||
|
||||
private static void database() {
|
||||
DRIVERS = getString("database.drivers", DRIVERS);
|
||||
IP = getString("database.ip", IP);
|
||||
PORT = getString("database.port", PORT);
|
||||
DATABASE_NAME = getString("database.database_name", DATABASE_NAME);
|
||||
USERNAME = getString("database.username", USERNAME);
|
||||
PASSWORD = getString("database.password", PASSWORD);
|
||||
}
|
||||
}
|
||||
|
||||
public static class NEXT_CLOUD {
|
||||
public static String ADDRESS = "drive.alttd.com";
|
||||
public static Integer PORT = 443;
|
||||
public static String USERNAME = "root";
|
||||
public static String PASSWORD = "root";
|
||||
|
||||
private static void loadNextCloud() {
|
||||
ADDRESS = getString("next_cloud.address", ADDRESS);
|
||||
PORT = getInt("next_cloud.port", PORT);
|
||||
USERNAME = getString("next_cloud.username", USERNAME);
|
||||
PASSWORD = getString("next_cloud.password", PASSWORD);
|
||||
}
|
||||
}
|
||||
|
||||
public static class MESSAGES {
|
||||
|
||||
public static String ALREADY_LINKED_ACCOUNTS = "<yellow>Your accounts are already linked. You can unlink your accounts by doing <gold>/discord unlink</gold>.</yellow>";
|
||||
|
||||
+55
-21
@@ -3,10 +3,14 @@ package com.alttd.proxydiscordlink.minecraft.listeners;
|
||||
import com.alttd.proxydiscordlink.DiscordLink;
|
||||
import com.alttd.proxydiscordlink.config.BotConfig;
|
||||
import com.alttd.proxydiscordlink.objects.DiscordLinkPlayer;
|
||||
import com.alttd.proxydiscordlink.util.Evidence;
|
||||
import com.alttd.proxydiscordlink.util.Utilities;
|
||||
import com.velocitypowered.api.proxy.Player;
|
||||
import litebans.api.Entry;
|
||||
import litebans.api.Events;
|
||||
import net.dv8tion.jda.api.EmbedBuilder;
|
||||
import net.dv8tion.jda.api.entities.MessageEmbed;
|
||||
import net.luckperms.api.model.user.User;
|
||||
|
||||
import java.awt.*;
|
||||
import java.util.Optional;
|
||||
@@ -32,39 +36,69 @@ public class LiteBansBanListener {
|
||||
}
|
||||
|
||||
private void onBan(Entry entry) {
|
||||
if (!entry.isPermanent())
|
||||
if (entry == null)
|
||||
return;
|
||||
|
||||
String stringUuid = entry.getUuid();
|
||||
if (stringUuid == null)
|
||||
return;
|
||||
UUID uuid = UUID.fromString(stringUuid);
|
||||
|
||||
Optional<Player> player = DiscordLink.getPlugin().getProxy().getPlayer(uuid);
|
||||
String username;
|
||||
|
||||
if (player.isPresent()) {
|
||||
username = player.get().getUsername();
|
||||
} else {
|
||||
User user = Utilities.getLuckPerms().getUserManager().getUser(uuid);
|
||||
if (user == null) {
|
||||
username = uuid.toString();
|
||||
} else {
|
||||
username = user.getUsername();
|
||||
}
|
||||
}
|
||||
|
||||
Evidence evidence = new Evidence();
|
||||
evidence.getNewEvidenceFolder(username).thenAcceptAsync(optionalUrl -> {
|
||||
Optional<MessageEmbed.Field> field = banDiscordUserIfExists(entry, uuid);
|
||||
|
||||
EmbedBuilder banEvidence = new EmbedBuilder()
|
||||
.setColor(entry.isPermanent() ? Color.RED : Color.ORANGE)
|
||||
.setAuthor(username, null, "https://crafatar.com/avatars/" + stringUuid + "?overlay")
|
||||
.addField("Banned by", entry.getExecutorName() == null ? "unknown" : entry.getExecutorName(), true)
|
||||
.addField("Reason", entry.getReason() == null ? "unknown" : entry.getReason(), true)
|
||||
.setTitle("Auto Discord ban");
|
||||
|
||||
if (optionalUrl.isPresent()) {
|
||||
banEvidence.addField("Evidence", optionalUrl.get(), false);
|
||||
} else {
|
||||
banEvidence.addField("Evidence", "Failed to get url, please make the folder yourself and reply to this post with the link.", false);
|
||||
}
|
||||
|
||||
field.ifPresent(banEvidence::addField);
|
||||
|
||||
DiscordLink.getPlugin().getBot().sendEmbedToDiscord(BotConfig.DISCORD.EVIDENCE_CHANNEL_ID, banEvidence, -1);
|
||||
});
|
||||
}
|
||||
|
||||
private Optional<MessageEmbed.Field> banDiscordUserIfExists(Entry entry, UUID uuid) {
|
||||
if (!entry.isPermanent())
|
||||
return Optional.empty();
|
||||
|
||||
DiscordLinkPlayer discordLinkPlayer = DiscordLinkPlayer.getDiscordLinkPlayer(uuid);
|
||||
if (discordLinkPlayer == null || !discordLinkPlayer.isActive())
|
||||
return;
|
||||
return Optional.empty();
|
||||
discordLinkPlayer.setActive(false);
|
||||
|
||||
DiscordLink.getPlugin().getBot().discordBan(BotConfig.DISCORD.GUILD_ID, discordLinkPlayer.getUserId(), "Auto ban due to Minecraft ban");
|
||||
Optional<Player> player = DiscordLink.getPlugin().getProxy().getPlayer(uuid);
|
||||
|
||||
String username = discordLinkPlayer.getUsername();
|
||||
if (player.isPresent())
|
||||
username = player.get().getUsername();
|
||||
|
||||
DiscordLink.getPlugin().getBot().sendEmbedToDiscord(BotConfig.DISCORD.EVIDENCE_CHANNEL_ID,
|
||||
new EmbedBuilder()
|
||||
.setColor(Color.RED)
|
||||
.setAuthor(username, null, "https://crafatar.com/avatars/" + stringUuid + "?overlay")
|
||||
.setTitle("Auto Discord ban")
|
||||
.addField("Ban info",
|
||||
"**Discord username**: `" + discordLinkPlayer.getDiscordUsername() + "`" +
|
||||
"\n**Discord id**: `" + discordLinkPlayer.getUserId() + "`" +
|
||||
"\n**UUID**: `" + stringUuid + "`" +
|
||||
"\n**Banned by**: `" + entry.getExecutorName() + "`" +
|
||||
"\n**For**: ```" + (entry.getReason().length() < 800 ? entry.getReason() : entry.getReason().substring(0, 797) + "...") + "```",
|
||||
false),
|
||||
-1);
|
||||
return Optional.of(new MessageEmbed.Field("Auto Discord ban",
|
||||
"**Discord username**: `" + discordLinkPlayer.getDiscordUsername() + "`" +
|
||||
"\n**Discord id**: `" + discordLinkPlayer.getUserId() + "`" +
|
||||
"\n**UUID**: `" + uuid.toString() + "`" +
|
||||
"\n**Banned by**: `" + entry.getExecutorName() + "`" +
|
||||
"\n**For**: ```" + (entry.getReason().length() < 800 ? entry.getReason() : entry.getReason().substring(0, 797) + "...") + "```",
|
||||
false)
|
||||
);
|
||||
}
|
||||
|
||||
private void onUnBan(Entry entry) {
|
||||
|
||||
@@ -21,4 +21,8 @@ public class ALogger {
|
||||
public static void error(String message) {
|
||||
logger.error(message);
|
||||
}
|
||||
|
||||
public static void error(String message, Throwable t) {
|
||||
logger.error(message, t);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
package com.alttd.proxydiscordlink.util;
|
||||
|
||||
import com.alttd.proxydiscordlink.config.Config;
|
||||
import org.aarboard.nextcloud.api.NextcloudConnector;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
public class Evidence {
|
||||
|
||||
public CompletableFuture<Optional<String>> getNewEvidenceFolder(String username) {
|
||||
return CompletableFuture.supplyAsync(() -> {
|
||||
NextcloudConnector nextcloudConnector = new NextcloudConnector(
|
||||
Config.NEXT_CLOUD.ADDRESS,
|
||||
true,
|
||||
Config.NEXT_CLOUD.PORT,
|
||||
Config.NEXT_CLOUD.USERNAME,
|
||||
Config.NEXT_CLOUD.PASSWORD
|
||||
);
|
||||
String evidenceFilePath = String.format("/Altitude/Altitude Moderation Tools/Evidence/%s", username);
|
||||
if (!nextcloudConnector.folderExists(evidenceFilePath)) {
|
||||
nextcloudConnector.createFolder(evidenceFilePath);
|
||||
}
|
||||
nextcloudConnector.createFolder(evidenceFilePath + "/" + getCurrentDate());
|
||||
String id;
|
||||
try {
|
||||
id = nextcloudConnector.getProperties(evidenceFilePath + "/" + getCurrentDate(), true).getId();
|
||||
} catch (IOException e) {
|
||||
ALogger.error("Failed to get share link for Next Cloud folder", e);
|
||||
return Optional.empty();
|
||||
}
|
||||
return Optional.of("https://drive.alttd.com/f/" + id);
|
||||
});
|
||||
}
|
||||
|
||||
private static String getCurrentDate() {
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd_HH_mm");
|
||||
return now.format(formatter);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.alttd.proxydiscordlink.util;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class EvidenceTest {
|
||||
|
||||
@Test
|
||||
void shouldReturnEvidenceUrl() {
|
||||
Evidence evidence = new Evidence();
|
||||
|
||||
evidence.getNewEvidenceFolder("akastijn").thenAccept(evidenceUrl -> {
|
||||
assertTrue(evidenceUrl.isPresent());
|
||||
System.out.println(evidenceUrl.get());
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user