Add admin console discord command (closes #1) and fix discord command arguments not being properly sent to the executed discord command.

This commit is contained in:
business-goose 2022-03-21 16:21:17 +00:00
parent dcac3e3822
commit 9741bae1fe
2 changed files with 52 additions and 3 deletions

View file

@ -1,5 +1,6 @@
package me.StevenLawson.TotalFreedomMod.discord.command; 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.HelpCommand;
import me.StevenLawson.TotalFreedomMod.discord.commands.ListCommand; import me.StevenLawson.TotalFreedomMod.discord.commands.ListCommand;
import me.StevenLawson.TotalFreedomMod.discord.commands.TPSCommand; import me.StevenLawson.TotalFreedomMod.discord.commands.TPSCommand;
@ -19,15 +20,19 @@ public class DiscordCommandManager {
public List<ExecutableDiscordCommand> commands = new ArrayList<>(); public List<ExecutableDiscordCommand> commands = new ArrayList<>();
public void init() { 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 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 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)); commands.add(new HelpCommand("help", "Displays the help command", "Help", false));
} }
public void parse(String content, User user, Server server, TextChannel channel, String prefix) { public void parse(String content, User user, Server server, TextChannel channel, String prefix) {
List<String> args = new ArrayList<>(Arrays.asList(content.split(prefix))); List<String> args = new ArrayList<>(Arrays.asList(content.split(" ")));
args.remove(0); String commandOrAlias = args.remove(0).split(prefix)[1];
String commandOrAlias = args.remove(0);
for (ExecutableDiscordCommand command : commands) { for (ExecutableDiscordCommand command : commands) {
if(command.command.equalsIgnoreCase(commandOrAlias) || command.aliases.contains(commandOrAlias.toLowerCase())) { if(command.command.equalsIgnoreCase(commandOrAlias) || command.aliases.contains(commandOrAlias.toLowerCase())) {

View file

@ -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<String> aliases, boolean isAdmin) {
super(command, description, category, aliases, isAdmin);
}
@Override
public MessageBuilder execute(User user, List<String> 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);
}
}
}