Skip to content

Commit

Permalink
1.2.1: Added Stringbuilder to Console to Discord so large error messa…
Browse files Browse the repository at this point in the history
…ges will get send in one message instead of many messages
  • Loading branch information
notTamion committed May 16, 2023
1 parent b8de9fc commit 2595293
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>de.tamion</groupId>
<artifactId>DCChatMC</artifactId>
<version>1.2.0</version>
<version>1.2.1</version>
<packaging>jar</packaging>

<name>DCChat</name>
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/de/tamion/discord/DCMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import de.tamion.minecraft.listeners.Chat;
import de.tamion.minecraft.listeners.JoinLeave;
import de.tamion.others.ConsoleAppender;
import de.tamion.others.ConsoleBuilder;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.OnlineStatus;
Expand Down Expand Up @@ -48,6 +49,7 @@ public static void startup() {
Logger log = (Logger) LogManager.getRootLogger();
log.addAppender(new ConsoleAppender());
jb.addEventListeners(new Console());
ConsoleBuilder.consolescheduler();
MCMain.getPlugin().getLogger().info("Console started");
}
} else {
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/de/tamion/others/ConsoleAppender.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,16 @@ public boolean isStarted() {
public void append(LogEvent e) {
FileConfiguration config = MCMain.getPlugin().getConfig();
if(DCMain.jda != null && DCMain.jda.getGuildById(config.getString("Bot.guildid")) != null) {
DCMain.jda.getGuildById(config.getString("Bot.guildid")).getTextChannelById(config.getString("Bot.consoleid")).sendMessage(e.getMessage().getFormattedMessage()).queue();
if(ConsoleBuilder.sb.length()+e.getMessage().getFormattedMessage().length()>1990) {
DCMain.jda.getGuildById(config.getString("Bot.guildid")).getTextChannelById(config.getString("Bot.consoleid")).sendMessage(ConsoleBuilder.sb.toString()).queue();
ConsoleBuilder.sb.setLength(0);
if(e.getMessage().getFormattedMessage().length()<1990) {
ConsoleBuilder.sb.append(e.getMessage().getFormattedMessage() + "\n");
}
} else {
ConsoleBuilder.sb.append(e.getMessage().getFormattedMessage() + "\n");
}

}
}
}
22 changes: 22 additions & 0 deletions src/main/java/de/tamion/others/ConsoleBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package de.tamion.others;

import de.tamion.discord.DCMain;
import de.tamion.minecraft.MCMain;
import org.bukkit.Bukkit;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.scheduler.BukkitScheduler;

public class ConsoleBuilder {

public static StringBuilder sb = new StringBuilder();
public static void consolescheduler() {
FileConfiguration config = MCMain.getPlugin().getConfig();
BukkitScheduler scheduler = Bukkit.getScheduler();
scheduler.runTaskTimer(MCMain.getPlugin(), () -> {
if(sb.length() != 0) {
DCMain.jda.getGuildById(config.getString("Bot.guildid")).getTextChannelById(config.getString("Bot.consoleid")).sendMessage(sb.toString()).queue();
sb.setLength(0);
}
},20L ,20L);
}
}

0 comments on commit 2595293

Please sign in to comment.