diff --git a/src/de/steamwar/bungeecore/bot/SteamwarDiscordBot.java b/src/de/steamwar/bungeecore/bot/SteamwarDiscordBot.java index 241ce90..11a4ac6 100644 --- a/src/de/steamwar/bungeecore/bot/SteamwarDiscordBot.java +++ b/src/de/steamwar/bungeecore/bot/SteamwarDiscordBot.java @@ -20,10 +20,7 @@ package de.steamwar.bungeecore.bot; import de.steamwar.bungeecore.BungeeCore; -import de.steamwar.bungeecore.bot.commands.BanCommand; -import de.steamwar.bungeecore.bot.commands.MuteCommand; -import de.steamwar.bungeecore.bot.commands.TeamCommand; -import de.steamwar.bungeecore.bot.commands.WhoisCommand; +import de.steamwar.bungeecore.bot.commands.*; import de.steamwar.bungeecore.bot.config.SteamwarDiscordBotConfig; import de.steamwar.bungeecore.bot.events.EventManager; import de.steamwar.bungeecore.bot.events.SchematicsManager; @@ -44,6 +41,8 @@ import net.dv8tion.jda.api.utils.MemberCachePolicy; import net.md_5.bungee.api.ProxyServer; import javax.security.auth.login.LoginException; +import java.util.HashMap; +import java.util.Map; import java.util.concurrent.TimeUnit; public class SteamwarDiscordBot { @@ -66,6 +65,9 @@ public class SteamwarDiscordBot { @Getter private final JDA jda; + @Getter + private static Map discordCommandMap = new HashMap<>(); + public SteamwarDiscordBot() { INSTANCE = this; JDABuilder builder = JDABuilder.createDefault(SteamwarDiscordBotConfig.TOKEN); @@ -106,13 +108,18 @@ public class SteamwarDiscordBot { Guild guild = jda.getGuildById(SteamwarDiscordBotConfig.GUILD); guild.retrieveCommands().complete().forEach(command -> guild.deleteCommandById(command.getId()).complete()); CommandListUpdateAction commands = jda.getGuildById(SteamwarDiscordBotConfig.GUILD).updateCommands(); - commands.addCommands(new MuteCommand()); - commands.addCommands(new BanCommand()); - commands.addCommands(new WhoisCommand()); - commands.addCommands(new TeamCommand()); + addCommand(commands, new MuteCommand()); + addCommand(commands, new BanCommand()); + addCommand(commands, new WhoisCommand()); + addCommand(commands, new TeamCommand()); commands.complete(); } + private void addCommand(CommandListUpdateAction commands, BasicDiscordCommand basicDiscordCommand) { + commands.addCommands(basicDiscordCommand); + discordCommandMap.put(basicDiscordCommand.getName(), basicDiscordCommand); + } + private int index = 0; private void activity() { diff --git a/src/de/steamwar/bungeecore/bot/commands/BanCommand.java b/src/de/steamwar/bungeecore/bot/commands/BanCommand.java index 65928f0..7d3772f 100644 --- a/src/de/steamwar/bungeecore/bot/commands/BanCommand.java +++ b/src/de/steamwar/bungeecore/bot/commands/BanCommand.java @@ -19,10 +19,14 @@ package de.steamwar.bungeecore.bot.commands; +import de.steamwar.bungeecore.Message; +import de.steamwar.bungeecore.sql.SteamwarUser; +import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; import net.dv8tion.jda.api.interactions.commands.OptionType; -import net.dv8tion.jda.api.interactions.commands.build.CommandData; -public class BanCommand extends CommandData { +import java.sql.Timestamp; + +public class BanCommand extends BasicDiscordCommand { public BanCommand() { super("ban", "Banne einen Nutzer, wenn du die Rechte hast."); @@ -31,4 +35,31 @@ public class BanCommand extends CommandData { addOption(OptionType.STRING, "time", "Bis Wann", true); addOption(OptionType.STRING, "reason", "Warum", true); } + + @Override + public void run(SlashCommandEvent event) { + if (!testPermission(event)) { + return; + } + + SteamwarUser sender = SteamwarUser.get(event.getMember().getIdLong()); + SteamwarUser target = SteamwarUser.getOrCreateOfflinePlayer(event.getOption("user").getAsString()); + if (target == null) { + event.reply("Angegebener User invalide").setEphemeral(true).complete(); + return; + } + + Timestamp time = de.steamwar.bungeecore.commands.BanCommand.parseTime(null, event.getOption("time").getAsString()); + if (time == null) { + event.reply("Angegebene Zeit invalide").setEphemeral(true).complete(); + return; + } + + String msg = event.getOption("reason").getAsString(); + boolean isPerma = event.getOption("time").getAsString().equals("perma"); + + target.ban(time, msg, sender.getId(), isPerma); + Message.team("BAN_TEAM_BANNED", new Message("PREFIX"), target.getUserName(), sender.getUserName(), new Message((isPerma ? "BAN_PERMA" : "BAN_UNTIL"), time), msg); + event.reply("Erfolgreich " + target.getUserName() + (isPerma ? " permanent" : " bis " + time) + " gebannt").setEphemeral(true).complete(); + } } diff --git a/src/de/steamwar/bungeecore/bot/commands/BasicDiscordCommand.java b/src/de/steamwar/bungeecore/bot/commands/BasicDiscordCommand.java new file mode 100644 index 0000000..11506b9 --- /dev/null +++ b/src/de/steamwar/bungeecore/bot/commands/BasicDiscordCommand.java @@ -0,0 +1,55 @@ +/* + * 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.commands; + +import de.steamwar.bungeecore.sql.SteamwarUser; +import de.steamwar.bungeecore.sql.UserGroup; +import net.dv8tion.jda.api.entities.Member; +import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; +import net.dv8tion.jda.api.interactions.commands.build.CommandData; + +public abstract class BasicDiscordCommand extends CommandData { + + protected BasicDiscordCommand(String name, String description) { + super(name, description); + } + + public abstract void run(SlashCommandEvent event); + + protected SteamwarUser getSteamwarUser(SlashCommandEvent event) { + Member member = event.getMember(); + SteamwarUser steamwarUser = SteamwarUser.get(member.getIdLong()); + if (steamwarUser == null) { + event.reply("Du hast keine Verknüpfung registriert.").setEphemeral(true).complete(); + return null; + } + return steamwarUser; + } + + protected boolean testPermission(SlashCommandEvent event) { + Member member = event.getMember(); + SteamwarUser steamwarUser = SteamwarUser.get(member.getIdLong()); + if (steamwarUser == null || (!steamwarUser.getUserGroup().isTeamGroup() && steamwarUser.getUserGroup() != UserGroup.Builder)) { + event.reply("Du hast für " + event.getName() + " keine Rechte oder es existiert keine Verknüpfung für dich.").setEphemeral(true).complete(); + return false; + } + return true; + } +} diff --git a/src/de/steamwar/bungeecore/bot/commands/MuteCommand.java b/src/de/steamwar/bungeecore/bot/commands/MuteCommand.java index 4eb9d99..700baac 100644 --- a/src/de/steamwar/bungeecore/bot/commands/MuteCommand.java +++ b/src/de/steamwar/bungeecore/bot/commands/MuteCommand.java @@ -19,10 +19,15 @@ package de.steamwar.bungeecore.bot.commands; +import de.steamwar.bungeecore.Message; +import de.steamwar.bungeecore.commands.BanCommand; +import de.steamwar.bungeecore.sql.SteamwarUser; +import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; import net.dv8tion.jda.api.interactions.commands.OptionType; -import net.dv8tion.jda.api.interactions.commands.build.CommandData; -public class MuteCommand extends CommandData { +import java.sql.Timestamp; + +public class MuteCommand extends BasicDiscordCommand { public MuteCommand() { super("mute", "Mute einen Nutzer, wenn du die Rechte hast."); @@ -31,4 +36,31 @@ public class MuteCommand extends CommandData { addOption(OptionType.STRING, "time", "Bis Wann", true); addOption(OptionType.STRING, "reason", "Warum", true); } + + @Override + public void run(SlashCommandEvent event) { + if (!testPermission(event)) { + return; + } + + SteamwarUser sender = SteamwarUser.get(event.getMember().getIdLong()); + SteamwarUser target = SteamwarUser.getOrCreateOfflinePlayer(event.getOption("user").getAsString()); + if (target == null) { + event.reply("Angegebener User invalide").setEphemeral(true).complete(); + return; + } + + Timestamp time = BanCommand.parseTime(null, event.getOption("time").getAsString()); + if (time == null) { + event.reply("Angegebene Zeit invalide").setEphemeral(true).complete(); + return; + } + + String msg = event.getOption("reason").getAsString(); + boolean isPerma = event.getOption("time").getAsString().equals("perma"); + + target.mute(time, msg, sender.getId(), isPerma); + Message.team("MUTE_TEAM_MUTED", new Message("PREFIX"), target.getUserName(), sender.getUserName(), new Message((isPerma ? "BAN_PERMA" : "BAN_UNTIL"), time), msg); + event.reply("Erfolgreich " + target.getUserName() + (isPerma ? " permanent" : " bis " + time) + " gemutet").setEphemeral(true).complete(); + } } diff --git a/src/de/steamwar/bungeecore/bot/commands/TeamCommand.java b/src/de/steamwar/bungeecore/bot/commands/TeamCommand.java index 3ae288d..4871c58 100644 --- a/src/de/steamwar/bungeecore/bot/commands/TeamCommand.java +++ b/src/de/steamwar/bungeecore/bot/commands/TeamCommand.java @@ -19,11 +19,20 @@ package de.steamwar.bungeecore.bot.commands; +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.EmbedBuilder; +import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; +import net.dv8tion.jda.api.interactions.commands.OptionMapping; import net.dv8tion.jda.api.interactions.commands.OptionType; -import net.dv8tion.jda.api.interactions.commands.build.CommandData; import net.dv8tion.jda.api.interactions.commands.build.SubcommandData; -public class TeamCommand extends CommandData { +import java.awt.*; +import java.util.stream.Collectors; + +public class TeamCommand extends BasicDiscordCommand { public TeamCommand() { super("team", "Alle Team bezogenen Befehle"); @@ -32,4 +41,44 @@ public class TeamCommand extends CommandData { .addOption(OptionType.STRING, "team", "Name oder Kuerzel", false) ); } + + @Override + public void run(SlashCommandEvent event) { + SteamwarUser steamwarUser = getSteamwarUser(event); + if (steamwarUser == null) { + return; + } + + if (event.getSubcommandName() != null) { + switch (event.getSubcommandName()) { + case "info": + OptionMapping optionMapping = event.getOption("team"); + Team team; + if (optionMapping == null) { + if (steamwarUser.getTeam() == 0) { + event.reply("Du bist in keinem Team").setEphemeral(true).complete(); + return; + } else { + team = Team.get(steamwarUser.getTeam()); + } + } else { + team = Team.get(optionMapping.getAsString()); + } + if (team == null) { + event.reply("Unbekanntes Team").setEphemeral(true).complete(); + return; + } + EmbedBuilder embedBuilder = new EmbedBuilder(); + embedBuilder.setTitle("Team: " + team.getTeamName()); + embedBuilder.setColor(Color.GRAY); + embedBuilder.addField("Kuerzel", team.getTeamKuerzel(), true); + embedBuilder.addField("Leader", team.getMembers().stream().map(SteamwarUser::get).filter(SteamwarUser::isLeader).map(SteamwarUser::getUserName).collect(Collectors.joining(" ")), true); + embedBuilder.addField("Member", team.getMembers().stream().map(SteamwarUser::get).filter(user -> !user.isLeader()).map(SteamwarUser::getUserName).collect(Collectors.joining(" ")), true); + embedBuilder.addField("Events", TeamTeilnahme.getEvents(team.getTeamId()).stream().map(Event::getEventName).collect(Collectors.joining(" ")), true); + return; + default: + return; + } + } + } } diff --git a/src/de/steamwar/bungeecore/bot/commands/WhoisCommand.java b/src/de/steamwar/bungeecore/bot/commands/WhoisCommand.java index a2ea9d8..b40c049 100644 --- a/src/de/steamwar/bungeecore/bot/commands/WhoisCommand.java +++ b/src/de/steamwar/bungeecore/bot/commands/WhoisCommand.java @@ -19,14 +19,76 @@ package de.steamwar.bungeecore.bot.commands; +import de.steamwar.bungeecore.sql.Punishment; +import de.steamwar.bungeecore.sql.SteamwarUser; +import de.steamwar.bungeecore.sql.Team; +import net.dv8tion.jda.api.EmbedBuilder; +import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; import net.dv8tion.jda.api.interactions.commands.OptionType; -import net.dv8tion.jda.api.interactions.commands.build.CommandData; -public class WhoisCommand extends CommandData { +import java.sql.Timestamp; +import java.text.DecimalFormat; +import java.util.List; + +public class WhoisCommand extends BasicDiscordCommand { public WhoisCommand() { super("whois", "Der whois Befehl"); addOption(OptionType.STRING, "user", "Der Benutzer", true); } + + @Override + public void run(SlashCommandEvent event) { + if (!testPermission(event)) { + return; + } + + String s = event.getOption("user").getAsString(); + + SteamwarUser user = SteamwarUser.get(s); + if (user == null) { + try { + int id = Integer.parseInt(s); + user = SteamwarUser.get(id); + } catch (NumberFormatException ignored) { + // Ignored + } + } + if (user == null) { + try { + long id = Long.parseLong(s); + user = SteamwarUser.get(id); + } catch (NumberFormatException ignored) { + // Ignored + } + } + + if (user == null) { + event.reply("Der angegebene Spieler ist unbekannt").setEphemeral(true).complete(); + return; + } + + EmbedBuilder embedBuilder = new EmbedBuilder(); + embedBuilder.setTitle("Whois: " + user.getUserName()); + StringBuilder st = new StringBuilder(); + st.append("UUID: ").append(user.getUuid()).append("\n"); + st.append("ID: ").append(user.getId()).append("\n"); + if (user.getDiscordId() != null) { + st.append("DiscordID: ").append(user.getDiscordId()).append("\n"); + } + Timestamp timestamp = user.getFirstjoin(); + st.append("Beigetreten am: ").append(timestamp == null ? "0000-00-00 00:00:00" : timestamp.toString()).append("\n"); + st.append("Online Time: ").append(new DecimalFormat("###.##").format(user.getOnlinetime() / (double) 3600)).append("h\n"); + Team team = Team.get(user.getTeam()); + st.append("Team: [").append(team.getTeamKuerzel()).append("] ").append(team.getTeamName()); + embedBuilder.addField("Daten:", st.toString(), false); + + List punishmentList = Punishment.getAllPunishmentsOfPlayer(user.getId()); + for (Punishment punishment : punishmentList) { + embedBuilder.addField(punishment.getType().name() + " von " + SteamwarUser.get(punishment.getPunisher()).getUserName(), "Von: " + punishment.getBantime(punishment.getStartTime(), false) + "\nBis: " + punishment.getBantime(punishment.getEndTime(), punishment.isPerma()) + "\nGrund: " + punishment.getReason(), true); + } + + event.replyEmbeds(embedBuilder.build()).setEphemeral(true).complete(); + } } diff --git a/src/de/steamwar/bungeecore/bot/listeners/DiscordTicketListener.java b/src/de/steamwar/bungeecore/bot/listeners/DiscordTicketListener.java index 9207ed5..1ac4920 100644 --- a/src/de/steamwar/bungeecore/bot/listeners/DiscordTicketListener.java +++ b/src/de/steamwar/bungeecore/bot/listeners/DiscordTicketListener.java @@ -33,11 +33,10 @@ import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent; 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.interactions.components.selections.SelectionMenu; import net.md_5.bungee.api.chat.ClickEvent; import org.jetbrains.annotations.NotNull; -import java.awt.Color; +import java.awt.*; import java.time.Instant; import java.util.LinkedList; diff --git a/src/de/steamwar/bungeecore/bot/listeners/SlashCommandListener.java b/src/de/steamwar/bungeecore/bot/listeners/SlashCommandListener.java index 4dda95d..565ca44 100644 --- a/src/de/steamwar/bungeecore/bot/listeners/SlashCommandListener.java +++ b/src/de/steamwar/bungeecore/bot/listeners/SlashCommandListener.java @@ -19,136 +19,14 @@ package de.steamwar.bungeecore.bot.listeners; -import de.steamwar.bungeecore.Message; -import de.steamwar.bungeecore.commands.BanCommand; -import de.steamwar.bungeecore.sql.Punishment; -import de.steamwar.bungeecore.sql.SteamwarUser; -import de.steamwar.bungeecore.sql.Team; -import de.steamwar.bungeecore.sql.UserGroup; -import net.dv8tion.jda.api.EmbedBuilder; -import net.dv8tion.jda.api.entities.Member; +import de.steamwar.bungeecore.bot.SteamwarDiscordBot; import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; import org.jetbrains.annotations.NotNull; -import java.sql.Timestamp; -import java.text.DecimalFormat; -import java.util.List; - public class SlashCommandListener extends BasicDiscordListener { @Override public void onSlashCommand(@NotNull SlashCommandEvent event) { - switch (event.getName()) { - case "mute": - case "ban": - banAndMute(event); - break; - case "whois": - whois(event); - break; - case "team": - team(event); - break; - default: - return; - } - } - - private void banAndMute(SlashCommandEvent event) { - if (!testPermission(event)) { - return; - } - - SteamwarUser sender = SteamwarUser.get(event.getMember().getIdLong()); - SteamwarUser target = SteamwarUser.getOrCreateOfflinePlayer(event.getOption("user").getAsString()); - if (target == null) { - event.reply("Angegebener User invalide").setEphemeral(true).complete(); - return; - } - - Timestamp time = BanCommand.parseTime(null, event.getOption("time").getAsString()); - if (time == null) { - event.reply("Angegebene Zeit invalide").setEphemeral(true).complete(); - return; - } - - String msg = event.getOption("reason").getAsString(); - boolean isPerma = event.getOption("time").getAsString().equals("perma"); - - if (event.getName().equals("ban")) { - target.ban(time, msg, sender.getId(), isPerma); - Message.team("BAN_TEAM_BANNED", new Message("PREFIX"), target.getUserName(), sender.getUserName(), new Message((isPerma ? "BAN_PERMA" : "BAN_UNTIL"), time), msg); - } else { - target.mute(time, msg, sender.getId(), isPerma); - Message.team("MUTE_TEAM_MUTED", new Message("PREFIX"), target.getUserName(), sender.getUserName(), new Message((isPerma ? "BAN_PERMA" : "BAN_UNTIL"), time), msg); - } - event.reply("Erfolgreich " + target.getUserName() + (isPerma ? " permanent" : " bis " + time) + (event.getName().equals("ban") ? " gebannt" : " gemutet")).setEphemeral(true).complete(); - } - - private void whois(SlashCommandEvent event) { - if (!testPermission(event)) { - return; - } - - String s = event.getOption("user").getAsString(); - - SteamwarUser user = SteamwarUser.get(s); - if (user == null) { - try { - int id = Integer.parseInt(s); - user = SteamwarUser.get(id); - } catch (NumberFormatException ignored) { - // Ignored - } - } - if (user == null) { - try { - long id = Long.parseLong(s); - user = SteamwarUser.get(id); - } catch (NumberFormatException ignored) { - // Ignored - } - } - - if (user == null) { - event.reply("Der angegebene Spieler ist unbekannt").setEphemeral(true).complete(); - return; - } - - EmbedBuilder embedBuilder = new EmbedBuilder(); - embedBuilder.setTitle("Whois: " + user.getUserName()); - StringBuilder st = new StringBuilder(); - st.append("UUID: ").append(user.getUuid()).append("\n"); - st.append("ID: ").append(user.getId()).append("\n"); - if (user.getDiscordId() != null) { - st.append("DiscordID: ").append(user.getDiscordId()).append("\n"); - } - Timestamp timestamp = user.getFirstjoin(); - st.append("Beigetreten am: ").append(timestamp == null ? "0000-00-00 00:00:00" : timestamp.toString()).append("\n"); - st.append("Online Time: ").append(new DecimalFormat("###.##").format(user.getOnlinetime() / (double) 3600)).append("h\n"); - Team team = Team.get(user.getTeam()); - st.append("Team: [").append(team.getTeamKuerzel()).append("] ").append(team.getTeamName()); - embedBuilder.addField("Daten:", st.toString(), false); - - List punishmentList = Punishment.getAllPunishmentsOfPlayer(user.getId()); - for (Punishment punishment : punishmentList) { - embedBuilder.addField(punishment.getType().name() + " von " + SteamwarUser.get(punishment.getPunisher()).getUserName(), "Von: " + punishment.getBantime(punishment.getStartTime(), false) + "\nBis: " + punishment.getBantime(punishment.getEndTime(), punishment.isPerma()) + "\nGrund: " + punishment.getReason(), true); - } - - event.replyEmbeds(embedBuilder.build()).setEphemeral(true).complete(); - } - - private void team(SlashCommandEvent event) { - - } - - private boolean testPermission(SlashCommandEvent event) { - Member member = event.getMember(); - SteamwarUser steamwarUser = SteamwarUser.get(member.getIdLong()); - if (steamwarUser == null || (!steamwarUser.getUserGroup().isTeamGroup() && steamwarUser.getUserGroup() != UserGroup.Builder)) { - event.reply("Du hast für " + event.getName() + " keine Rechte oder es existiert keine Verknüpfung für dich.").setEphemeral(true).complete(); - return false; - } - return true; + SteamwarDiscordBot.getDiscordCommandMap().get(event.getName()).run(event); } }