From 9146c38450a318d71b9c1cb1d2edf1884d738075 Mon Sep 17 00:00:00 2001 From: ChomeNS <95471003+ChomeNS@users.noreply.github.com> Date: Fri, 31 Mar 2023 15:03:04 +0700 Subject: [PATCH] make the bot worse :( --- .../chipmunk/chayapak/chomens_bot/Bot.java | 18 ++++--- .../chomens_bot/commands/EndCommand.java | 43 +++++++++++++++++ .../chomens_bot/commands/SudoAllCommand.java | 47 +++++++++++++++++++ .../plugins/ChatCommandHandlerPlugin.java | 2 +- .../plugins/CommandHandlerPlugin.java | 20 ++++---- .../chomens_bot/plugins/ConsolePlugin.java | 2 +- .../chomens_bot/plugins/DiscordPlugin.java | 14 ++++-- 7 files changed, 120 insertions(+), 26 deletions(-) create mode 100644 src/main/java/land/chipmunk/chayapak/chomens_bot/commands/EndCommand.java create mode 100644 src/main/java/land/chipmunk/chayapak/chomens_bot/commands/SudoAllCommand.java diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/Bot.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/Bot.java index 34a9c0b..dc844a1 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/Bot.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/Bot.java @@ -65,6 +65,8 @@ public class Bot { try { DiscordPlugin.readyLatch().await(); + + Thread.sleep(2000); // prob the worst way to fix this thing } catch (InterruptedException ignored) { System.exit(1); } this.chat = new ChatPlugin(this); @@ -84,16 +86,12 @@ public class Bot { this.bruhify = new BruhifyPlugin(this); this.grepLog = new GrepLogPlugin(this); - try { - Thread.sleep(1000); // real - } catch (InterruptedException ignored) { - System.exit(1); - } - reconnect(); } public void reconnect () { + if (session != null) session.disconnect("Disconnect"); + if (_username == null) username = RandomStringUtils.randomAlphabetic(8); else username = _username; @@ -105,15 +103,15 @@ public class Bot { @Override public void packetReceived(Session session, Packet packet) { + for (SessionListener listener : listeners) { + listener.packetReceived(session, packet); + } + if (packet instanceof ClientboundLoginPacket) { for (SessionListener listener : listeners) { listener.connected(new ConnectedEvent(session)); } } - - for (SessionListener listener : listeners) { - listener.packetReceived(session, packet); - } } @Override diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/EndCommand.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/EndCommand.java new file mode 100644 index 0000000..7bcaaad --- /dev/null +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/EndCommand.java @@ -0,0 +1,43 @@ +package land.chipmunk.chayapak.chomens_bot.commands; + +import land.chipmunk.chayapak.chomens_bot.Bot; +import land.chipmunk.chayapak.chomens_bot.command.Command; +import land.chipmunk.chayapak.chomens_bot.command.CommandContext; +import net.kyori.adventure.text.Component; + +import java.util.ArrayList; +import java.util.List; + +public class EndCommand implements Command { + public String name() { return "end"; } + + public String description() { + return "End/Restarts the bot"; + } + + public List usage() { + final List usages = new ArrayList<>(); + usages.add(""); + + return usages; + } + + public List alias() { + final List aliases = new ArrayList<>(); + aliases.add("restart"); + + return aliases; + } + + public int trustLevel() { + return 1; + } + + public Component execute(CommandContext context, String[] args, String[] fullArgs) { + final Bot bot = context.bot(); + + bot.reconnect(); + + return Component.text("success"); + } +} diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/SudoAllCommand.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/SudoAllCommand.java new file mode 100644 index 0000000..4ce49f6 --- /dev/null +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/SudoAllCommand.java @@ -0,0 +1,47 @@ +package land.chipmunk.chayapak.chomens_bot.commands; + +import land.chipmunk.chayapak.chomens_bot.Bot; +import land.chipmunk.chayapak.chomens_bot.chatParsers.data.MutablePlayerListEntry; +import land.chipmunk.chayapak.chomens_bot.command.Command; +import land.chipmunk.chayapak.chomens_bot.command.CommandContext; +import net.kyori.adventure.text.Component; + +import java.util.ArrayList; +import java.util.List; + +// ayunsudo renamed. +public class SudoAllCommand implements Command { + public String name() { return "sudoall"; } + + public String description() { + return "Sudoes everyone"; + } + + public List usage() { + final List usages = new ArrayList<>(); + usages.add("<{c:message|command}>"); + + return usages; + } + + public List alias() { + final List aliases = new ArrayList<>(); + aliases.add("ayunsudo"); + + return aliases; + } + + public int trustLevel() { + return 1; + } + + public Component execute(CommandContext context, String[] args, String[] fullArgs) { + final Bot bot = context.bot(); + + for (MutablePlayerListEntry entry : bot.players().list()) { + bot.core().run("essentials:sudo " + entry.profile().getName() + " " + String.join(" ", args)); + } + + return Component.text("success"); + } +} diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/ChatCommandHandlerPlugin.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/ChatCommandHandlerPlugin.java index a634e94..20ede9e 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/ChatCommandHandlerPlugin.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/ChatCommandHandlerPlugin.java @@ -55,7 +55,7 @@ public class ChatCommandHandlerPlugin extends ChatPlugin.ChatListener { final PlayerCommandContext context = new PlayerCommandContext(bot, displayName, prefix, selector, message.sender(), bot.hashing().hash(), bot.hashing().ownerHash()); - final Component output = bot.commandHandler().executeCommand(commandString, context, false, bot.hashing().hash(), bot.hashing().ownerHash(), null); + final Component output = bot.commandHandler().executeCommand(commandString, context, true, false, bot.hashing().hash(), bot.hashing().ownerHash(), null); final String textOutput = ((TextComponent) output).content(); if (!textOutput.equals("success")) { diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/CommandHandlerPlugin.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/CommandHandlerPlugin.java index b56464e..b958e95 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/CommandHandlerPlugin.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/CommandHandlerPlugin.java @@ -44,13 +44,15 @@ public class CommandHandlerPlugin { registerCommand(new TimeCommand()); registerCommand(new BruhifyCommand()); registerCommand(new GrepLogCommand()); + registerCommand(new SudoAllCommand()); + registerCommand(new EndCommand()); } public void registerCommand (Command command) { commands.add(command); } - public Component executeCommand (String input, CommandContext context, boolean discord, String hash, String ownerHash, MessageReceivedEvent event) { + public Component executeCommand (String input, CommandContext context, boolean inGame, boolean discord, String hash, String ownerHash, MessageReceivedEvent event) { final String[] splitInput = input.split("\\s+"); final String commandName = splitInput[0]; @@ -64,15 +66,15 @@ public class CommandHandlerPlugin { final String[] fullArgs = Arrays.copyOfRange(splitInput, 1, splitInput.length); final int longestUsageIndex = getLongestUsageIndex(command.usage()); final String usage = command.usage().get(longestUsageIndex); - final int minimumArgs = getMinimumArgs(usage, discord, command.trustLevel()); - final int maximumArgs = getMaximumArgs(usage, discord, command.trustLevel()); + final int minimumArgs = getMinimumArgs(usage, inGame, command.trustLevel()); + final int maximumArgs = getMaximumArgs(usage, inGame, command.trustLevel()); if (fullArgs.length < minimumArgs) return Component.text("Excepted minimum of " + minimumArgs + " argument(s), got " + fullArgs.length).color(NamedTextColor.RED); if (fullArgs.length > maximumArgs && !usage.contains("{")) return Component.text("Too much arguments, expected " + maximumArgs + " max").color(NamedTextColor.RED); String userHash = ""; if (trustLevel > 0 && splitInput.length >= 2) userHash = splitInput[1]; - final String[] args = Arrays.copyOfRange(splitInput, (trustLevel > 0 && !discord) ? 2 : 1, splitInput.length); + final String[] args = Arrays.copyOfRange(splitInput, (trustLevel > 0 && inGame) ? 2 : 1, splitInput.length); // fix shit random messy code if (command.trustLevel() > 0) { @@ -109,7 +111,7 @@ public class CommandHandlerPlugin { exception.printStackTrace(); final String stackTrace = ExceptionUtils.getStackTrace(exception); - if (!discord) { + if (inGame) { return Component .text("An error occurred while trying to execute the command, hover here for more details", NamedTextColor.RED) .hoverEvent( @@ -138,7 +140,7 @@ public class CommandHandlerPlugin { return longestIndex; } - private static int getMinimumArgs(String usage, boolean discord, int trustLevel) { + private static int getMinimumArgs(String usage, boolean inGame, int trustLevel) { int count = 0; for (int i = 0; i < usage.length(); i++) { if (usage.charAt(i) == '<') { @@ -146,18 +148,18 @@ public class CommandHandlerPlugin { } } if (usage.contains("")) count--; // bad fix? - if ((discord && trustLevel > 0)) count--; + if ((!inGame && trustLevel > 0)) count--; return count; } - private static int getMaximumArgs(String usage, boolean discord, int trustLevel) { + private static int getMaximumArgs(String usage, boolean inGame, int trustLevel) { int count = 0; for (int i = 0; i < usage.length(); i++) { if (usage.charAt(i) == '<' || usage.charAt(i) == '[') { count++; } } - if (discord && trustLevel > 0) count++; + if (!inGame && trustLevel > 0) count++; return count; } } diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/ConsolePlugin.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/ConsolePlugin.java index 7595d01..7c832c8 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/ConsolePlugin.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/ConsolePlugin.java @@ -84,7 +84,7 @@ public class ConsolePlugin { if (line.startsWith(prefix)) { final ConsoleCommandContext context = new ConsoleCommandContext(bot, prefix, "h", "o"); // ? should the hashes be hardcoded? - final Component output = bot.commandHandler().executeCommand(line.substring(prefix.length()), context, false, "h", "o", null); + final Component output = bot.commandHandler().executeCommand(line.substring(prefix.length()), context, false, false, "h", "o", null); final String textOutput = ((TextComponent) output).content(); if (!textOutput.equals("success")) { diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/DiscordPlugin.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/DiscordPlugin.java index 835123d..602948b 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/DiscordPlugin.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/DiscordPlugin.java @@ -1,15 +1,17 @@ package land.chipmunk.chayapak.chomens_bot.plugins; -import com.github.steveice10.packetlib.event.session.ConnectedEvent; +import com.github.steveice10.mc.protocol.packet.ingame.clientbound.ClientboundLoginPacket; +import com.github.steveice10.packetlib.Session; import com.github.steveice10.packetlib.event.session.DisconnectedEvent; import com.github.steveice10.packetlib.event.session.SessionAdapter; -import land.chipmunk.chayapak.chomens_bot.util.EscapeCodeBlock; -import lombok.Getter; +import com.github.steveice10.packetlib.packet.Packet; import land.chipmunk.chayapak.chomens_bot.Bot; import land.chipmunk.chayapak.chomens_bot.Configuration; import land.chipmunk.chayapak.chomens_bot.Main; import land.chipmunk.chayapak.chomens_bot.command.DiscordCommandContext; import land.chipmunk.chayapak.chomens_bot.util.ComponentUtilities; +import land.chipmunk.chayapak.chomens_bot.util.EscapeCodeBlock; +import lombok.Getter; import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.JDABuilder; import net.dv8tion.jda.api.entities.Message; @@ -71,7 +73,9 @@ public class DiscordPlugin { bot.addListener(new SessionAdapter() { @Override - public void connected(ConnectedEvent event) { + public void packetReceived (Session session, Packet packet) { + if (!(packet instanceof ClientboundLoginPacket)) return; + boolean channelAlreadyAddedListeners = alreadyAddedListeners.getOrDefault(channelId, false); sendMessageInstantly("Successfully connected to: " + "`" + bot.host() + ":" + bot.port() + "`", channelId); @@ -92,7 +96,7 @@ public class DiscordPlugin { if (message.startsWith(prefix)) { final DiscordCommandContext context = new DiscordCommandContext(bot, prefix, event, null, null); - final Component output = bot.commandHandler().executeCommand(message.substring(prefix.length()), context, true, null, null, event); + final Component output = bot.commandHandler().executeCommand(message.substring(prefix.length()), context, false, true, null, null, event); final String textOutput = ((TextComponent) output).content(); if (!textOutput.equals("success")) {