From 6350a993921a135c19d4424dfcba71fd4090b5a6 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Sat, 29 Jun 2024 08:10:14 +0200 Subject: [PATCH] Fix command fixup, Discord command registration Signed-off-by: Lixfel --- .../velocitycore/discord/DiscordBot.java | 38 +++++++++------ .../velocitycore/listeners/ChatListener.java | 46 ++++++++++++++----- 2 files changed, 60 insertions(+), 24 deletions(-) diff --git a/src/de/steamwar/velocitycore/discord/DiscordBot.java b/src/de/steamwar/velocitycore/discord/DiscordBot.java index 120ff88..3928da7 100644 --- a/src/de/steamwar/velocitycore/discord/DiscordBot.java +++ b/src/de/steamwar/velocitycore/discord/DiscordBot.java @@ -19,6 +19,9 @@ package de.steamwar.velocitycore.discord; +import de.steamwar.command.SWCommand; +import de.steamwar.messages.Chatter; +import de.steamwar.sql.Event; import de.steamwar.velocitycore.VelocityCore; import de.steamwar.velocitycore.discord.channels.*; import de.steamwar.velocitycore.discord.listeners.ChannelListener; @@ -26,9 +29,6 @@ import de.steamwar.velocitycore.discord.listeners.DiscordSchemUpload; import de.steamwar.velocitycore.discord.listeners.DiscordTeamEvent; import de.steamwar.velocitycore.discord.listeners.DiscordTicketHandler; import de.steamwar.velocitycore.discord.util.AuthManager; -import de.steamwar.command.SWCommand; -import de.steamwar.messages.Chatter; -import de.steamwar.sql.Event; import lombok.Getter; import net.dv8tion.jda.api.*; import net.dv8tion.jda.api.entities.*; @@ -40,16 +40,13 @@ import net.dv8tion.jda.api.interactions.commands.build.CommandData; import net.dv8tion.jda.api.interactions.commands.build.OptionData; import net.dv8tion.jda.api.interactions.components.ActionRow; import net.dv8tion.jda.api.interactions.components.Button; -import net.dv8tion.jda.api.requests.RestAction; import net.dv8tion.jda.api.requests.restaction.CommandListUpdateAction; import net.dv8tion.jda.api.utils.MemberCachePolicy; import javax.security.auth.login.LoginException; import java.awt.*; -import java.util.Arrays; -import java.util.HashMap; import java.util.List; -import java.util.Map; +import java.util.*; import java.util.concurrent.TimeUnit; import java.util.function.Consumer; @@ -177,18 +174,33 @@ public class DiscordBot { new DiscordSchemUpload() ); - commandSetup(jda.retrieveCommands(), jda.updateCommands()); + commandSetup(jda.retrieveCommands().complete(), jda.updateCommands()); } - private void commandSetup(RestAction> existingCommands, CommandListUpdateAction updateCommands) { - existingCommands.complete().forEach(command -> command.delete().complete()); + private final OptionData commandArgument = new OptionData(OptionType.STRING, ARGUMENT_NAME, "Command arguments", false); + private void commandSetup(List existing, CommandListUpdateAction updateCommands) { + Set correctCommands = new HashSet<>(); + for(Command command : existing) { + if(!commands.containsKey(command.getName())) { + command.delete().complete(); + continue; + } + + List options = command.getOptions(); + if(options.size() != 1 || options.get(0).getType() != OptionType.STRING) + command.editCommand().clearOptions().addOptions(commandArgument).complete(); + + correctCommands.add(command.getName()); + } + updateCommands - .addCommands(getCommands() + .addCommands(commands .keySet().stream() + .filter(command -> !correctCommands.contains(command)) .filter(command -> command.matches("^[\\w-]+$")) - .map(command -> new CommandData(command, "SteamWar Command").addOptions(new OptionData(OptionType.STRING, ARGUMENT_NAME, "SteamWar arguments", false))) + .map(command -> new CommandData(command, command).addOptions(commandArgument)) .toArray(CommandData[]::new)) - .complete(); + .queue(); } private boolean activityToggle = false; diff --git a/src/de/steamwar/velocitycore/listeners/ChatListener.java b/src/de/steamwar/velocitycore/listeners/ChatListener.java index 2c871bc..a5fbb7a 100644 --- a/src/de/steamwar/velocitycore/listeners/ChatListener.java +++ b/src/de/steamwar/velocitycore/listeners/ChatListener.java @@ -19,23 +19,25 @@ package de.steamwar.velocitycore.listeners; +import com.velocitypowered.api.event.PostOrder; import com.velocitypowered.api.event.Subscribe; +import com.velocitypowered.api.event.command.CommandExecuteEvent; import com.velocitypowered.api.event.player.PlayerChatEvent; import com.velocitypowered.api.event.player.TabCompleteEvent; import com.velocitypowered.api.proxy.Player; -import de.steamwar.velocitycore.ArenaMode; -import de.steamwar.persistent.Servertype; -import de.steamwar.persistent.Subserver; -import de.steamwar.velocitycore.VelocityCore; -import de.steamwar.velocitycore.commands.PunishmentCommand; -import de.steamwar.velocitycore.discord.DiscordBot; -import de.steamwar.velocitycore.network.NetworkSender; import de.steamwar.messages.Chatter; import de.steamwar.messages.ChatterGroup; import de.steamwar.messages.Message; import de.steamwar.messages.PlayerChatter; import de.steamwar.network.packets.server.PingPacket; +import de.steamwar.persistent.Servertype; +import de.steamwar.persistent.Subserver; import de.steamwar.sql.*; +import de.steamwar.velocitycore.ArenaMode; +import de.steamwar.velocitycore.VelocityCore; +import de.steamwar.velocitycore.commands.PunishmentCommand; +import de.steamwar.velocitycore.discord.DiscordBot; +import de.steamwar.velocitycore.network.NetworkSender; import java.util.Arrays; import java.util.List; @@ -47,6 +49,28 @@ public class ChatListener extends BasicListener { private static final List rankedModes = ArenaMode.getAllModes().stream().filter(ArenaMode::isRanked).map(ArenaMode::getSchemType).toList(); + @Subscribe(order = PostOrder.FIRST) + public void fixCommands(CommandExecuteEvent e) { + String command = e.getCommand(); + if(command.startsWith("7")) { + command = "/" + command.substring(1); + + CommandExecuteEvent.CommandResult result = e.getResult(); + if(result.isForwardToServer()) + result = CommandExecuteEvent.CommandResult.forwardToServer(command); + else if(result.isAllowed()) + result = CommandExecuteEvent.CommandResult.command(command); + + e.setResult(result); + } + } + + @Subscribe(order = PostOrder.LAST) + public void logCommands(CommandExecuteEvent e) { + if(e.getResult().isAllowed()) + VelocityCore.getLogger().log(Level.INFO, "%s -> executed command %s".formatted(e.getCommandSource(), e.getCommand())); + } + @Subscribe public void onChatEvent(PlayerChatEvent e) { Player player = e.getPlayer(); @@ -61,7 +85,7 @@ public class ChatListener extends BasicListener { return; } - if (isCommand(player, message)) + if (isMistypedCommand(player, message)) return; Subserver subserver = Subserver.getSubserver(player); @@ -74,10 +98,10 @@ public class ChatListener extends BasicListener { } } - private static boolean isCommand(Player player, String message) { + private static boolean isMistypedCommand(Player player, String message) { String command = message.substring(1); - boolean isCommand = message.startsWith("/") || (message.startsWith("7") && command.split(" ", 2)[0].matches("[7/]?[A-Za-z]+")); - if(isCommand && Boolean.FALSE.equals(VelocityCore.getProxy().getCommandManager().executeImmediatelyAsync(player, command).join())) { + boolean isCommand = message.startsWith("7") && command.split(" ", 2)[0].matches("[7/]?[A-Za-z]+"); + if(isCommand && Boolean.FALSE.equals(VelocityCore.getProxy().getCommandManager().executeAsync(player, command).join())) { if(command.startsWith("7")) command = "/" + command.substring(1); message = "/" + command;