From 8bab33942ee22847f583ea87d00aa9106ba49f7c Mon Sep 17 00:00:00 2001 From: yoyosource Date: Wed, 28 Jul 2021 16:45:25 +0200 Subject: [PATCH] Add EventManager --- .../bungeecore/bot/SteamwarDiscordBot.java | 122 ++---------------- .../bot/config/SteamwarDiscordBotConfig.java | 2 + .../bungeecore/bot/events/EventManager.java | 105 +++++++++++++++ 3 files changed, 115 insertions(+), 114 deletions(-) create mode 100644 src/de/steamwar/bungeecore/bot/events/EventManager.java diff --git a/src/de/steamwar/bungeecore/bot/SteamwarDiscordBot.java b/src/de/steamwar/bungeecore/bot/SteamwarDiscordBot.java index cb3ae26b..30b2649f 100644 --- a/src/de/steamwar/bungeecore/bot/SteamwarDiscordBot.java +++ b/src/de/steamwar/bungeecore/bot/SteamwarDiscordBot.java @@ -21,27 +21,23 @@ package de.steamwar.bungeecore.bot; import de.steamwar.bungeecore.BungeeCore; import de.steamwar.bungeecore.bot.config.SteamwarDiscordBotConfig; +import de.steamwar.bungeecore.bot.events.EventManager; import de.steamwar.bungeecore.bot.listeners.RolesInteractionButtonListener; import de.steamwar.bungeecore.bot.util.DiscordRolesMessage; import de.steamwar.bungeecore.bot.util.DiscordRulesMessage; import de.steamwar.bungeecore.sql.Event; -import de.steamwar.bungeecore.sql.EventFight; +import de.steamwar.bungeecore.sql.Team; +import de.steamwar.bungeecore.sql.TeamTeilnahme; import lombok.Getter; import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.JDABuilder; import net.dv8tion.jda.api.OnlineStatus; import net.dv8tion.jda.api.entities.Activity; -import net.dv8tion.jda.api.entities.ActivityFlag; -import net.dv8tion.jda.api.entities.RichPresence; import net.dv8tion.jda.api.hooks.ListenerAdapter; import net.dv8tion.jda.internal.entities.EntityBuilder; -import net.dv8tion.jda.internal.entities.SelfUserImpl; import net.md_5.bungee.api.ProxyServer; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; import javax.security.auth.login.LoginException; -import java.util.EnumSet; import java.util.concurrent.TimeUnit; public class SteamwarDiscordBot { @@ -58,112 +54,6 @@ public class SteamwarDiscordBot { public SteamwarDiscordBot() { INSTANCE = this; JDABuilder builder = JDABuilder.createDefault(SteamwarDiscordBotConfig.TOKEN); - /*builder.setActivity(new RichPresence() { - @Override - public long getApplicationIdLong() { - return 869611389818400779L; - } - - @NotNull - @Override - public String getApplicationId() { - return "869611389818400779"; - } - - @Nullable - @Override - public String getSessionId() { - return null; - } - - @Nullable - @Override - public String getSyncId() { - return null; - } - - @Override - public int getFlags() { - return ActivityFlag.PLAY.getRaw(); - } - - @Override - public EnumSet getFlagSet() { - return EnumSet.of(ActivityFlag.PLAY); - } - - @Nullable - @Override - public String getState() { - return "Steamwar.de"; - } - - @Nullable - @Override - public String getDetails() { - return "Spielt auf Steamwar.de"; - } - - @Nullable - @Override - public Party getParty() { - return new Party(null, 1, 100); - } - - @Nullable - @Override - public Image getLargeImage() { - return null; - } - - @Nullable - @Override - public Image getSmallImage() { - return null; - } - - @Override - public boolean isRich() { - return true; - } - - @Nullable - @Override - public RichPresence asRichPresence() { - return this; - } - - @NotNull - @Override - public String getName() { - return "SteamWar"; - } - - @Nullable - @Override - public String getUrl() { - return null; - } - - @NotNull - @Override - public ActivityType getType() { - return ActivityType.DEFAULT; - } - - @Nullable - @Override - public Timestamps getTimestamps() { - return null; - } - - @Nullable - @Override - public Emoji getEmoji() { - return null; - } - });*/ - builder.setStatus(OnlineStatus.ONLINE); try { jda = builder.build(); @@ -176,7 +66,11 @@ public class SteamwarDiscordBot { e.printStackTrace(); } activity(); - ProxyServer.getInstance().getScheduler().schedule(BungeeCore.get(), this::activity, 30, 30, TimeUnit.SECONDS); + EventManager.update(); + ProxyServer.getInstance().getScheduler().schedule(BungeeCore.get(), () -> { + activity(); + EventManager.update(); + }, 30, 30, TimeUnit.SECONDS); DiscordRolesMessage.sendMessage(); DiscordRulesMessage.sendMessage(); diff --git a/src/de/steamwar/bungeecore/bot/config/SteamwarDiscordBotConfig.java b/src/de/steamwar/bungeecore/bot/config/SteamwarDiscordBotConfig.java index b1baaee7..9726763b 100644 --- a/src/de/steamwar/bungeecore/bot/config/SteamwarDiscordBotConfig.java +++ b/src/de/steamwar/bungeecore/bot/config/SteamwarDiscordBotConfig.java @@ -28,6 +28,7 @@ public class SteamwarDiscordBotConfig { public static String TOKEN; public static String GUILD; + public static String EVENTS_CHANNEL; public static String ROLES_CHANNEL; public static String ROLES_BASE_MESSAGE; public static String ROLES_ADDED; @@ -41,6 +42,7 @@ public class SteamwarDiscordBotConfig { public static void loadConfig(Configuration config) { TOKEN = config.getString("token"); GUILD = config.getString("guild"); + EVENTS_CHANNEL = config.getString("events-channel"); Configuration rolesSection = config.getSection("roles-claim"); ROLES_CHANNEL = rolesSection.getString("channel"); ROLES_BASE_MESSAGE = rolesSection.getString("base"); diff --git a/src/de/steamwar/bungeecore/bot/events/EventManager.java b/src/de/steamwar/bungeecore/bot/events/EventManager.java new file mode 100644 index 00000000..34595437 --- /dev/null +++ b/src/de/steamwar/bungeecore/bot/events/EventManager.java @@ -0,0 +1,105 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2020 SteamWar.de-Serverteam + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.steamwar.bungeecore.bot.events; + +import de.steamwar.bungeecore.bot.SteamwarDiscordBot; +import de.steamwar.bungeecore.bot.config.SteamwarDiscordBotConfig; +import de.steamwar.bungeecore.sql.Event; +import de.steamwar.bungeecore.sql.EventFight; +import de.steamwar.bungeecore.sql.Team; +import lombok.experimental.UtilityClass; +import net.dv8tion.jda.api.EmbedBuilder; +import net.dv8tion.jda.api.MessageBuilder; +import net.dv8tion.jda.api.entities.Message; +import net.dv8tion.jda.api.entities.TextChannel; + +import java.awt.*; +import java.sql.Timestamp; +import java.time.Instant; + +@UtilityClass +public class EventManager { + + private Message message; + private TextChannel textChannel; + + static { + textChannel = SteamwarDiscordBot.instance().getJda().getGuildById(SteamwarDiscordBotConfig.GUILD).getTextChannelById(SteamwarDiscordBotConfig.EVENTS_CHANNEL); + assert textChannel != null; + if(textChannel.hasLatestMessage()) { + textChannel.getIterableHistory().complete().forEach(message -> message.delete().complete()); + } + } + + public void update() { + if (Event.get() == null) { + updateComing(); + } else { + updateCurrent(); + } + } + + private void updateComing() { + EmbedBuilder embedBuilder = new EmbedBuilder(); + embedBuilder.setColor(Color.GRAY); + embedBuilder.setTitle("Zukünftige Events"); + embedBuilder.setAuthor("SteamWar", "https://www.steamwar.de"); + + Timestamp now = Timestamp.from(Instant.now()); + Event.getComing().forEach(event -> { + StringBuilder st = new StringBuilder(); + if (event.getDeadline().after(now)) { + st.append("Deadline: \n"); + } + st.append("Start: "); + embedBuilder.addField(event.getEventName(), st.toString(), false); + }); + + MessageBuilder messageBuilder = new MessageBuilder(); + messageBuilder.setEmbeds(embedBuilder.build()); + if (message == null) { + message = textChannel.sendMessage(messageBuilder.build()).complete(); + } else { + message.editMessage(messageBuilder.build()).complete(); + } + } + + private void updateCurrent() { + Event event = Event.get(); + if (event == null) return; + EmbedBuilder embedBuilder = new EmbedBuilder(); + embedBuilder.setColor(Color.GRAY); + embedBuilder.setTitle("Event: " + event.getEventName()); + embedBuilder.setAuthor("SteamWar", "https://www.steamwar.de"); + + EventFight.getEvent(event.getEventID()).forEach(eventFight -> { + embedBuilder.addField(Team.get(eventFight.getTeamBlue()) + " vs. " + Team.get(eventFight.getTeamRed()), "", true); + }); + + MessageBuilder messageBuilder = new MessageBuilder(); + messageBuilder.setEmbeds(embedBuilder.build()); + if (message == null) { + message = textChannel.sendMessage(messageBuilder.build()).complete(); + } else { + message.editMessage(messageBuilder.build()).complete(); + } + } + +}