From e66990da294c0a7088f77128ad1c2ef161b0ce93 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Tue, 7 Sep 2021 23:55:07 +0200 Subject: [PATCH 1/4] 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 2/4] 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 3/4] 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 4/4] 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(); }