From 9fede92907a9763ec8961a2b39d613714e7607a2 Mon Sep 17 00:00:00 2001 From: 132ikl <132@ikl.sh> Date: Tue, 26 May 2020 20:40:13 -0400 Subject: [PATCH] Upgrade to Discord4J 3.0.14, add all_roles config option, change command format --- .idea/artifacts/Server_jar.xml | 52 ++++++++--------- ...> com_discord4j_discord4j_core_3_0_14.xml} | 56 +++++++++---------- .../networking/io/MessageReceiver.java | 3 +- .../networking/io/PlayerStatusMessage.java | 4 +- Server/Server.iml | 2 +- .../polychat/server/DiscordHandler.java | 16 ++++-- .../moddedminecraft/polychat/server/Main.java | 2 +- .../polychat/server/PrintMessageQueue.java | 3 +- .../server/command/CommandManager.java | 40 +++++++++++-- .../server/command/MinecraftCommand.java | 16 +++++- .../polychat/server/info/OnlineServers.java | 16 +++--- 11 files changed, 126 insertions(+), 84 deletions(-) rename .idea/libraries/{com_discord4j_discord4j_core_3_0_10.xml => com_discord4j_discord4j_core_3_0_14.xml} (57%) diff --git a/.idea/artifacts/Server_jar.xml b/.idea/artifacts/Server_jar.xml index eb8cf90..04e2482 100644 --- a/.idea/artifacts/Server_jar.xml +++ b/.idea/artifacts/Server_jar.xml @@ -4,34 +4,34 @@ - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + - - + + - - + + diff --git a/.idea/libraries/com_discord4j_discord4j_core_3_0_10.xml b/.idea/libraries/com_discord4j_discord4j_core_3_0_14.xml similarity index 57% rename from .idea/libraries/com_discord4j_discord4j_core_3_0_10.xml rename to .idea/libraries/com_discord4j_discord4j_core_3_0_14.xml index 8e3d079..5dd3de0 100644 --- a/.idea/libraries/com_discord4j_discord4j_core_3_0_10.xml +++ b/.idea/libraries/com_discord4j_discord4j_core_3_0_14.xml @@ -1,35 +1,35 @@ - - + + - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + - - + + - - + + diff --git a/NetworkLibrary/src/club/moddedminecraft/polychat/networking/io/MessageReceiver.java b/NetworkLibrary/src/club/moddedminecraft/polychat/networking/io/MessageReceiver.java index 0d5c57a..a30e093 100644 --- a/NetworkLibrary/src/club/moddedminecraft/polychat/networking/io/MessageReceiver.java +++ b/NetworkLibrary/src/club/moddedminecraft/polychat/networking/io/MessageReceiver.java @@ -24,6 +24,7 @@ import java.io.IOException; import java.io.InterruptedIOException; import java.net.Socket; +import java.net.SocketException; public final class MessageReceiver { private final Socket socket; @@ -82,7 +83,7 @@ private void receiverThread() { System.err.println("[PolyChat] Warning: Illegal message id: " + messageId); } } - } catch (InterruptedIOException | EOFException ignored) { + } catch (InterruptedIOException | EOFException | SocketException ignored) { } catch (IOException e) { e.printStackTrace(); } diff --git a/NetworkLibrary/src/club/moddedminecraft/polychat/networking/io/PlayerStatusMessage.java b/NetworkLibrary/src/club/moddedminecraft/polychat/networking/io/PlayerStatusMessage.java index e32d8c3..bcca4fd 100644 --- a/NetworkLibrary/src/club/moddedminecraft/polychat/networking/io/PlayerStatusMessage.java +++ b/NetworkLibrary/src/club/moddedminecraft/polychat/networking/io/PlayerStatusMessage.java @@ -29,10 +29,10 @@ public class PlayerStatusMessage extends AbstractMessage { private final String userName, serverID, formattedPrefix; private final boolean joined, silent; - public PlayerStatusMessage(String userName, String serverID, String prefixJson, boolean joined, boolean silent) { + public PlayerStatusMessage(String userName, String serverID, String formattedJson, boolean joined, boolean silent) { this.userName = userName; this.serverID = serverID; - this.formattedPrefix = prefixJson; + this.formattedPrefix = formattedJson; this.joined = joined; this.silent = silent; } diff --git a/Server/Server.iml b/Server/Server.iml index 2875aea..1b7fff3 100644 --- a/Server/Server.iml +++ b/Server/Server.iml @@ -10,6 +10,6 @@ - + \ No newline at end of file diff --git a/Server/src/club/moddedminecraft/polychat/server/DiscordHandler.java b/Server/src/club/moddedminecraft/polychat/server/DiscordHandler.java index 3053643..0ca13ea 100644 --- a/Server/src/club/moddedminecraft/polychat/server/DiscordHandler.java +++ b/Server/src/club/moddedminecraft/polychat/server/DiscordHandler.java @@ -106,15 +106,18 @@ public void registerCommands() throws FileNotFoundException, NoSuchMethodExcepti ArrayList commandChannels = (ArrayList) yamlObj.remove("channels"); manager.setChannels(commandChannels); - for (Object entryObj : yamlObj.entrySet()) { - Map.Entry entry = (Map.Entry) entryObj; + ArrayList prefixRoles = (ArrayList) yamlObj.remove("all_roles"); + manager.addPrefixRoles(prefixRoles); + + for (Object entryObj : (ArrayList) yamlObj.get("commands")) { + LinkedHashMap entry = (LinkedHashMap) entryObj; HashMap argMap = new HashMap<>(); - for (Object mapObj : entry.getValue()) { + for (Object mapObj : (ArrayList) entry.values().iterator().next()) { argMap.putAll((HashMap) mapObj); } - String name = entry.getKey(); + String name = (String) entry.keySet().iterator().next(); // kinda ugly but way too deep down the rabbit hole String type = argMap.remove("type"); Class element = COMMAND_TYPES.get(type); @@ -143,8 +146,9 @@ public boolean processCommand(Message message) { } public void processMessage(Message message) { - ChatMessage discordMessage = new ChatMessage(message.getAuthorAsMember().block().getDisplayName() + ":", formatMessage(message), "empty"); - System.out.println(discordMessage.getMessage()); + String author = message.getAuthorAsMember().block().getDisplayName() + ":"; + ChatMessage discordMessage = new ChatMessage(author, formatMessage(message), "empty"); + System.out.println(String.format("[Discord] %s %s", author, discordMessage.getMessage())); Main.chatServer.sendMessage(discordMessage); } diff --git a/Server/src/club/moddedminecraft/polychat/server/Main.java b/Server/src/club/moddedminecraft/polychat/server/Main.java index f33919e..774c27a 100644 --- a/Server/src/club/moddedminecraft/polychat/server/Main.java +++ b/Server/src/club/moddedminecraft/polychat/server/Main.java @@ -157,7 +157,7 @@ public static boolean handleConfig() { //Initializes the connection to the discord API using the API token public static void initDiscord() { - discordClient = new DiscordClientBuilder(config.getProperty("api_token")).build(); + discordClient = DiscordClientBuilder.create(config.getProperty("api_token")).build(); discordHandler.registerEventSubscribers(discordClient); discordClient.login().block(); } diff --git a/Server/src/club/moddedminecraft/polychat/server/PrintMessageQueue.java b/Server/src/club/moddedminecraft/polychat/server/PrintMessageQueue.java index 6c4a2f2..74e18f7 100644 --- a/Server/src/club/moddedminecraft/polychat/server/PrintMessageQueue.java +++ b/Server/src/club/moddedminecraft/polychat/server/PrintMessageQueue.java @@ -137,10 +137,9 @@ private String formatMessage(String message) { while (userMentionMatcher.find()) { for (int i = 0; i <= userMentionMatcher.groupCount(); i++) { String userMention = userMentionMatcher.group(i); + // Remove @ and () String name = userMention.substring(2, (userMention.indexOf(')'))); - System.out.println(userMention); - System.out.println(name); Member member = Main.getMemberByName(name); if (member != null) { diff --git a/Server/src/club/moddedminecraft/polychat/server/command/CommandManager.java b/Server/src/club/moddedminecraft/polychat/server/command/CommandManager.java index eca72ad..96f039b 100644 --- a/Server/src/club/moddedminecraft/polychat/server/command/CommandManager.java +++ b/Server/src/club/moddedminecraft/polychat/server/command/CommandManager.java @@ -20,15 +20,22 @@ package club.moddedminecraft.polychat.server.command; +import club.moddedminecraft.polychat.server.DiscordHandler; +import club.moddedminecraft.polychat.server.Main; +import discord4j.core.object.entity.Member; import discord4j.core.object.entity.Message; +import discord4j.core.object.entity.Role; import discord4j.core.object.entity.TextChannel; +import reactor.core.publisher.Flux; import java.util.ArrayList; +import java.util.Arrays; public class CommandManager { - private ArrayList cmdList = new ArrayList<>(); + private final ArrayList cmdList = new ArrayList<>(); private ArrayList channels = new ArrayList<>(); private String prefix; + private final ArrayList allPrefixRoles = new ArrayList<>(); public void setPrefix(String prefix) { this.prefix = prefix; @@ -42,18 +49,35 @@ public void register(ArrayList collection) { cmdList.addAll(collection); } - public void setChannels(ArrayList channels) { - this.channels = channels; - } - public ArrayList getChannels() { return channels; } + public void setChannels(ArrayList channels) { + this.channels = channels; + } + public ArrayList getCommandList() { return cmdList; } + public void addPrefixRoles(ArrayList roles) { + allPrefixRoles.addAll(roles); + } + + public boolean isAllAuthorized(Member user) { + if (allPrefixRoles.isEmpty()) { + return true; + } + Flux userRoles = user.getRoles(); + for(Role role : userRoles.toIterable()){ + if(allPrefixRoles.contains(role.getName())){ + return true; + } + } + return false; + } + public String run(Message message) { TextChannel textChannel = message.getChannel().ofType(TextChannel.class).block(); @@ -69,7 +93,11 @@ public String run(Message message) { // get everything after first word (args) for (Command cmd : cmdList) { if (cmd.getName().equals(cmdName)) { - if (cmd instanceof RoleCommand) { + if (cmd instanceof MinecraftCommand) { + boolean authorized = isAllAuthorized(message.getAuthorAsMember().block()); + ((MinecraftCommand) cmd).setAllAuthorized(authorized); + return ((MinecraftCommand) cmd).verifyAndRun(message.getAuthorAsMember().block(), args, textChannel.getName()); + } else if (cmd instanceof RoleCommand) { return ((RoleCommand) cmd).verifyAndRun(message.getAuthorAsMember().block(), args, textChannel.getName()); } else { return cmd.run(args, textChannel.getName()); diff --git a/Server/src/club/moddedminecraft/polychat/server/command/MinecraftCommand.java b/Server/src/club/moddedminecraft/polychat/server/command/MinecraftCommand.java index f274c48..ea081e8 100644 --- a/Server/src/club/moddedminecraft/polychat/server/command/MinecraftCommand.java +++ b/Server/src/club/moddedminecraft/polychat/server/command/MinecraftCommand.java @@ -23,6 +23,7 @@ import club.moddedminecraft.polychat.networking.io.CommandMessage; import club.moddedminecraft.polychat.server.Main; import club.moddedminecraft.polychat.server.info.OnlineServer; +import discord4j.core.object.entity.Role; import java.util.ArrayList; import java.util.Arrays; @@ -35,6 +36,7 @@ public class MinecraftCommand extends RoleCommand { private final String defaultcmd; private final int argCount; private String channel; + private boolean allAuthorized; public MinecraftCommand(String name, Map args) { super(name, args); @@ -42,6 +44,10 @@ public MinecraftCommand(String name, Map args) { this.argCount = calculateParameters(defaultcmd); } + public void setAllAuthorized(boolean allAuthorized) { + this.allAuthorized = allAuthorized; + } + public int calculateParameters(String command) { Pattern pattern = Pattern.compile("(\\$\\d+)"); Matcher matcher = pattern.matcher(command); @@ -51,17 +57,21 @@ public int calculateParameters(String command) { public String run(String[] inputArgs, String channel) { if (inputArgs.length < 1) { - return "Error running command: Server prefix required"; + return "Error: Server prefix required"; } String serverID = inputArgs[0]; ArrayList executeServers = new ArrayList<>(); if (serverID.equals("")) { - executeServers.addAll(Main.serverInfo.getServers()); + if (allAuthorized) { + executeServers.addAll(Main.serverInfo.getServers()); + } else { + return "Error: You do not have permission to use !"; + } } else { OnlineServer server = Main.serverInfo.getServerNormalized(serverID); if (server == null) { - return "Error running command: server prefix " + serverID + " does not exist."; + return "Error: server prefix " + serverID + " does not exist."; } executeServers.add(server); } diff --git a/Server/src/club/moddedminecraft/polychat/server/info/OnlineServers.java b/Server/src/club/moddedminecraft/polychat/server/info/OnlineServers.java index efb15b6..c651164 100644 --- a/Server/src/club/moddedminecraft/polychat/server/info/OnlineServers.java +++ b/Server/src/club/moddedminecraft/polychat/server/info/OnlineServers.java @@ -56,7 +56,7 @@ public OnlineServer getServerNormalized(String serverID) { //Adds a server to the list public void serverConnected(String serverID, String serverName, String serverAddress, int maxPlayers, MessageBus messageBus) { - OnlineServer toRemove = serverMap.get(serverID); + OnlineServer toRemove = getServerNormalized(serverID); if (toRemove != null) { onlineServers.remove(toRemove); } @@ -67,7 +67,7 @@ public void serverConnected(String serverID, String serverName, String serverAdd //Marks a server as online public void serverOnline(String serverID) { - OnlineServer server = serverMap.get(serverID); + OnlineServer server = getServerNormalized(serverID); if (server != null) { server.setStarted(); } @@ -75,29 +75,29 @@ public void serverOnline(String serverID) { //Removes a server as it went offline public void serverOffline(String serverID) { - OnlineServer toRemove = serverMap.get(serverID); + OnlineServer toRemove = getServerNormalized(serverID); onlineServers.remove(toRemove); serverMap.remove(serverID); } public void updatePlayerList(String serverID, ArrayList playerList) { - OnlineServer server = serverMap.get(serverID); + OnlineServer server = getServerNormalized(serverID); if (server != null) { server.updatePlayerList(playerList); } } public void playerJoin(String serverID, String username) { - OnlineServer server = serverMap.get(serverID); + OnlineServer server = getServerNormalized(serverID); if (server != null) { - serverMap.get(serverID).onlinePlayers.add(username); + server.onlinePlayers.add(username); } } public void playerLeave(String serverID, String username) { - OnlineServer server = serverMap.get(serverID); + OnlineServer server = getServerNormalized(serverID); if (server != null) { - serverMap.get(serverID).onlinePlayers.remove(username); + server.onlinePlayers.remove(username); } }