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;
|
||||
|
||||
@Inject
|
||||
public DiscordLink(ProxyServer proxyServer, Logger proxyLogger, @DataDirectory Path proxydataDirectory)
|
||||
{
|
||||
public DiscordLink(ProxyServer proxyServer, Logger proxyLogger, @DataDirectory Path proxydataDirectory) {
|
||||
plugin = this;
|
||||
server = proxyServer;
|
||||
logger = proxyLogger;
|
||||
|
|
@ -71,6 +70,12 @@ public class DiscordLink {
|
|||
loadEvents();
|
||||
loadBot();
|
||||
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() {
|
||||
|
|
@ -94,7 +99,6 @@ public class DiscordLink {
|
|||
bot.connect();
|
||||
}
|
||||
|
||||
|
||||
public File getDataDirectory() {
|
||||
return dataDirectory.toFile();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package com.alttd.proxydiscordlink;
|
||||
|
||||
import com.alttd.proxydiscordlink.bot.commandManager.CommandManager;
|
||||
import com.alttd.proxydiscordlink.util.ALogger;
|
||||
import net.dv8tion.jda.api.JDA;
|
||||
import net.dv8tion.jda.api.events.session.ReadyEvent;
|
||||
import net.dv8tion.jda.api.hooks.ListenerAdapter;
|
||||
|
|
@ -16,8 +17,9 @@ public class JDAListener extends ListenerAdapter {
|
|||
|
||||
@Override
|
||||
public void onReady(@NotNull ReadyEvent event) {
|
||||
CommandManager commandManager = new CommandManager(jda);
|
||||
jda.addEventListener(commandManager);
|
||||
// ALogger.info("JDA ready, loading command manager");
|
||||
// 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.DiscordLink;
|
||||
import com.alttd.proxydiscordlink.bot.commandManager.CommandManager;
|
||||
import com.alttd.proxydiscordlink.bot.listeners.DiscordRoleListener;
|
||||
import com.alttd.proxydiscordlink.bot.tasks.CheckLinkSync;
|
||||
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.exceptions.HierarchyException;
|
||||
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.utils.MemberCachePolicy;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
|
@ -31,6 +31,7 @@ public class Bot {
|
|||
public void connect() {
|
||||
disconnect();
|
||||
try {
|
||||
ALogger.info("Creating bot instance");
|
||||
jda = JDABuilder
|
||||
.createDefault(BotConfig.DISCORD.BOT_TOKEN)
|
||||
.setMemberCachePolicy(MemberCachePolicy.ALL)
|
||||
|
|
@ -38,13 +39,16 @@ public class Bot {
|
|||
.build();
|
||||
jda.setAutoReconnect(true);
|
||||
jda.awaitReady();
|
||||
ALogger.info("JDA ready");
|
||||
jda.addEventListener(
|
||||
new DiscordRoleListener(),
|
||||
new JDAListener(jda));
|
||||
new DiscordRoleListener()/*,
|
||||
new JDAListener(jda)*/);
|
||||
DiscordLink.getPlugin().getProxy().getScheduler().buildTask(DiscordLink.getPlugin(), new CheckLinkSync())
|
||||
.delay(120, TimeUnit.SECONDS)
|
||||
.repeat(12, TimeUnit.HOURS)
|
||||
.schedule();
|
||||
CommandManager commandManager = new CommandManager(jda);
|
||||
jda.addEventListener(commandManager);
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -37,6 +37,7 @@ public class CommandManager extends ListenerAdapter {
|
|||
.filter(discordCommand -> discordCommand.getName().equalsIgnoreCase(commandName))
|
||||
.findFirst();
|
||||
if (first.isEmpty()) {
|
||||
ALogger.info(String.format("The command %s was used, but it's not on this plugin", commandName));
|
||||
return;
|
||||
}
|
||||
first.get().execute(event);
|
||||
|
|
|
|||
|
|
@ -90,18 +90,19 @@ public class CommandLink extends DiscordCommand {
|
|||
Utilities.commandErrAutoRem("Unable to find guild", event);
|
||||
return;
|
||||
}
|
||||
if (player != null || user != null)
|
||||
if (player != null || user != null) {
|
||||
DiscordLink.getPlugin().getBot().changeNick(
|
||||
guild.getIdLong(),
|
||||
member.getIdLong(),
|
||||
player == null ?
|
||||
user.getUsername() :
|
||||
player.getUsername());
|
||||
else
|
||||
} else {
|
||||
DiscordLink.getPlugin().getBot().changeNick(
|
||||
guild.getIdLong(),
|
||||
member.getIdLong(),
|
||||
discordLinkPlayer.getUsername());
|
||||
}
|
||||
|
||||
event.replyEmbeds(Utilities.genericSuccessEmbed("Success","You have successfully linked " +
|
||||
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));
|
||||
|
||||
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) {
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ import ninja.leaping.configurate.ConfigurationNode;
|
|||
import ninja.leaping.configurate.ConfigurationOptions;
|
||||
import ninja.leaping.configurate.objectmapping.ObjectMappingException;
|
||||
import ninja.leaping.configurate.yaml.YAMLConfigurationLoader;
|
||||
import org.aarboard.nextcloud.api.utils.WebdavInputStream;
|
||||
import org.yaml.snakeyaml.DumperOptions;
|
||||
|
||||
import java.io.File;
|
||||
|
|
@ -174,7 +175,9 @@ public final class Config {
|
|||
@SuppressWarnings("unused")
|
||||
private static void loadSubclasses() {
|
||||
DB.database();
|
||||
WORDPRESS_DB.database();
|
||||
MESSAGES.loadMessages();
|
||||
NEXT_CLOUD.loadNextCloud();
|
||||
}
|
||||
|
||||
public static class DB {
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ 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.ALogger;
|
||||
import com.alttd.proxydiscordlink.util.Evidence;
|
||||
import com.alttd.proxydiscordlink.util.Utilities;
|
||||
import com.velocitypowered.api.proxy.Player;
|
||||
|
|
@ -37,11 +38,16 @@ public class LiteBansBanListener {
|
|||
}
|
||||
|
||||
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;
|
||||
}
|
||||
String stringUuid = entry.getUuid();
|
||||
if (stringUuid == null)
|
||||
if (stringUuid == null) {
|
||||
ALogger.warn("Failed to find uuid in entry, no evidence made");
|
||||
return;
|
||||
}
|
||||
UUID uuid = UUID.fromString(stringUuid);
|
||||
|
||||
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.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);
|
||||
|
||||
EmbedBuilder banEvidence = new EmbedBuilder()
|
||||
|
|
@ -70,15 +82,20 @@ public class LiteBansBanListener {
|
|||
.addField("Reason", entry.getReason() == null ? "unknown" : entry.getReason(), true)
|
||||
.setTitle("Auto Discord ban");
|
||||
|
||||
Optional<String> returnUrl;
|
||||
if (optionalUrl.isPresent()) {
|
||||
banEvidence.addField("Evidence", optionalUrl.get(), false);
|
||||
String s = optionalUrl.get();
|
||||
banEvidence.addField("Evidence", s, false);
|
||||
returnUrl = Optional.of(s);
|
||||
} else {
|
||||
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);
|
||||
|
||||
DiscordLink.getPlugin().getBot().sendEmbedToDiscord(BotConfig.DISCORD.EVIDENCE_CHANNEL_ID, banEvidence, -1);
|
||||
return returnUrl;
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user