diff --git a/src/main/java/me/StevenLawson/TotalFreedomMod/discord/command/DiscordCommandManager.java b/src/main/java/me/StevenLawson/TotalFreedomMod/discord/command/DiscordCommandManager.java index 71ae80b..c53a687 100644 --- a/src/main/java/me/StevenLawson/TotalFreedomMod/discord/command/DiscordCommandManager.java +++ b/src/main/java/me/StevenLawson/TotalFreedomMod/discord/command/DiscordCommandManager.java @@ -1,5 +1,6 @@ package me.StevenLawson.TotalFreedomMod.discord.command; +import me.StevenLawson.TotalFreedomMod.discord.commands.AdminConsoleCommand; import me.StevenLawson.TotalFreedomMod.discord.commands.HelpCommand; import me.StevenLawson.TotalFreedomMod.discord.commands.ListCommand; import me.StevenLawson.TotalFreedomMod.discord.commands.TPSCommand; @@ -19,15 +20,19 @@ public class DiscordCommandManager { public List commands = new ArrayList<>(); public void init() { + + // Server Commands commands.add(new ListCommand("list", "Gives a list of online players.", "Server Commands", Collections.singletonList("l"), false)); commands.add(new TPSCommand("tps", "Lag information regarding the server.", "Server Commands", false)); + commands.add(new AdminConsoleCommand("adminconsole", "Execute admin commands from discord.", "Server Commands", Collections.singletonList("ac"),true)); + + // Help commands.add(new HelpCommand("help", "Displays the help command", "Help", false)); } public void parse(String content, User user, Server server, TextChannel channel, String prefix) { - List args = new ArrayList<>(Arrays.asList(content.split(prefix))); - args.remove(0); - String commandOrAlias = args.remove(0); + List args = new ArrayList<>(Arrays.asList(content.split(" "))); + String commandOrAlias = args.remove(0).split(prefix)[1]; for (ExecutableDiscordCommand command : commands) { if(command.command.equalsIgnoreCase(commandOrAlias) || command.aliases.contains(commandOrAlias.toLowerCase())) { diff --git a/src/main/java/me/StevenLawson/TotalFreedomMod/discord/commands/AdminConsoleCommand.java b/src/main/java/me/StevenLawson/TotalFreedomMod/discord/commands/AdminConsoleCommand.java new file mode 100644 index 0000000..95cc14a --- /dev/null +++ b/src/main/java/me/StevenLawson/TotalFreedomMod/discord/commands/AdminConsoleCommand.java @@ -0,0 +1,44 @@ +package me.StevenLawson.TotalFreedomMod.discord.commands; + +import me.StevenLawson.TotalFreedomMod.TotalFreedomMod; +import me.StevenLawson.TotalFreedomMod.discord.command.ExecutableDiscordCommand; +import me.StevenLawson.TotalFreedomMod.discord.sender.DiscordCommandSender; +import me.StevenLawson.TotalFreedomMod.util.SynchronousUtil; +import org.bukkit.Bukkit; +import org.bukkit.scheduler.BukkitRunnable; +import org.javacord.api.entity.message.MessageBuilder; +import org.javacord.api.entity.message.embed.Embed; +import org.javacord.api.entity.message.embed.EmbedBuilder; +import org.javacord.api.entity.user.User; + +import java.awt.*; +import java.util.List; + +public class AdminConsoleCommand extends ExecutableDiscordCommand { + private final EmbedBuilder success = new EmbedBuilder().setTitle("Success").setColor(Color.GREEN).setDescription("Command sent."); + private final EmbedBuilder error = new EmbedBuilder().setTitle("Command error").setColor(Color.RED).setDescription("An error occured. Check your DMs for more info."); + + public AdminConsoleCommand(String command, String description, String category, List aliases, boolean isAdmin) { + super(command, description, category, aliases, isAdmin); + } + + @Override + public MessageBuilder execute(User user, List args) { + try { + new BukkitRunnable() { + + @Override + public void run() { + DiscordCommandSender discordCommandSender = new DiscordCommandSender(user); + String command = String.join(" ", args); + Bukkit.dispatchCommand(discordCommandSender, command); + } + }.runTask(TotalFreedomMod.plugin); + + return new MessageBuilder().addEmbed(success); + } catch (Exception e) { + user.sendMessage(String.valueOf(e)); + return new MessageBuilder().addEmbed(error); + } + } +}