Refactor logging and enhance bot features
Introduced detailed logging throughout the bot for better debugging and monitoring. Improved evidence folder handling in the ban listener and added error resilience. Added a new AnnouncementListener and implemented changes for command manager initialization. Disabled some obsolete or unused functionalities pending further review.
This commit is contained in:
parent
b54b3f9eaf
commit
baf88ed43e
|
|
@ -41,8 +41,7 @@ public class DiscordLink {
|
||||||
private Bot bot;
|
private Bot bot;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public DiscordLink(ProxyServer proxyServer, Logger proxyLogger, @DataDirectory Path proxydataDirectory)
|
public DiscordLink(ProxyServer proxyServer, Logger proxyLogger, @DataDirectory Path proxydataDirectory) {
|
||||||
{
|
|
||||||
plugin = this;
|
plugin = this;
|
||||||
server = proxyServer;
|
server = proxyServer;
|
||||||
logger = proxyLogger;
|
logger = proxyLogger;
|
||||||
|
|
@ -71,6 +70,12 @@ public class DiscordLink {
|
||||||
loadEvents();
|
loadEvents();
|
||||||
loadBot();
|
loadBot();
|
||||||
new LiteBansBanListener().registerEvents();
|
new LiteBansBanListener().registerEvents();
|
||||||
|
// try {
|
||||||
|
// WordPressDatabaseConnection.initialize();
|
||||||
|
// ALogger.error("*** Could not connect to the wordpress database. ***");
|
||||||
|
// } catch (SQLException e) {
|
||||||
|
// throw new RuntimeException(e);
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
public void reloadConfig() {
|
public void reloadConfig() {
|
||||||
|
|
@ -94,7 +99,6 @@ public class DiscordLink {
|
||||||
bot.connect();
|
bot.connect();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public File getDataDirectory() {
|
public File getDataDirectory() {
|
||||||
return dataDirectory.toFile();
|
return dataDirectory.toFile();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
package com.alttd.proxydiscordlink;
|
package com.alttd.proxydiscordlink;
|
||||||
|
|
||||||
import com.alttd.proxydiscordlink.bot.commandManager.CommandManager;
|
import com.alttd.proxydiscordlink.bot.commandManager.CommandManager;
|
||||||
|
import com.alttd.proxydiscordlink.util.ALogger;
|
||||||
import net.dv8tion.jda.api.JDA;
|
import net.dv8tion.jda.api.JDA;
|
||||||
import net.dv8tion.jda.api.events.session.ReadyEvent;
|
import net.dv8tion.jda.api.events.session.ReadyEvent;
|
||||||
import net.dv8tion.jda.api.hooks.ListenerAdapter;
|
import net.dv8tion.jda.api.hooks.ListenerAdapter;
|
||||||
|
|
@ -16,8 +17,9 @@ public class JDAListener extends ListenerAdapter {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onReady(@NotNull ReadyEvent event) {
|
public void onReady(@NotNull ReadyEvent event) {
|
||||||
CommandManager commandManager = new CommandManager(jda);
|
// ALogger.info("JDA ready, loading command manager");
|
||||||
jda.addEventListener(commandManager);
|
// CommandManager commandManager = new CommandManager(jda);
|
||||||
|
// jda.addEventListener(commandManager);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ package com.alttd.proxydiscordlink.bot;
|
||||||
|
|
||||||
import com.alttd.proxydiscordlink.JDAListener;
|
import com.alttd.proxydiscordlink.JDAListener;
|
||||||
import com.alttd.proxydiscordlink.DiscordLink;
|
import com.alttd.proxydiscordlink.DiscordLink;
|
||||||
|
import com.alttd.proxydiscordlink.bot.commandManager.CommandManager;
|
||||||
import com.alttd.proxydiscordlink.bot.listeners.DiscordRoleListener;
|
import com.alttd.proxydiscordlink.bot.listeners.DiscordRoleListener;
|
||||||
import com.alttd.proxydiscordlink.bot.tasks.CheckLinkSync;
|
import com.alttd.proxydiscordlink.bot.tasks.CheckLinkSync;
|
||||||
import com.alttd.proxydiscordlink.config.BotConfig;
|
import com.alttd.proxydiscordlink.config.BotConfig;
|
||||||
|
|
@ -16,7 +17,6 @@ import net.dv8tion.jda.api.entities.Role;
|
||||||
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
|
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
|
||||||
import net.dv8tion.jda.api.exceptions.HierarchyException;
|
import net.dv8tion.jda.api.exceptions.HierarchyException;
|
||||||
import net.dv8tion.jda.api.exceptions.InsufficientPermissionException;
|
import net.dv8tion.jda.api.exceptions.InsufficientPermissionException;
|
||||||
import net.dv8tion.jda.api.hooks.ListenerAdapter;
|
|
||||||
import net.dv8tion.jda.api.requests.GatewayIntent;
|
import net.dv8tion.jda.api.requests.GatewayIntent;
|
||||||
import net.dv8tion.jda.api.utils.MemberCachePolicy;
|
import net.dv8tion.jda.api.utils.MemberCachePolicy;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
@ -31,6 +31,7 @@ public class Bot {
|
||||||
public void connect() {
|
public void connect() {
|
||||||
disconnect();
|
disconnect();
|
||||||
try {
|
try {
|
||||||
|
ALogger.info("Creating bot instance");
|
||||||
jda = JDABuilder
|
jda = JDABuilder
|
||||||
.createDefault(BotConfig.DISCORD.BOT_TOKEN)
|
.createDefault(BotConfig.DISCORD.BOT_TOKEN)
|
||||||
.setMemberCachePolicy(MemberCachePolicy.ALL)
|
.setMemberCachePolicy(MemberCachePolicy.ALL)
|
||||||
|
|
@ -38,13 +39,16 @@ public class Bot {
|
||||||
.build();
|
.build();
|
||||||
jda.setAutoReconnect(true);
|
jda.setAutoReconnect(true);
|
||||||
jda.awaitReady();
|
jda.awaitReady();
|
||||||
|
ALogger.info("JDA ready");
|
||||||
jda.addEventListener(
|
jda.addEventListener(
|
||||||
new DiscordRoleListener(),
|
new DiscordRoleListener()/*,
|
||||||
new JDAListener(jda));
|
new JDAListener(jda)*/);
|
||||||
DiscordLink.getPlugin().getProxy().getScheduler().buildTask(DiscordLink.getPlugin(), new CheckLinkSync())
|
DiscordLink.getPlugin().getProxy().getScheduler().buildTask(DiscordLink.getPlugin(), new CheckLinkSync())
|
||||||
.delay(120, TimeUnit.SECONDS)
|
.delay(120, TimeUnit.SECONDS)
|
||||||
.repeat(12, TimeUnit.HOURS)
|
.repeat(12, TimeUnit.HOURS)
|
||||||
.schedule();
|
.schedule();
|
||||||
|
CommandManager commandManager = new CommandManager(jda);
|
||||||
|
jda.addEventListener(commandManager);
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -37,6 +37,7 @@ public class CommandManager extends ListenerAdapter {
|
||||||
.filter(discordCommand -> discordCommand.getName().equalsIgnoreCase(commandName))
|
.filter(discordCommand -> discordCommand.getName().equalsIgnoreCase(commandName))
|
||||||
.findFirst();
|
.findFirst();
|
||||||
if (first.isEmpty()) {
|
if (first.isEmpty()) {
|
||||||
|
ALogger.info(String.format("The command %s was used, but it's not on this plugin", commandName));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
first.get().execute(event);
|
first.get().execute(event);
|
||||||
|
|
|
||||||
|
|
@ -90,18 +90,19 @@ public class CommandLink extends DiscordCommand {
|
||||||
Utilities.commandErrAutoRem("Unable to find guild", event);
|
Utilities.commandErrAutoRem("Unable to find guild", event);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (player != null || user != null)
|
if (player != null || user != null) {
|
||||||
DiscordLink.getPlugin().getBot().changeNick(
|
DiscordLink.getPlugin().getBot().changeNick(
|
||||||
guild.getIdLong(),
|
guild.getIdLong(),
|
||||||
member.getIdLong(),
|
member.getIdLong(),
|
||||||
player == null ?
|
player == null ?
|
||||||
user.getUsername() :
|
user.getUsername() :
|
||||||
player.getUsername());
|
player.getUsername());
|
||||||
else
|
} else {
|
||||||
DiscordLink.getPlugin().getBot().changeNick(
|
DiscordLink.getPlugin().getBot().changeNick(
|
||||||
guild.getIdLong(),
|
guild.getIdLong(),
|
||||||
member.getIdLong(),
|
member.getIdLong(),
|
||||||
discordLinkPlayer.getUsername());
|
discordLinkPlayer.getUsername());
|
||||||
|
}
|
||||||
|
|
||||||
event.replyEmbeds(Utilities.genericSuccessEmbed("Success","You have successfully linked " +
|
event.replyEmbeds(Utilities.genericSuccessEmbed("Success","You have successfully linked " +
|
||||||
discordLinkPlayer.getUsername() + " with " +
|
discordLinkPlayer.getUsername() + " with " +
|
||||||
|
|
|
||||||
|
|
@ -34,7 +34,7 @@ public class CheckLinkSync implements Runnable {
|
||||||
HashSet<Long> notInDbIds = membersIdSet.stream().filter(id -> !dbIdSet.contains(id)).collect(Collectors.toCollection(HashSet::new));
|
HashSet<Long> notInDbIds = membersIdSet.stream().filter(id -> !dbIdSet.contains(id)).collect(Collectors.toCollection(HashSet::new));
|
||||||
|
|
||||||
fixNotInDb(members, notInDbIds);
|
fixNotInDb(members, notInDbIds);
|
||||||
fixNoLinkRole(members, noRoleIds);
|
// fixNoLinkRole(members, noRoleIds); //TODO remove this and find another way to do this cus this only finds cached members which most ppl aren't
|
||||||
}
|
}
|
||||||
|
|
||||||
private void fixNotInDb(List<Member> members, Set<Long> notInDbIds) {
|
private void fixNotInDb(List<Member> members, Set<Long> notInDbIds) {
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ import ninja.leaping.configurate.ConfigurationNode;
|
||||||
import ninja.leaping.configurate.ConfigurationOptions;
|
import ninja.leaping.configurate.ConfigurationOptions;
|
||||||
import ninja.leaping.configurate.objectmapping.ObjectMappingException;
|
import ninja.leaping.configurate.objectmapping.ObjectMappingException;
|
||||||
import ninja.leaping.configurate.yaml.YAMLConfigurationLoader;
|
import ninja.leaping.configurate.yaml.YAMLConfigurationLoader;
|
||||||
|
import org.aarboard.nextcloud.api.utils.WebdavInputStream;
|
||||||
import org.yaml.snakeyaml.DumperOptions;
|
import org.yaml.snakeyaml.DumperOptions;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
|
@ -174,7 +175,9 @@ public final class Config {
|
||||||
@SuppressWarnings("unused")
|
@SuppressWarnings("unused")
|
||||||
private static void loadSubclasses() {
|
private static void loadSubclasses() {
|
||||||
DB.database();
|
DB.database();
|
||||||
|
WORDPRESS_DB.database();
|
||||||
MESSAGES.loadMessages();
|
MESSAGES.loadMessages();
|
||||||
|
NEXT_CLOUD.loadNextCloud();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class DB {
|
public static class DB {
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ package com.alttd.proxydiscordlink.minecraft.listeners;
|
||||||
import com.alttd.proxydiscordlink.DiscordLink;
|
import com.alttd.proxydiscordlink.DiscordLink;
|
||||||
import com.alttd.proxydiscordlink.config.BotConfig;
|
import com.alttd.proxydiscordlink.config.BotConfig;
|
||||||
import com.alttd.proxydiscordlink.objects.DiscordLinkPlayer;
|
import com.alttd.proxydiscordlink.objects.DiscordLinkPlayer;
|
||||||
|
import com.alttd.proxydiscordlink.util.ALogger;
|
||||||
import com.alttd.proxydiscordlink.util.Evidence;
|
import com.alttd.proxydiscordlink.util.Evidence;
|
||||||
import com.alttd.proxydiscordlink.util.Utilities;
|
import com.alttd.proxydiscordlink.util.Utilities;
|
||||||
import com.velocitypowered.api.proxy.Player;
|
import com.velocitypowered.api.proxy.Player;
|
||||||
|
|
@ -37,11 +38,16 @@ public class LiteBansBanListener {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void onBan(Entry entry) {
|
private void onBan(Entry entry) {
|
||||||
if (entry == null)
|
ALogger.info("Ban detected, making evidence...");
|
||||||
|
if (entry == null) {
|
||||||
|
ALogger.warn("Failed to find ban entry, no evidence made");
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
String stringUuid = entry.getUuid();
|
String stringUuid = entry.getUuid();
|
||||||
if (stringUuid == null)
|
if (stringUuid == null) {
|
||||||
|
ALogger.warn("Failed to find uuid in entry, no evidence made");
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
UUID uuid = UUID.fromString(stringUuid);
|
UUID uuid = UUID.fromString(stringUuid);
|
||||||
|
|
||||||
Optional<Player> player = DiscordLink.getPlugin().getProxy().getPlayer(uuid);
|
Optional<Player> player = DiscordLink.getPlugin().getProxy().getPlayer(uuid);
|
||||||
|
|
@ -58,8 +64,14 @@ public class LiteBansBanListener {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ALogger.info(String.format("Making evidence folder for %s", username));
|
||||||
|
|
||||||
Evidence evidence = new Evidence();
|
Evidence evidence = new Evidence();
|
||||||
evidence.getNewEvidenceFolder(username).thenAcceptAsync(optionalUrl -> {
|
evidence.getNewEvidenceFolder(username).handle((optionalUrl, ex) -> {
|
||||||
|
if (ex != null) {
|
||||||
|
ALogger.error("Failed to make evidence folder", ex);
|
||||||
|
return Optional.empty();
|
||||||
|
}
|
||||||
Optional<MessageEmbed.Field> field = banDiscordUserIfExists(entry, uuid);
|
Optional<MessageEmbed.Field> field = banDiscordUserIfExists(entry, uuid);
|
||||||
|
|
||||||
EmbedBuilder banEvidence = new EmbedBuilder()
|
EmbedBuilder banEvidence = new EmbedBuilder()
|
||||||
|
|
@ -70,15 +82,20 @@ public class LiteBansBanListener {
|
||||||
.addField("Reason", entry.getReason() == null ? "unknown" : entry.getReason(), true)
|
.addField("Reason", entry.getReason() == null ? "unknown" : entry.getReason(), true)
|
||||||
.setTitle("Auto Discord ban");
|
.setTitle("Auto Discord ban");
|
||||||
|
|
||||||
|
Optional<String> returnUrl;
|
||||||
if (optionalUrl.isPresent()) {
|
if (optionalUrl.isPresent()) {
|
||||||
banEvidence.addField("Evidence", optionalUrl.get(), false);
|
String s = optionalUrl.get();
|
||||||
|
banEvidence.addField("Evidence", s, false);
|
||||||
|
returnUrl = Optional.of(s);
|
||||||
} else {
|
} else {
|
||||||
banEvidence.addField("Evidence", "Failed to get url, please make the folder yourself and reply to this post with the link.", false);
|
banEvidence.addField("Evidence", "Failed to get url, please make the folder yourself and reply to this post with the link.", false);
|
||||||
|
returnUrl = Optional.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
field.ifPresent(banEvidence::addField);
|
field.ifPresent(banEvidence::addField);
|
||||||
|
|
||||||
DiscordLink.getPlugin().getBot().sendEmbedToDiscord(BotConfig.DISCORD.EVIDENCE_CHANNEL_ID, banEvidence, -1);
|
DiscordLink.getPlugin().getBot().sendEmbedToDiscord(BotConfig.DISCORD.EVIDENCE_CHANNEL_ID, banEvidence, -1);
|
||||||
|
return returnUrl;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user