From a0f9764a4b9d04e07c608be0531a6110fd9e5747 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Thu, 29 Jul 2021 20:27:43 +0200 Subject: [PATCH] Add SlashCommandListener Add WhoisCommand --- .../bungeecore/bot/SteamwarDiscordBot.java | 5 +- .../bungeecore/bot/commands/BanCommand.java | 1 + .../bungeecore/bot/commands/MuteCommand.java | 1 + .../bungeecore/bot/commands/WhoisCommand.java | 32 +++++ .../bot/listeners/SlashCommandListener.java | 115 ++++++++++++++++++ .../bungeecore/commands/WhoisCommand.java | 3 + .../steamwar/messages/BungeeCore.properties | 1 + 7 files changed, 157 insertions(+), 1 deletion(-) create mode 100644 src/de/steamwar/bungeecore/bot/commands/WhoisCommand.java create mode 100644 src/de/steamwar/bungeecore/bot/listeners/SlashCommandListener.java diff --git a/src/de/steamwar/bungeecore/bot/SteamwarDiscordBot.java b/src/de/steamwar/bungeecore/bot/SteamwarDiscordBot.java index ac50e539..7c5fe8e6 100644 --- a/src/de/steamwar/bungeecore/bot/SteamwarDiscordBot.java +++ b/src/de/steamwar/bungeecore/bot/SteamwarDiscordBot.java @@ -22,6 +22,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.WhoisCommand; import de.steamwar.bungeecore.bot.config.SteamwarDiscordBotConfig; import de.steamwar.bungeecore.bot.events.EventManager; import de.steamwar.bungeecore.bot.events.SchematicsManager; @@ -96,10 +97,12 @@ public class SteamwarDiscordBot { announcementListener = new AnnouncementListener(); ingameChatListener = new IngameChatListener(); serverTeamChatListener = new ServerTeamChatListener(); + new SlashCommandListener(); - CommandListUpdateAction commands = jda.updateCommands(); + CommandListUpdateAction commands = jda.getGuildById(SteamwarDiscordBotConfig.GUILD).updateCommands(); commands.addCommands(new MuteCommand()); commands.addCommands(new BanCommand()); + commands.addCommands(new WhoisCommand()); commands.complete(); } diff --git a/src/de/steamwar/bungeecore/bot/commands/BanCommand.java b/src/de/steamwar/bungeecore/bot/commands/BanCommand.java index e73dd82a..598a997e 100644 --- a/src/de/steamwar/bungeecore/bot/commands/BanCommand.java +++ b/src/de/steamwar/bungeecore/bot/commands/BanCommand.java @@ -29,5 +29,6 @@ public class BanCommand extends CommandData { addOption(OptionType.STRING, "user", "The user to bam", true); addOption(OptionType.STRING, "duration", "The time to ban until", true); + addOption(OptionType.STRING, "reason", "The reason to ban", true); } } diff --git a/src/de/steamwar/bungeecore/bot/commands/MuteCommand.java b/src/de/steamwar/bungeecore/bot/commands/MuteCommand.java index 2a6cb819..d5adc978 100644 --- a/src/de/steamwar/bungeecore/bot/commands/MuteCommand.java +++ b/src/de/steamwar/bungeecore/bot/commands/MuteCommand.java @@ -29,5 +29,6 @@ public class MuteCommand extends CommandData { addOption(OptionType.STRING, "user", "The user to mute", true); addOption(OptionType.STRING, "duration", "The time to mute until", true); + addOption(OptionType.STRING, "reason", "The reason to mute", true); } } diff --git a/src/de/steamwar/bungeecore/bot/commands/WhoisCommand.java b/src/de/steamwar/bungeecore/bot/commands/WhoisCommand.java new file mode 100644 index 00000000..390a4008 --- /dev/null +++ b/src/de/steamwar/bungeecore/bot/commands/WhoisCommand.java @@ -0,0 +1,32 @@ +/* + * 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 net.dv8tion.jda.api.interactions.commands.OptionType; +import net.dv8tion.jda.api.interactions.commands.build.CommandData; + +public class WhoisCommand extends CommandData { + + public WhoisCommand() { + super("whois", "Check a users id and stuff"); + + addOption(OptionType.STRING, "user", "The user to get", true); + } +} diff --git a/src/de/steamwar/bungeecore/bot/listeners/SlashCommandListener.java b/src/de/steamwar/bungeecore/bot/listeners/SlashCommandListener.java new file mode 100644 index 00000000..efcc2fd9 --- /dev/null +++ b/src/de/steamwar/bungeecore/bot/listeners/SlashCommandListener.java @@ -0,0 +1,115 @@ +/* + * 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.Message; +import de.steamwar.bungeecore.sql.Punishment; +import de.steamwar.bungeecore.sql.SteamwarUser; +import de.steamwar.bungeecore.sql.Team; +import jdk.nashorn.internal.runtime.regexp.joni.constants.TargetInfo; +import net.dv8tion.jda.api.EmbedBuilder; +import net.dv8tion.jda.api.entities.Member; +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; + default: + return; + } + } + + private void banAndMute(SlashCommandEvent event) { + if (!testPermission(event)) { + return; + } + + System.out.println(event.getName() + " " + event.getOptions()); + } + + 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) { + 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(), false); + } + + event.replyEmbeds(embedBuilder.build()).setEphemeral(true).complete(); + } + + private boolean testPermission(SlashCommandEvent event) { + Member member = event.getMember(); + SteamwarUser steamwarUser = SteamwarUser.get(member.getIdLong()); + if (steamwarUser == null || !steamwarUser.getUserGroup().isTeamGroup()) { + 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/commands/WhoisCommand.java b/src/de/steamwar/bungeecore/commands/WhoisCommand.java index 5bcb2b6e..9360eb4f 100644 --- a/src/de/steamwar/bungeecore/commands/WhoisCommand.java +++ b/src/de/steamwar/bungeecore/commands/WhoisCommand.java @@ -67,6 +67,9 @@ public class WhoisCommand extends BasicCommand { Message.send("WHOIS_USERNAME", player, user.getUserName()); Message.send("WHOIS_UUID", player, Message.parse("WHOIS_UUID_HOVER", player), new ClickEvent(ClickEvent.Action.COPY_TO_CLIPBOARD, user.getUuid().toString()), user.getUuid().toString()); Message.send("WHOIS_ID", player, user.getId()); + if (user.getDiscordId() != null) { + Message.send("WHOIS_DISCORD_ID", player, user.getDiscordId()); + } Timestamp firstJoin = user.getFirstjoin(); if(firstJoin == null) Message.send("WHOIS_JOINED_FIRST", player, "0000-00-00 00:00:00"); diff --git a/src/de/steamwar/messages/BungeeCore.properties b/src/de/steamwar/messages/BungeeCore.properties index 3e191d0d..0d637325 100644 --- a/src/de/steamwar/messages/BungeeCore.properties +++ b/src/de/steamwar/messages/BungeeCore.properties @@ -505,6 +505,7 @@ WHOIS_USERNAME=§7Username§8: §e{0} WHOIS_UUID=§7UUID§8: §e{0} WHOIS_UUID_HOVER=§eUUID Kopieren WHOIS_ID=§7ID§8: §e{0} +WHOIS_DISCORD_ID=§7Discord-D§8: §e{0} WHOIS_JOINED_FIRST=§7Beigetreten am§8: §e{0} WHOIS_HOURS_PLAYED=§7Online Time§8: §e{0}h WHOIS_TEAM=§7Team§8: §e[§{0}{1}§e] {2}