From 03382c853b24f711287cf068f4ee99eb7dbbdbde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20Gla=C3=9F?= Date: Sun, 13 Mar 2022 19:48:47 +0100 Subject: [PATCH] [Jo alter geh kaggern] --- .../yoshibot/main/YoshiBot.java | 23 +++++----- .../yoshibot/utils/Resources.java | 28 +++++++----- .../yoshibot/utils/StatusProvider.java | 43 +++++++++++++------ rsc/Config.properties | 16 ++++--- 4 files changed, 68 insertions(+), 42 deletions(-) diff --git a/app/src/main/java/de/yannicpunktdee/yoshibot/main/YoshiBot.java b/app/src/main/java/de/yannicpunktdee/yoshibot/main/YoshiBot.java index ffcc5ce..8b0d3f5 100644 --- a/app/src/main/java/de/yannicpunktdee/yoshibot/main/YoshiBot.java +++ b/app/src/main/java/de/yannicpunktdee/yoshibot/main/YoshiBot.java @@ -115,17 +115,18 @@ public final class YoshiBot { commandLineThread = new CommandLine(); commandLineThread.start(); - allProvides.add(new StatusProvider("Kreativ - 1.17", Resources.getStatus_message_vanilla(), guild, - Resources.getStatus_update(), - 25565)); - allProvides.add(new StatusProvider("Valhelsia 3 - 3.4.7", Resources.getStatus_message_modded(), guild, - Resources.getStatus_update(), - 25566)); - allProvides.add(new StatusProvider("Vanilla - 1.18", Resources.getStatus_message_18(), guild, - Resources.getStatus_update(), - 25567)); - - SauceProvider.init(300); + allProvides.add( + new StatusProvider("Kreativ - 1.17", "status_message_kreativ", guild, Resources.getStatus_update(), + 25565)); + allProvides.add( + new StatusProvider("Valhelsia 3 - 3.4.7", "status_message_modded", guild, Resources.getStatus_update(), + 25566)); + allProvides.add(new StatusProvider("Vanilla - 1.18", "status_message_eighteen_vanilla", guild, + Resources.getStatus_update(), 25567)); + allProvides.add(new StatusProvider("MCSelina oder so, keine Ahnung", "status_message_selina", guild, + Resources.getStatus_update(), 25568)); + + //SauceProvider.init(300); Executors.newScheduledThreadPool(1).scheduleAtFixedRate(YoshiBot::setRandomActivity, 0, 10, TimeUnit.HOURS); diff --git a/app/src/main/java/de/yannicpunktdee/yoshibot/utils/Resources.java b/app/src/main/java/de/yannicpunktdee/yoshibot/utils/Resources.java index cade7e4..88cc8ba 100644 --- a/app/src/main/java/de/yannicpunktdee/yoshibot/utils/Resources.java +++ b/app/src/main/java/de/yannicpunktdee/yoshibot/utils/Resources.java @@ -8,6 +8,7 @@ import org.json.JSONObject; import java.io.File; import java.io.FileInputStream; +import java.io.FileOutputStream; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; @@ -29,8 +30,7 @@ public final class Resources { @Getter private static String resourcePath, configPath, audioPath, tempPath, activitiesPath, greetingsPath, byebyesPath, sauceConfigPath, ttsPath, patPngPath, imagePath, bonkPngPath, jda_builder_string, mc_server, status_channel, - status_message_vanilla, status_message_modded, status_message_18, path_to_mcstatus, comebacksPath, - departsPath; + path_to_mcstatus, comebacksPath, departsPath; private static List greetings, byebyes, departs, comebacks; @@ -288,15 +288,10 @@ public final class Resources { } private static boolean initStatusMessage() { - if (propertiesFile.containsKey("mc_server") && propertiesFile.containsKey("status_channel") - && propertiesFile.containsKey("status_message_vanilla") && propertiesFile.containsKey("status_update") && - propertiesFile.containsKey("status_message_modded")) { - mc_server = propertiesFile.getProperty("mc_server"); - status_channel = propertiesFile.getProperty("status_channel"); - status_message_vanilla = propertiesFile.getProperty("status_message_vanilla"); - status_message_modded = propertiesFile.getProperty("status_message_modded"); - status_message_18 = propertiesFile.getProperty("status_message_eighteen"); - path_to_mcstatus = propertiesFile.getProperty("path_to_mcstatus"); + if (propertiesFile.containsKey("mc_server") && propertiesFile.containsKey("status_channel")) { + mc_server = propertiesFile.getProperty("mc_server"); + status_channel = propertiesFile.getProperty("status_channel"); + path_to_mcstatus = propertiesFile.getProperty("path_to_mcstatus"); try { status_update = Integer.parseInt(propertiesFile.getProperty("status_update")); } catch (NumberFormatException e) { @@ -306,4 +301,15 @@ public final class Resources { } else return false; } + public static String getProperty(String key) { + return propertiesFile.getProperty(key); + } + + @SneakyThrows + public synchronized static void setProperty(String key, String value) { + propertiesFile.setProperty(key, value); + + propertiesFile.store(new FileOutputStream(configPath), ""); + } + } diff --git a/app/src/main/java/de/yannicpunktdee/yoshibot/utils/StatusProvider.java b/app/src/main/java/de/yannicpunktdee/yoshibot/utils/StatusProvider.java index e727cfc..b538b9b 100644 --- a/app/src/main/java/de/yannicpunktdee/yoshibot/utils/StatusProvider.java +++ b/app/src/main/java/de/yannicpunktdee/yoshibot/utils/StatusProvider.java @@ -1,8 +1,11 @@ package de.yannicpunktdee.yoshibot.utils; +import de.yannicpunktdee.yoshibot.main.YoshiBot; import lombok.SneakyThrows; import net.dv8tion.jda.api.EmbedBuilder; +import net.dv8tion.jda.api.MessageBuilder; import net.dv8tion.jda.api.entities.Guild; +import net.dv8tion.jda.api.entities.Message; import net.dv8tion.jda.api.entities.TextChannel; import org.apache.commons.io.FileUtils; import org.json.JSONObject; @@ -30,7 +33,7 @@ public class StatusProvider implements Provider { private int lastPlayersOnline = -1; - private final String message_id; + private volatile String messageId = null; private final int serverPort; @@ -45,9 +48,13 @@ public class StatusProvider implements Provider { @SneakyThrows - public StatusProvider(String desc, String messageId, Guild guild, int secondsPerTime, int serverPort) { + @SuppressWarnings("empty") + public StatusProvider(String desc, String messageIdKey, Guild guild, int secondsPerTime, + int serverPort) { + TextChannel statusChannel = guild.getTextChannelById(Resources.getStatus_channel()); + this.desc = desc; - this.message_id = messageId; + this.messageId = StatusProvider.ensureMessageId(statusChannel, messageIdKey, guild); this.serverPort = serverPort; this.mcstatus.command(Resources.getPath_to_mcstatus(), Resources.getMc_server() + ":" + serverPort, "json"); @@ -61,11 +68,24 @@ public class StatusProvider implements Provider { } } - TextChannel statusChannel = guild.getTextChannelById(Resources.getStatus_channel()); statusScheduler.scheduleAtFixedRate(() -> updateStatusMessage(statusChannel), 0, secondsPerTime, TimeUnit.SECONDS); } + private synchronized static String ensureMessageId(TextChannel statusChannel, String messageIdKey, Guild guild) { + if (Resources.getProperty(messageIdKey) != null) { + return Resources.getProperty(messageIdKey); + } else { + assert statusChannel != null; + MessageBuilder mb = new MessageBuilder(); + mb.append("ServerInformation"); + Message msg = mb.build(); + statusChannel.sendMessage(msg).complete(); + Resources.setProperty(messageIdKey, statusChannel.getLatestMessageId()); + return statusChannel.getLatestMessageId(); + } + } + @SuppressWarnings("unchecked") public void updateStatusMessage(TextChannel statusChannel) { EmbedBuilder eb = new EmbedBuilder(); @@ -102,7 +122,7 @@ public class StatusProvider implements Provider { eb.addField("Offline", "", false); } - statusChannel.editMessageById(this.message_id, eb.build()).queue(); + statusChannel.editMessageById(this.messageId, eb.build()).queue(); } catch (IOException e) { Logger.logError(e.toString()); } @@ -121,8 +141,8 @@ public class StatusProvider implements Provider { Logger.logError("MCStatus on port " + serverPort + " exited with errorcode " + process.exitValue()); } - String output = new BufferedReader(new InputStreamReader(process.getInputStream())).lines().collect( - Collectors.joining()); + String output = new BufferedReader(new InputStreamReader(process.getInputStream())).lines() + .collect(Collectors.joining()); Map result = new HashMap<>(); @@ -147,10 +167,8 @@ public class StatusProvider implements Provider { result.put("playerCount", obj.getInt("player_count")); result.put("playerMax", obj.getInt("player_max")); - result.put("playerNames", - StreamSupport.stream(obj.getJSONArray("players").spliterator(), false) - .map(jsonobj -> ((JSONObject) jsonobj).getString("name")).sorted().collect( - Collectors.toList())); + result.put("playerNames", StreamSupport.stream(obj.getJSONArray("players").spliterator(), false) + .map(jsonobj -> ((JSONObject) jsonobj).getString("name")).sorted().collect(Collectors.toList())); result.put("motd", obj.getString("motd")); result.put("version", obj.getString("version")); @@ -163,8 +181,7 @@ public class StatusProvider implements Provider { Logger.logInfo(String.format("Stopping StatusProvider for \"%s\" on Port %d", desc, serverPort)); if (timestampLastPlayerOnline != null) { FileUtils.writeStringToFile(new File(Resources.getResourcePath() + "/" + serverPort + ".txt"), - TIME_FORMATTER.format(timestampLastPlayerOnline), "UTF" + - "-8"); + TIME_FORMATTER.format(timestampLastPlayerOnline), "UTF" + "-8"); } } } diff --git a/rsc/Config.properties b/rsc/Config.properties index 505fff9..feb8fdb 100644 --- a/rsc/Config.properties +++ b/rsc/Config.properties @@ -1,11 +1,13 @@ -guild_id=801554100814741524 -# audio_source_directory=C:/Users/linky/workspace/Yoshi_Bot_Audio/ +#Sun Mar 13 19:43:48 CET 2022 +status_message_vanilla=890007862456238120 +status_channel=889880296168755231 restrict_commands_to_channel=bot-muell schrein-auf-den-bot -greetings_and_byebyes_on=true +status_message_eighteen_vanilla=952638126327726150 +status_message_selina=952638127451820042 mc_server=85.214.148.23 -status_channel=889880296168755231 -status_message_vanilla=890007862456238120 -status_message_modded=901742870552281148 -status_message_eighteen=916382187023106068 +greetings_and_byebyes_on=true +guild_id=801554100814741524 +status_message_modded=952638124197040139 +status_message_kreativ=952638123324616735 status_update=30 path_to_mcstatus=/home/paul/.local/bin/mcstatus