From 27293fc051a2ab0bfc03c2290233476caff33579 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Tue, 7 Sep 2021 16:49:32 +0200 Subject: [PATCH 1/8] Add Message Spliting Signed-off-by: Chaoscaot --- .../bot/listeners/DiscordTicketListener.java | 41 ++++++++++++------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/src/de/steamwar/bungeecore/bot/listeners/DiscordTicketListener.java b/src/de/steamwar/bungeecore/bot/listeners/DiscordTicketListener.java index 8c8c4fa..f7a2fa2 100644 --- a/src/de/steamwar/bungeecore/bot/listeners/DiscordTicketListener.java +++ b/src/de/steamwar/bungeecore/bot/listeners/DiscordTicketListener.java @@ -75,29 +75,40 @@ public class DiscordTicketListener extends BasicDiscordListener { } else if(event.getComponentId().startsWith("close-")) { TextChannel logChannel = event.getGuild().getTextChannelById(SteamwarDiscordBotConfig.TICKET_LOG); - MessageBuilder builder = new MessageBuilder(); - StringBuilder stringBuilder = new StringBuilder(); + LinkedList stringBuilders = new LinkedList<>(); + stringBuilders.add(new StringBuilder()); new LinkedList<>(event.getTextChannel().getIterableHistory().complete()).descendingIterator().forEachRemaining(message -> { if(message.getAuthor().isSystem() || message.getAuthor().isBot()) return; - stringBuilder.append(" ") + StringBuilder currentBuilder = new StringBuilder(); + currentBuilder.append(" ") .append("**") .append(message.getAuthor().getName()) .append("**: ") .append(message.getContentRaw()); if(!message.getAttachments().isEmpty()) { - stringBuilder.append("\n") + currentBuilder.append("\n") .append("Files: ").append("\n"); - message.getAttachments().forEach(attachment -> stringBuilder.append(attachment.getUrl()).append("\n")); + message.getAttachments().forEach(attachment -> currentBuilder.append(attachment.getUrl()).append("\n")); } - stringBuilder.append("\n"); + currentBuilder.append("\n"); + if(currentBuilder.length() >= 4096) { + stringBuilders.getLast().append(currentBuilder.substring(0, 4090)); + stringBuilders.add(new StringBuilder(currentBuilder.substring(4090, currentBuilder.length() - 1))); + } else if (currentBuilder.length() + stringBuilders.getLast().length() >= 4096) { + stringBuilders.add(new StringBuilder(currentBuilder.toString())); + } else { + stringBuilders.getLast().append(currentBuilder); + } }); - stringBuilder.append("\n"); - stringBuilder.append(" ") - .append("**").append(event.getUser().getName()).append("**: ") - .append("Ticket geschlossen"); + String footer = " **" + event.getUser().getName() + "**: Ticket geschlossen"; + if(stringBuilders.getLast().length() + footer.length() > 4090) { + stringBuilders.add(new StringBuilder(footer)); + } else { + stringBuilders.getLast().append(footer); + } EmbedBuilder embedBuilder = new EmbedBuilder(); embedBuilder.setColor(Color.GREEN); @@ -107,11 +118,13 @@ public class DiscordTicketListener extends BasicDiscordListener { embedBuilder.setAuthor(user.getName(), swuser==null?"https://steamwar.de/":("https://steamwar.de/users/" + swuser.getUserName().toLowerCase() + "/"), user.getAvatarUrl()); embedBuilder.setTimestamp(Instant.now()); embedBuilder.setTitle(event.getTextChannel().getName()); - embedBuilder.setDescription(stringBuilder); + stringBuilders.forEach(stringBuilder -> { + embedBuilder.setDescription(stringBuilder.toString()); + MessageBuilder builder = new MessageBuilder(); + builder.setEmbeds(embedBuilder.build()); - builder.setEmbeds(embedBuilder.build()); - - logChannel.sendMessage(builder.build()).complete(); + logChannel.sendMessage(builder.build()).queue(); + }); Message.team("DISCORD_TICKET_CLOSED", event.getTextChannel().getName()); event.getTextChannel().delete().reason("Closed").queue(); From e66990da294c0a7088f77128ad1c2ef161b0ce93 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Tue, 7 Sep 2021 23:55:07 +0200 Subject: [PATCH 2/8] Add Discord Event Teilnahme Signed-off-by: Chaoscaot --- .../bungeecore/bot/SteamwarDiscordBot.java | 1 + .../bungeecore/bot/events/EventManager.java | 22 ++++- .../bot/listeners/DiscordEventListener.java | 95 +++++++++++++++++++ 3 files changed, 117 insertions(+), 1 deletion(-) create mode 100644 src/de/steamwar/bungeecore/bot/listeners/DiscordEventListener.java diff --git a/src/de/steamwar/bungeecore/bot/SteamwarDiscordBot.java b/src/de/steamwar/bungeecore/bot/SteamwarDiscordBot.java index 381720e..ba2ff10 100644 --- a/src/de/steamwar/bungeecore/bot/SteamwarDiscordBot.java +++ b/src/de/steamwar/bungeecore/bot/SteamwarDiscordBot.java @@ -99,6 +99,7 @@ public class SteamwarDiscordBot { new RolesInteractionButtonListener(); new DiscordTicketListener(); new DiscordAuthListener(); + new DiscordEventListener(); announcementListener = new AnnouncementListener(); ingameChatListener = new IngameChatListener(); serverTeamChatListener = new ServerTeamChatListener(); diff --git a/src/de/steamwar/bungeecore/bot/events/EventManager.java b/src/de/steamwar/bungeecore/bot/events/EventManager.java index d40a80c..506e8b7 100644 --- a/src/de/steamwar/bungeecore/bot/events/EventManager.java +++ b/src/de/steamwar/bungeecore/bot/events/EventManager.java @@ -28,13 +28,22 @@ import de.steamwar.bungeecore.sql.TeamTeilnahme; import lombok.experimental.UtilityClass; import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.MessageBuilder; +import net.dv8tion.jda.api.entities.Emoji; import net.dv8tion.jda.api.entities.Message; import net.dv8tion.jda.api.entities.TextChannel; +import net.dv8tion.jda.api.interactions.components.ActionRow; +import net.dv8tion.jda.api.interactions.components.Button; +import net.dv8tion.jda.api.interactions.components.ComponentLayout; +import net.dv8tion.jda.api.interactions.components.selections.SelectOption; +import net.dv8tion.jda.api.interactions.components.selections.SelectionMenu; import java.awt.*; import java.sql.Timestamp; import java.time.Instant; import java.time.temporal.ChronoUnit; +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; import java.util.stream.Collectors; @UtilityClass @@ -66,7 +75,12 @@ public class EventManager { embedBuilder.setAuthor("SteamWar", "https://www.steamwar.de"); Timestamp now = Timestamp.from(Instant.now()); - Event.getComing().forEach(event -> { + SelectionMenu.Builder menuBuilder = SelectionMenu.create("eventName"); + menuBuilder.setPlaceholder("Wähle ein Event aus!") + .setMinValues(1) + .setMaxValues(1); + List events = Event.getComing(); + events.forEach(event -> { StringBuilder st = new StringBuilder(); if (event.getDeadline().after(now)) { st.append("Deadline: \n"); @@ -77,10 +91,16 @@ public class EventManager { st.append("\nAngemeldete Teams: ").append(teilname); } embedBuilder.addField(event.getEventName(), st.toString(), false); + if(event.getDeadline().after(Timestamp.from(Instant.now()))) { + menuBuilder.addOption(event.getEventName(), event.getEventID() + "", "An " + event.getEventName() + " teilnehmen", Emoji.fromUnicode("U+1F4DD")); + } }); MessageBuilder messageBuilder = new MessageBuilder(); messageBuilder.setEmbeds(embedBuilder.build()); + if(!events.isEmpty()) { + messageBuilder.setActionRows(ActionRow.of(menuBuilder.build())); + } if (message == null) { message = textChannel.sendMessage(messageBuilder.build()).complete(); } else { diff --git a/src/de/steamwar/bungeecore/bot/listeners/DiscordEventListener.java b/src/de/steamwar/bungeecore/bot/listeners/DiscordEventListener.java new file mode 100644 index 0000000..5c26293 --- /dev/null +++ b/src/de/steamwar/bungeecore/bot/listeners/DiscordEventListener.java @@ -0,0 +1,95 @@ +/* + 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.listeners; + +import de.steamwar.bungeecore.bot.config.SteamwarDiscordBotConfig; +import de.steamwar.bungeecore.sql.Event; +import de.steamwar.bungeecore.sql.SteamwarUser; +import de.steamwar.bungeecore.sql.Team; +import de.steamwar.bungeecore.sql.TeamTeilnahme; +import net.dv8tion.jda.api.events.interaction.SelectionMenuEvent; +import net.dv8tion.jda.api.interactions.components.Component; +import org.jetbrains.annotations.NotNull; + +import java.time.Instant; + +public class DiscordEventListener extends BasicDiscordListener { + + + @Override + public void onSelectionMenu(@NotNull SelectionMenuEvent event) { + if(event.getChannel().getId().equals(SteamwarDiscordBotConfig.EVENTS_CHANNEL) && event.getComponentType() == Component.Type.SELECTION_MENU) { + if(event.getSelectedOptions().isEmpty()) { + event.deferReply(true).queue(); + return; + } + if(event.getSelectedOptions().get(0).getValue().matches("([0-9]+)")) { + SteamwarUser user = SteamwarUser.get(event.getUser().getIdLong()); + if(user == null) { + event.reply("Du hast dein Minecraft nicht verknüpft").setEphemeral(true).queue(); + return; + } + + if(user.getTeam() == 0) { + event.reply("Du bist in keinem Team").setEphemeral(true).queue(); + return; + } + + if(user.isLeader()) { + event.reply("Du bist kein Leader in deinem Team").setEphemeral(true).queue(); + return; + } + + if(Event.get() != null) { + event.reply("Du kannst dich nicht während einem Event an einem Event anmelden").setEphemeral(true).queue(); + return; + } + + Event swEvent = Event.get( + Integer.decode( + event.getSelectedOptions() + .get(0) + .getValue() + ).intValue()); + if(swEvent == null){ + event.reply("Das Event gibt es nicht").setEphemeral(true).queue(); + return; + } + + if(Instant.now().isAfter(swEvent.getDeadline().toInstant())){ + event.reply("Du kannst dich nicht mehr an diesen Event anmelden").setEphemeral(true).queue(); + return; + } + + Team team = Team.get(user.getTeam()); + + if(TeamTeilnahme.nimmtTeil(team.getTeamId(), swEvent.getEventID())){ + TeamTeilnahme.notTeilnehmen(team.getTeamId(), swEvent.getEventID()); + event.reply("Dein Team **" + team.getTeamName() + "** nimmt nun nicht mehr an **" + swEvent.getEventName() + "** teil!").setEphemeral(true).queue(); + }else{ + TeamTeilnahme.teilnehmen(team.getTeamId(), swEvent.getEventID()); + event.reply("Dein Team **" + team.getTeamName() + "** nimmt nun an **" + swEvent.getEventName() + "** teil!").setEphemeral(true).queue(); + } + } else { + event.reply("Lefuq?").setEphemeral(true).queue(); + } + } + } +} From 21dd3e3e0cb8475961a3cf1976d92bcc22919e17 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Wed, 8 Sep 2021 00:57:53 +0200 Subject: [PATCH 3/8] House Keeping Signed-off-by: Chaoscaot --- src/de/steamwar/bungeecore/bot/events/EventManager.java | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/de/steamwar/bungeecore/bot/events/EventManager.java b/src/de/steamwar/bungeecore/bot/events/EventManager.java index 506e8b7..398f05d 100644 --- a/src/de/steamwar/bungeecore/bot/events/EventManager.java +++ b/src/de/steamwar/bungeecore/bot/events/EventManager.java @@ -32,17 +32,12 @@ import net.dv8tion.jda.api.entities.Emoji; import net.dv8tion.jda.api.entities.Message; import net.dv8tion.jda.api.entities.TextChannel; import net.dv8tion.jda.api.interactions.components.ActionRow; -import net.dv8tion.jda.api.interactions.components.Button; -import net.dv8tion.jda.api.interactions.components.ComponentLayout; -import net.dv8tion.jda.api.interactions.components.selections.SelectOption; import net.dv8tion.jda.api.interactions.components.selections.SelectionMenu; import java.awt.*; import java.sql.Timestamp; import java.time.Instant; import java.time.temporal.ChronoUnit; -import java.util.ArrayList; -import java.util.LinkedList; import java.util.List; import java.util.stream.Collectors; From 96f54cf6df537cd99a912ca317a1c8597cc5351b Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Wed, 8 Sep 2021 19:40:59 +0200 Subject: [PATCH 4/8] Simplyfy Int Signed-off-by: Chaoscaot --- .../bungeecore/bot/listeners/DiscordEventListener.java | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/de/steamwar/bungeecore/bot/listeners/DiscordEventListener.java b/src/de/steamwar/bungeecore/bot/listeners/DiscordEventListener.java index 5c26293..a8a7cbe 100644 --- a/src/de/steamwar/bungeecore/bot/listeners/DiscordEventListener.java +++ b/src/de/steamwar/bungeecore/bot/listeners/DiscordEventListener.java @@ -62,12 +62,7 @@ public class DiscordEventListener extends BasicDiscordListener { return; } - Event swEvent = Event.get( - Integer.decode( - event.getSelectedOptions() - .get(0) - .getValue() - ).intValue()); + Event swEvent = Event.get(Integer.decode(event.getSelectedOptions().get(0).getValue())); if(swEvent == null){ event.reply("Das Event gibt es nicht").setEphemeral(true).queue(); return; From 613b78688aa6baf82d948d7acd1b1d2fe710380f Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Wed, 8 Sep 2021 19:44:40 +0200 Subject: [PATCH 5/8] Add EventManager Update Signed-off-by: Chaoscaot --- .../bungeecore/bot/listeners/DiscordEventListener.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/de/steamwar/bungeecore/bot/listeners/DiscordEventListener.java b/src/de/steamwar/bungeecore/bot/listeners/DiscordEventListener.java index a8a7cbe..da5ac0c 100644 --- a/src/de/steamwar/bungeecore/bot/listeners/DiscordEventListener.java +++ b/src/de/steamwar/bungeecore/bot/listeners/DiscordEventListener.java @@ -20,6 +20,7 @@ package de.steamwar.bungeecore.bot.listeners; import de.steamwar.bungeecore.bot.config.SteamwarDiscordBotConfig; +import de.steamwar.bungeecore.bot.events.EventManager; import de.steamwar.bungeecore.sql.Event; import de.steamwar.bungeecore.sql.SteamwarUser; import de.steamwar.bungeecore.sql.Team; @@ -82,6 +83,8 @@ public class DiscordEventListener extends BasicDiscordListener { TeamTeilnahme.teilnehmen(team.getTeamId(), swEvent.getEventID()); event.reply("Dein Team **" + team.getTeamName() + "** nimmt nun an **" + swEvent.getEventName() + "** teil!").setEphemeral(true).queue(); } + + EventManager.update(); } else { event.reply("Lefuq?").setEphemeral(true).queue(); } From ac2a952017885810c05a29403b828694db86b2c3 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Thu, 9 Sep 2021 21:04:56 +0200 Subject: [PATCH 6/8] Update Heap size of ListCommand --- .../bungeecore/bot/commands/ListCommand.java | 4 ++-- src/de/steamwar/bungeecore/commands/ListCommand.java | 12 ++++-------- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/src/de/steamwar/bungeecore/bot/commands/ListCommand.java b/src/de/steamwar/bungeecore/bot/commands/ListCommand.java index 3b014f9..746bb02 100644 --- a/src/de/steamwar/bungeecore/bot/commands/ListCommand.java +++ b/src/de/steamwar/bungeecore/bot/commands/ListCommand.java @@ -32,10 +32,10 @@ public class ListCommand extends BasicDiscordCommand { @Override public void run(SlashCommandEvent event) { - de.steamwar.bungeecore.commands.ListCommand.updateCustomTablist(); + de.steamwar.bungeecore.commands.ListCommand.getCustomTablist(); EmbedBuilder embedBuilder = new EmbedBuilder(); embedBuilder.setTitle("List"); - de.steamwar.bungeecore.commands.ListCommand.getPlayerMap().forEach((s, proxiedPlayers) -> { + de.steamwar.bungeecore.commands.ListCommand.getCustomTablist().forEach((s, proxiedPlayers) -> { embedBuilder.addField(s, proxiedPlayers.stream().map(player -> "`" + player.getName() + "`").collect(Collectors.joining(", ")), true); }); event.replyEmbeds(embedBuilder.build()).setEphemeral(true).queue(); diff --git a/src/de/steamwar/bungeecore/commands/ListCommand.java b/src/de/steamwar/bungeecore/commands/ListCommand.java index b7a4a63..cb2de64 100644 --- a/src/de/steamwar/bungeecore/commands/ListCommand.java +++ b/src/de/steamwar/bungeecore/commands/ListCommand.java @@ -22,7 +22,6 @@ package de.steamwar.bungeecore.commands; import de.steamwar.bungeecore.Message; import de.steamwar.bungeecore.Servertype; import de.steamwar.bungeecore.Subserver; -import lombok.Getter; import net.md_5.bungee.api.CommandSender; import net.md_5.bungee.api.ProxyServer; import net.md_5.bungee.api.config.ServerInfo; @@ -40,12 +39,8 @@ public class ListCommand extends BasicCommand { super("list", ""); } - @Getter - private static final TreeMap> playerMap = new TreeMap<>(); - - public static synchronized void updateCustomTablist(){ - //Calculate server-player-map - playerMap.clear(); + public static synchronized TreeMap> getCustomTablist(){ + TreeMap> playerMap = new TreeMap<>(); for (ProxiedPlayer player : ProxyServer.getInstance().getPlayers()) { Server pserver = player.getServer(); if (pserver == null) //Happens temporarily @@ -62,11 +57,12 @@ public class ListCommand extends BasicCommand { } } playerMap.forEach((server, players) -> players.sort((proxiedPlayer, t1) -> proxiedPlayer.getName().compareToIgnoreCase(t1.getName()))); + return playerMap; } @Override public void execute(CommandSender commandSender, String[] strings) { - updateCustomTablist(); + TreeMap> playerMap = getCustomTablist(); for (String server : playerMap.navigableKeySet()) { if (server.equals("Bau")) { server = Message.parse("TABLIST_BAU", commandSender); From 689538f6c5153b6967f707811d0ead4a316f4d98 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Thu, 9 Sep 2021 21:30:48 +0200 Subject: [PATCH 7/8] Hotfix DiscordBot command integration --- src/de/steamwar/bungeecore/bot/SteamwarDiscordBot.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/de/steamwar/bungeecore/bot/SteamwarDiscordBot.java b/src/de/steamwar/bungeecore/bot/SteamwarDiscordBot.java index ba2ff10..8ff2dfc 100644 --- a/src/de/steamwar/bungeecore/bot/SteamwarDiscordBot.java +++ b/src/de/steamwar/bungeecore/bot/SteamwarDiscordBot.java @@ -108,7 +108,7 @@ public class SteamwarDiscordBot { jda.retrieveCommands().complete().forEach(command -> jda.deleteCommandById(command.getId()).queue()); Guild guild = jda.getGuildById(SteamwarDiscordBotConfig.GUILD); - guild.retrieveCommands().queue(commands -> commands.forEach(command -> guild.deleteCommandById(command.getId()).queue())); + guild.retrieveCommands().complete().forEach(command -> guild.deleteCommandById(command.getId()).complete()); CommandListUpdateAction commands = jda.getGuildById(SteamwarDiscordBotConfig.GUILD).updateCommands(); addCommand(commands, new MuteCommand()); addCommand(commands, new BanCommand()); @@ -116,7 +116,7 @@ public class SteamwarDiscordBot { addCommand(commands, new TeamCommand()); addCommand(commands, new ListCommand()); addCommand(commands, new UnbanCommand()); - commands.queue(); + commands.complete(); }); } From 2d89d642886c5c47f92fac0bb51ac5ffd781dd36 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Fri, 10 Sep 2021 20:57:19 +0200 Subject: [PATCH 8/8] Hotfix DiscordEventListener --- .../steamwar/bungeecore/bot/listeners/DiscordEventListener.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/de/steamwar/bungeecore/bot/listeners/DiscordEventListener.java b/src/de/steamwar/bungeecore/bot/listeners/DiscordEventListener.java index da5ac0c..a08f74d 100644 --- a/src/de/steamwar/bungeecore/bot/listeners/DiscordEventListener.java +++ b/src/de/steamwar/bungeecore/bot/listeners/DiscordEventListener.java @@ -53,7 +53,7 @@ public class DiscordEventListener extends BasicDiscordListener { return; } - if(user.isLeader()) { + if(!user.isLeader()) { event.reply("Du bist kein Leader in deinem Team").setEphemeral(true).queue(); return; }