From 850a0d212e316f84c59cf691aebaff73263ccecd Mon Sep 17 00:00:00 2001 From: yoyosource Date: Thu, 29 Jul 2021 22:20:15 +0200 Subject: [PATCH] Add BanCommand and MuteCommand --- .../bungeecore/bot/SteamwarDiscordBot.java | 5 +++ .../bungeecore/bot/commands/BanCommand.java | 2 +- .../bungeecore/bot/commands/MuteCommand.java | 2 +- .../bot/listeners/SlashCommandListener.java | 35 ++++++++++++++++++- .../bungeecore/commands/BanCommand.java | 4 ++- .../bungeecore/commands/WhoisCommand.java | 8 +++++ 6 files changed, 52 insertions(+), 4 deletions(-) diff --git a/src/de/steamwar/bungeecore/bot/SteamwarDiscordBot.java b/src/de/steamwar/bungeecore/bot/SteamwarDiscordBot.java index 7c5fe8e..dda49ca 100644 --- a/src/de/steamwar/bungeecore/bot/SteamwarDiscordBot.java +++ b/src/de/steamwar/bungeecore/bot/SteamwarDiscordBot.java @@ -36,6 +36,7 @@ import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.JDABuilder; import net.dv8tion.jda.api.OnlineStatus; import net.dv8tion.jda.api.entities.Activity; +import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.hooks.ListenerAdapter; import net.dv8tion.jda.api.requests.restaction.CommandListUpdateAction; import net.dv8tion.jda.api.utils.MemberCachePolicy; @@ -99,6 +100,10 @@ public class SteamwarDiscordBot { serverTeamChatListener = new ServerTeamChatListener(); new SlashCommandListener(); + jda.retrieveCommands().complete().forEach(command -> jda.deleteCommandById(command.getId()).complete()); + + 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()); diff --git a/src/de/steamwar/bungeecore/bot/commands/BanCommand.java b/src/de/steamwar/bungeecore/bot/commands/BanCommand.java index 598a997..d0924b0 100644 --- a/src/de/steamwar/bungeecore/bot/commands/BanCommand.java +++ b/src/de/steamwar/bungeecore/bot/commands/BanCommand.java @@ -28,7 +28,7 @@ public class BanCommand extends CommandData { super("ban", "Ban a user. Requires permission to ban users."); addOption(OptionType.STRING, "user", "The user to bam", true); - addOption(OptionType.STRING, "duration", "The time to ban until", true); + addOption(OptionType.STRING, "time", "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 d5adc97..18f6074 100644 --- a/src/de/steamwar/bungeecore/bot/commands/MuteCommand.java +++ b/src/de/steamwar/bungeecore/bot/commands/MuteCommand.java @@ -28,7 +28,7 @@ public class MuteCommand extends CommandData { super("mute", "Mute a user. Requires permission to mute users."); addOption(OptionType.STRING, "user", "The user to mute", true); - addOption(OptionType.STRING, "duration", "The time to mute until", true); + addOption(OptionType.STRING, "time", "The time to mute until", true); addOption(OptionType.STRING, "reason", "The reason to mute", true); } } diff --git a/src/de/steamwar/bungeecore/bot/listeners/SlashCommandListener.java b/src/de/steamwar/bungeecore/bot/listeners/SlashCommandListener.java index def9188..ee08993 100644 --- a/src/de/steamwar/bungeecore/bot/listeners/SlashCommandListener.java +++ b/src/de/steamwar/bungeecore/bot/listeners/SlashCommandListener.java @@ -19,6 +19,8 @@ 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; @@ -53,7 +55,30 @@ public class SlashCommandListener extends BasicDiscordListener { return; } - System.out.println(event.getName() + " " + event.getOptions()); + 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) { @@ -72,6 +97,14 @@ public class SlashCommandListener extends BasicDiscordListener { // 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(); diff --git a/src/de/steamwar/bungeecore/commands/BanCommand.java b/src/de/steamwar/bungeecore/commands/BanCommand.java index 6b3bcea..1b3f38d 100644 --- a/src/de/steamwar/bungeecore/commands/BanCommand.java +++ b/src/de/steamwar/bungeecore/commands/BanCommand.java @@ -74,7 +74,9 @@ public class BanCommand extends BasicCommand { Date parsedDate = dateFormat.parse(arg.split("_")[0]); return new java.sql.Timestamp(parsedDate.getTime()); }catch(ParseException exception){ - Message.send("INVALID_TIME", sender); + if (sender != null) { + Message.send("INVALID_TIME", sender); + } return null; } } diff --git a/src/de/steamwar/bungeecore/commands/WhoisCommand.java b/src/de/steamwar/bungeecore/commands/WhoisCommand.java index 9360eb4..089ad82 100644 --- a/src/de/steamwar/bungeecore/commands/WhoisCommand.java +++ b/src/de/steamwar/bungeecore/commands/WhoisCommand.java @@ -54,6 +54,14 @@ public class WhoisCommand extends BasicCommand { user = SteamwarUser.get(id); }catch (NumberFormatException ignored) {} } + if (user == null) { + try { + long id = Long.parseLong(args[0]); + user = SteamwarUser.get(id); + } catch (NumberFormatException ignored) { + // Ignored + } + } if(user == null) { Message.send("UNKNOWN_PLAYER", player);