From 22523fa5bef027994749bb74680b75a8887d8c1f Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Wed, 28 Jul 2021 18:15:40 +0200 Subject: [PATCH] Discord Ticket System --- .../bungeecore/bot/SteamwarDiscordBot.java | 8 +- .../bot/config/DiscordTicketType.java | 41 +++++++ .../bot/config/SteamwarDiscordBotConfig.java | 25 +++++ .../bot/listeners/DiscordTicketListener.java | 106 ++++++++++++++++++ .../bot/util/DiscordTicketMessage.java | 56 +++++++++ 5 files changed, 234 insertions(+), 2 deletions(-) create mode 100644 src/de/steamwar/bungeecore/bot/config/DiscordTicketType.java create mode 100644 src/de/steamwar/bungeecore/bot/listeners/DiscordTicketListener.java create mode 100644 src/de/steamwar/bungeecore/bot/util/DiscordTicketMessage.java diff --git a/src/de/steamwar/bungeecore/bot/SteamwarDiscordBot.java b/src/de/steamwar/bungeecore/bot/SteamwarDiscordBot.java index 5ae563f8..5114f085 100644 --- a/src/de/steamwar/bungeecore/bot/SteamwarDiscordBot.java +++ b/src/de/steamwar/bungeecore/bot/SteamwarDiscordBot.java @@ -22,7 +22,9 @@ 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.DiscordTicketListener; import de.steamwar.bungeecore.bot.listeners.RolesInteractionButtonListener; +import de.steamwar.bungeecore.bot.util.DiscordTicketMessage; import de.steamwar.bungeecore.bot.util.DiscordRolesMessage; import de.steamwar.bungeecore.bot.util.DiscordRulesMessage; import de.steamwar.bungeecore.sql.Event; @@ -70,8 +72,10 @@ public class SteamwarDiscordBot { }, 30, 30, TimeUnit.SECONDS); DiscordRolesMessage.sendMessage(); DiscordRulesMessage.sendMessage(); + DiscordTicketMessage.sendMessage(); new RolesInteractionButtonListener(); + new DiscordTicketListener(); } private int index = 0; @@ -83,11 +87,11 @@ public class SteamwarDiscordBot { if (event != null) { jda.getPresence().setActivity(Activity.competing("dem Event " + event.getEventName())); } else { - jda.getPresence().setActivity(Activity.playing("SteamWar.de")); + jda.getPresence().setActivity(Activity.playing("auf SteamWar.de")); } break; case 1: - jda.getPresence().setActivity(Activity.playing("Online: " + BungeeCore.get().getProxy().getOnlineCount())); + jda.getPresence().setActivity(Activity.playing("mit " + BungeeCore.get().getProxy().getOnlineCount() + " Spielern")); index = 0; return; } diff --git a/src/de/steamwar/bungeecore/bot/config/DiscordTicketType.java b/src/de/steamwar/bungeecore/bot/config/DiscordTicketType.java new file mode 100644 index 00000000..5ba4c654 --- /dev/null +++ b/src/de/steamwar/bungeecore/bot/config/DiscordTicketType.java @@ -0,0 +1,41 @@ +/* + 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.config; + +import lombok.AllArgsConstructor; +import lombok.Data; +import net.dv8tion.jda.api.entities.Emoji; +import net.dv8tion.jda.api.interactions.components.Button; +import net.dv8tion.jda.api.interactions.components.ButtonStyle; + +@Data +@AllArgsConstructor +public class DiscordTicketType { + + private String key; + private String emoji; + private String label; + private String color; + private String preMessage; + + public Button toButton() { + return Button.of(ButtonStyle.valueOf(color), key, Emoji.fromUnicode(emoji)).withLabel(label); + } +} diff --git a/src/de/steamwar/bungeecore/bot/config/SteamwarDiscordBotConfig.java b/src/de/steamwar/bungeecore/bot/config/SteamwarDiscordBotConfig.java index 9726763b..1e00925f 100644 --- a/src/de/steamwar/bungeecore/bot/config/SteamwarDiscordBotConfig.java +++ b/src/de/steamwar/bungeecore/bot/config/SteamwarDiscordBotConfig.java @@ -22,7 +22,9 @@ package de.steamwar.bungeecore.bot.config; import net.md_5.bungee.config.Configuration; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; public class SteamwarDiscordBotConfig { @@ -38,6 +40,12 @@ public class SteamwarDiscordBotConfig { public static String RULES_TITLE; public static List RULES_RULES; public static List RULES_LINKS; + public static String TICKET_CATEGORY; + public static String TICKET_CHANNEL; + public static String TICKET_MESSAGE; + public static String TICKET_CREATED; + public static String TICKET_LOG; + public static Map TICKET_TYPES; public static void loadConfig(Configuration config) { TOKEN = config.getString("token"); @@ -69,5 +77,22 @@ public class SteamwarDiscordBotConfig { RULES_LINKS.add(new DiscordRulesLink(link.getString("label"), link.getString("url"))); } + + Configuration ticketSection = config.getSection("tickets"); + TICKET_CATEGORY = ticketSection.getString("category"); + TICKET_CHANNEL = ticketSection.getString("channel"); + TICKET_MESSAGE = ticketSection.getString("message"); + TICKET_CREATED = ticketSection.getString("created"); + TICKET_LOG = ticketSection.getString("log"); + TICKET_TYPES = new HashMap<>(); + + for (String types : ticketSection.getSection("types").getKeys()) { + Configuration type = ticketSection.getSection("types").getSection(types); + TICKET_TYPES.put(types, new DiscordTicketType(types, + type.getString("emoji"), + type.getString("label"), + type.getString("color"), + type.getString("pre"))); + } } } diff --git a/src/de/steamwar/bungeecore/bot/listeners/DiscordTicketListener.java b/src/de/steamwar/bungeecore/bot/listeners/DiscordTicketListener.java new file mode 100644 index 00000000..0b9b9399 --- /dev/null +++ b/src/de/steamwar/bungeecore/bot/listeners/DiscordTicketListener.java @@ -0,0 +1,106 @@ +/* + 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.DiscordTicketType; +import de.steamwar.bungeecore.bot.config.SteamwarDiscordBotConfig; +import net.dv8tion.jda.api.EmbedBuilder; +import net.dv8tion.jda.api.MessageBuilder; +import net.dv8tion.jda.api.Permission; +import net.dv8tion.jda.api.entities.*; +import net.dv8tion.jda.api.events.interaction.GenericComponentInteractionCreateEvent; +import net.dv8tion.jda.api.interactions.InteractionType; +import net.dv8tion.jda.api.interactions.components.ActionRow; +import net.dv8tion.jda.api.interactions.components.Button; +import net.dv8tion.jda.api.requests.restaction.MessageAction; +import org.jetbrains.annotations.NotNull; + +import java.awt.Color; +import java.io.File; +import java.time.Instant; +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; + +public class DiscordTicketListener extends BasicDiscordListener { + + @Override + public void onGenericComponentInteractionCreate(@NotNull GenericComponentInteractionCreateEvent event) { + if(event.getType() == InteractionType.COMPONENT && event.getTextChannel().getParent().getId().equals(SteamwarDiscordBotConfig.TICKET_CATEGORY)) { + if(event.getTextChannel().getId().equals(SteamwarDiscordBotConfig.TICKET_CHANNEL) && SteamwarDiscordBotConfig.TICKET_TYPES.containsKey(event.getComponentId())) { + DiscordTicketType ticketType = SteamwarDiscordBotConfig.TICKET_TYPES.get(event.getComponentId()); + Category ct = event.getGuild().getCategoryById(SteamwarDiscordBotConfig.TICKET_CATEGORY); + TextChannel ticketChannel = ct.createTextChannel(event.getUser().getName() + "-" + event.getComponentId() + "-" + System.currentTimeMillis() % 1000).complete(); + ticketChannel.createPermissionOverride(event.getMember()).setAllow(Permission.VIEW_CHANNEL, + Permission.MESSAGE_WRITE, + Permission.MESSAGE_ATTACH_FILES, + Permission.MESSAGE_ADD_REACTION, + Permission.MESSAGE_READ, + Permission.MESSAGE_EMBED_LINKS, + Permission.MESSAGE_HISTORY).complete(); + + MessageBuilder messageBuilder = new MessageBuilder(); + EmbedBuilder builder = new EmbedBuilder(); + builder.setDescription(ticketType.getPreMessage()); + builder.setTitle("Steamwar Ticket"); + builder.setColor(Color.GREEN); + + Button closeButton = Button.danger("close-" + ticketChannel.getName(), "Schliesen").withEmoji(Emoji.fromUnicode("U+26A0")); + messageBuilder.setEmbeds(builder.build()); + messageBuilder.setActionRows(ActionRow.of(closeButton)); + + ticketChannel.sendMessage(messageBuilder.build()).complete(); + event.reply(SteamwarDiscordBotConfig.TICKET_CREATED.replace("%channel%", ticketChannel.getAsMention())).setEphemeral(true).complete(); + } else if(event.getComponentId().startsWith("close-")) { + TextChannel logChannel = event.getGuild().getTextChannelById(SteamwarDiscordBotConfig.TICKET_LOG); + + MessageBuilder builder = new MessageBuilder(); + StringBuilder stringBuilder = new StringBuilder(); + + new LinkedList<>(event.getTextChannel().getIterableHistory().complete()).descendingIterator().forEachRemaining(message -> { + stringBuilder.append(" ") + .append("**") + .append(message.getAuthor().getName()) + .append("**: ") + .append(message.getContentRaw()); + if(!message.getAttachments().isEmpty()) { + stringBuilder.append("\n") + .append("Files: ").append("\n"); + message.getAttachments().forEach(attachment -> stringBuilder.append(attachment.getUrl()).append("\n")); + } + + stringBuilder.append("\n"); + }); + + EmbedBuilder embedBuilder = new EmbedBuilder(); + embedBuilder.setColor(Color.GREEN); + embedBuilder.setTimestamp(Instant.now()); + embedBuilder.setTitle(event.getTextChannel().getName()); + embedBuilder.setDescription(stringBuilder); + + builder.setEmbeds(embedBuilder.build()); + + logChannel.sendMessage(builder.build()).complete(); + + event.getTextChannel().delete().reason("Closed").complete(); + } + } + } +} diff --git a/src/de/steamwar/bungeecore/bot/util/DiscordTicketMessage.java b/src/de/steamwar/bungeecore/bot/util/DiscordTicketMessage.java new file mode 100644 index 00000000..34454ebc --- /dev/null +++ b/src/de/steamwar/bungeecore/bot/util/DiscordTicketMessage.java @@ -0,0 +1,56 @@ +/* + 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.util; + +import de.steamwar.bungeecore.bot.SteamwarDiscordBot; +import de.steamwar.bungeecore.bot.config.SteamwarDiscordBotConfig; +import net.dv8tion.jda.api.EmbedBuilder; +import net.dv8tion.jda.api.MessageBuilder; +import net.dv8tion.jda.api.entities.TextChannel; +import net.dv8tion.jda.api.interactions.components.ActionRow; +import net.dv8tion.jda.api.interactions.components.Button; + +import java.awt.*; +import java.util.ArrayList; +import java.util.List; + +public class DiscordTicketMessage { + + public static void sendMessage() { + TextChannel channel = SteamwarDiscordBot.instance().getJda().getGuildById(SteamwarDiscordBotConfig.GUILD).getTextChannelById(SteamwarDiscordBotConfig.TICKET_CHANNEL); + assert channel != null; + if(channel.hasLatestMessage()) { + channel.getIterableHistory().complete().forEach(message -> message.delete().complete()); + } + + EmbedBuilder builder = new EmbedBuilder(); + builder.setDescription(SteamwarDiscordBotConfig.TICKET_MESSAGE); + builder.setTitle("Steamwar-Tickets"); + builder.setColor(Color.RED); + + List