diff --git a/src/de/steamwar/bungeecore/bot/SteamwarDiscordBot.java b/src/de/steamwar/bungeecore/bot/SteamwarDiscordBot.java index 381720e9..ba2ff108 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 d40a80c1..506e8b7f 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 00000000..5c262936 --- /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(); + } + } + } +}