SteamWar/BungeeCore
Archiviert
13
2

Fix command fixup, Discord command registration
Alle Prüfungen waren erfolgreich
SteamWarCI Build successful

Signed-off-by: Lixfel <git-5w3l@lixfel.de>
Dieser Commit ist enthalten in:
Lixfel 2024-06-29 08:10:14 +02:00
Ursprung 276587b07f
Commit 6350a99392
2 geänderte Dateien mit 60 neuen und 24 gelöschten Zeilen

Datei anzeigen

@ -19,6 +19,9 @@
package de.steamwar.velocitycore.discord; 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.VelocityCore;
import de.steamwar.velocitycore.discord.channels.*; import de.steamwar.velocitycore.discord.channels.*;
import de.steamwar.velocitycore.discord.listeners.ChannelListener; 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.DiscordTeamEvent;
import de.steamwar.velocitycore.discord.listeners.DiscordTicketHandler; import de.steamwar.velocitycore.discord.listeners.DiscordTicketHandler;
import de.steamwar.velocitycore.discord.util.AuthManager; 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 lombok.Getter;
import net.dv8tion.jda.api.*; import net.dv8tion.jda.api.*;
import net.dv8tion.jda.api.entities.*; 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.commands.build.OptionData;
import net.dv8tion.jda.api.interactions.components.ActionRow; import net.dv8tion.jda.api.interactions.components.ActionRow;
import net.dv8tion.jda.api.interactions.components.Button; 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.requests.restaction.CommandListUpdateAction;
import net.dv8tion.jda.api.utils.MemberCachePolicy; import net.dv8tion.jda.api.utils.MemberCachePolicy;
import javax.security.auth.login.LoginException; import javax.security.auth.login.LoginException;
import java.awt.*; import java.awt.*;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.*;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.function.Consumer; import java.util.function.Consumer;
@ -177,18 +174,33 @@ public class DiscordBot {
new DiscordSchemUpload() new DiscordSchemUpload()
); );
commandSetup(jda.retrieveCommands(), jda.updateCommands()); commandSetup(jda.retrieveCommands().complete(), jda.updateCommands());
} }
private void commandSetup(RestAction<List<Command>> existingCommands, CommandListUpdateAction updateCommands) { private final OptionData commandArgument = new OptionData(OptionType.STRING, ARGUMENT_NAME, "Command arguments", false);
existingCommands.complete().forEach(command -> command.delete().complete()); private void commandSetup(List<Command> existing, CommandListUpdateAction updateCommands) {
Set<String> correctCommands = new HashSet<>();
for(Command command : existing) {
if(!commands.containsKey(command.getName())) {
command.delete().complete();
continue;
}
List<Command.Option> options = command.getOptions();
if(options.size() != 1 || options.get(0).getType() != OptionType.STRING)
command.editCommand().clearOptions().addOptions(commandArgument).complete();
correctCommands.add(command.getName());
}
updateCommands updateCommands
.addCommands(getCommands() .addCommands(commands
.keySet().stream() .keySet().stream()
.filter(command -> !correctCommands.contains(command))
.filter(command -> command.matches("^[\\w-]+$")) .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)) .toArray(CommandData[]::new))
.complete(); .queue();
} }
private boolean activityToggle = false; private boolean activityToggle = false;

Datei anzeigen

@ -19,23 +19,25 @@
package de.steamwar.velocitycore.listeners; package de.steamwar.velocitycore.listeners;
import com.velocitypowered.api.event.PostOrder;
import com.velocitypowered.api.event.Subscribe; 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.PlayerChatEvent;
import com.velocitypowered.api.event.player.TabCompleteEvent; import com.velocitypowered.api.event.player.TabCompleteEvent;
import com.velocitypowered.api.proxy.Player; 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.Chatter;
import de.steamwar.messages.ChatterGroup; import de.steamwar.messages.ChatterGroup;
import de.steamwar.messages.Message; import de.steamwar.messages.Message;
import de.steamwar.messages.PlayerChatter; import de.steamwar.messages.PlayerChatter;
import de.steamwar.network.packets.server.PingPacket; import de.steamwar.network.packets.server.PingPacket;
import de.steamwar.persistent.Servertype;
import de.steamwar.persistent.Subserver;
import de.steamwar.sql.*; 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.Arrays;
import java.util.List; import java.util.List;
@ -47,6 +49,28 @@ public class ChatListener extends BasicListener {
private static final List<String> rankedModes = ArenaMode.getAllModes().stream().filter(ArenaMode::isRanked).map(ArenaMode::getSchemType).toList(); private static final List<String> 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 @Subscribe
public void onChatEvent(PlayerChatEvent e) { public void onChatEvent(PlayerChatEvent e) {
Player player = e.getPlayer(); Player player = e.getPlayer();
@ -61,7 +85,7 @@ public class ChatListener extends BasicListener {
return; return;
} }
if (isCommand(player, message)) if (isMistypedCommand(player, message))
return; return;
Subserver subserver = Subserver.getSubserver(player); 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); String command = message.substring(1);
boolean isCommand = message.startsWith("/") || (message.startsWith("7") && command.split(" ", 2)[0].matches("[7/]?[A-Za-z]+")); boolean isCommand = message.startsWith("7") && command.split(" ", 2)[0].matches("[7/]?[A-Za-z]+");
if(isCommand && Boolean.FALSE.equals(VelocityCore.getProxy().getCommandManager().executeImmediatelyAsync(player, command).join())) { if(isCommand && Boolean.FALSE.equals(VelocityCore.getProxy().getCommandManager().executeAsync(player, command).join())) {
if(command.startsWith("7")) if(command.startsWith("7"))
command = "/" + command.substring(1); command = "/" + command.substring(1);
message = "/" + command; message = "/" + command;