refactor: improve connection message suppressing in discord and logger

i also saw nbot, fnf bot, and other bots copying the message, so i changed that too, hopefully no one skids it again
This commit is contained in:
ChomeNS
2025-04-10 08:10:40 +07:00
parent 70bf2574cf
commit 58fcd586a4
4 changed files with 18 additions and 25 deletions

View File

@@ -1 +1 @@
2574
2576

View File

@@ -65,6 +65,9 @@ public class Bot extends SessionAdapter {
public boolean printDisconnectedCause = false;
public int connectingTimes = 0;
public int disconnectedTimes = 0;
public boolean loggedIn = false;
public long loginTime;
@@ -180,6 +183,8 @@ public class Bot extends SessionAdapter {
private void reconnect () {
if (session != null) session = null; // does this do nothing?
connectingTimes++;
for (final Listener listener : listeners) {
listener.connecting();
}
@@ -241,6 +246,8 @@ public class Bot extends SessionAdapter {
private void packetReceived (final ClientboundLoginPacket ignoredPacket) {
loggedIn = true;
loginTime = System.currentTimeMillis();
connectingTimes = 0;
disconnectedTimes = 0;
for (final SessionListener listener : listeners) {
listener.connected(new ConnectedEvent(session));
@@ -359,6 +366,8 @@ public class Bot extends SessionAdapter {
public void disconnected (final DisconnectedEvent disconnectedEvent) {
loggedIn = false;
disconnectedTimes++;
final Throwable cause = disconnectedEvent.getCause();
if (printDisconnectedCause && cause != null) logger.error(cause);

View File

@@ -46,8 +46,6 @@ public class DiscordPlugin extends ListenerAdapter {
public final String discordUrl;
private final Map<String, Integer> totalConnects = new HashMap<>();
public DiscordPlugin (final Configuration config) {
final Configuration.Discord options = config.discord;
this.prefix = options.prefix;
@@ -82,8 +80,6 @@ public class DiscordPlugin extends ListenerAdapter {
bot.executor.scheduleAtFixedRate(() -> onDiscordTick(channelId), 0, 50, TimeUnit.MILLISECONDS);
totalConnects.put(channelId, 0); // is this necessary?
bot.addListener(new Bot.Listener() {
@Override
public void loadedPlugins (final Bot bot) {
@@ -108,13 +104,9 @@ public class DiscordPlugin extends ListenerAdapter {
@Override
public void connecting () {
final int newTotalConnects = totalConnects.get(channelId) + 1;
totalConnects.put(channelId, newTotalConnects);
if (newTotalConnects > 6) return;
else if (newTotalConnects == 6) {
sendMessageInstantly("Suspending connecting and disconnect messages from now on", channelId);
if (bot.connectingTimes > 6) return;
else if (bot.connectingTimes == 6) {
sendMessageInstantly("Suppressing connection status messages from now on", channelId);
return;
}
@@ -130,8 +122,6 @@ public class DiscordPlugin extends ListenerAdapter {
@Override
public void connected (final ConnectedEvent event) {
totalConnects.put(channelId, 0);
sendMessageInstantly(
String.format(
"Successfully connected to: `%s`",
@@ -143,7 +133,7 @@ public class DiscordPlugin extends ListenerAdapter {
@Override
public void disconnected (final DisconnectedEvent event) {
if (totalConnects.get(channelId) >= 6) return;
if (bot.disconnectedTimes >= 6) return;
final String reason = ComponentUtilities.stringifyDiscordAnsi(event.getReason());
sendMessageInstantly(

View File

@@ -13,19 +13,15 @@ public class LoggerPlugin implements ChatPlugin.Listener {
public boolean logToConsole = true;
private int totalConnects = 0;
public LoggerPlugin (final Bot bot) {
this.bot = bot;
bot.addListener(new Bot.Listener() {
@Override
public void connecting () {
totalConnects++;
if (totalConnects > 20) return;
else if (totalConnects == 20) {
log("Suspending connecting and disconnect messages from now on");
if (bot.connectingTimes > 10) return;
else if (bot.connectingTimes == 10) {
log("Suppressing connection status messages from now on");
return;
}
@@ -46,13 +42,11 @@ public class LoggerPlugin implements ChatPlugin.Listener {
bot.getServerString(true)
)
);
totalConnects = 0;
}
@Override
public void disconnected (final DisconnectedEvent event) {
if (totalConnects >= 20) return;
if (bot.disconnectedTimes >= 10) return;
final Component message = Component.translatable(
"Disconnected from %s, reason: %s",