Make a discord command handler

This commit is contained in:
business-goose 2022-03-21 14:24:30 +00:00
parent 06499c69b1
commit 23fdf715f5
10 changed files with 383 additions and 38 deletions

View file

@ -5,7 +5,7 @@ import me.StevenLawson.TotalFreedomMod.admin.AdminList;
import me.StevenLawson.TotalFreedomMod.announcer.Announcer;
import me.StevenLawson.TotalFreedomMod.ban.BanManager;
import me.StevenLawson.TotalFreedomMod.ban.PermbanList;
import me.StevenLawson.TotalFreedomMod.bridge.DiscordBridge;
import me.StevenLawson.TotalFreedomMod.discord.bridge.DiscordBridge;
import me.StevenLawson.TotalFreedomMod.command.CommandBlocker;
import me.StevenLawson.TotalFreedomMod.commands.CommandHandler;
import me.StevenLawson.TotalFreedomMod.commands.CommandLoader;

View file

@ -1,31 +1,30 @@
package me.StevenLawson.TotalFreedomMod.bridge;
package me.StevenLawson.TotalFreedomMod.discord.bridge;
import com.earth2me.essentials.User;
import me.StevenLawson.TotalFreedomMod.Log;
import me.StevenLawson.TotalFreedomMod.config.ConfigurationEntry;
import me.StevenLawson.TotalFreedomMod.config.MainConfig;
import me.StevenLawson.TotalFreedomMod.player.PlayerList;
import me.StevenLawson.TotalFreedomMod.player.PlayerRank;
import me.StevenLawson.TotalFreedomMod.discord.command.DiscordCommandManager;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.HoverEvent;
import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.javacord.api.DiscordApi;
import org.javacord.api.DiscordApiBuilder;
import org.javacord.api.entity.channel.TextChannel;
import org.javacord.api.entity.message.MessageAttachment;
import org.javacord.api.entity.message.MessageAuthor;
import org.javacord.api.entity.message.embed.EmbedBuilder;
import org.javacord.api.entity.server.Server;
import org.javacord.api.entity.user.User;
import java.util.*;
import java.util.Optional;
import java.util.regex.Pattern;
public class DiscordBridge {
private static DiscordApi DISCORD_API;
private static TextChannel CHANNEL;
public static DiscordCommandManager COMMAND_MANAGER;
public static void load() {
@ -48,43 +47,36 @@ public class DiscordBridge {
}
CHANNEL = channelFuture.get();
COMMAND_MANAGER = new DiscordCommandManager();
COMMAND_MANAGER.init();
CHANNEL.addMessageCreateListener((message) -> {
String content = message.getMessageContent();
String prefix = MainConfig.getString(ConfigurationEntry.DISCORD_PREFIX);
MessageAuthor author = message.getMessage().getAuthor();
if (author.isBotUser()) return;
if (content.equalsIgnoreCase(String.format("%sl", MainConfig.getString(ConfigurationEntry.DISCORD_PREFIX)))) {
EmbedBuilder builder = new EmbedBuilder()
.setTitle(String.format("Player List - %s", MainConfig.getString(ConfigurationEntry.SERVER_NAME)))
.setDescription(String.format("There are %s / %s online players", Bukkit.getOnlinePlayers().size(), Bukkit.getMaxPlayers()));
if (author.isBotUser() || !message.isServerMessage()) return;
Optional<Server> server = message.getServer();
Optional<User> user = author.asUser();
List<PlayerRank> inGameRanks = new ArrayList<>();
if(prefix == null) {
Log.severe("Bot prefix does not exist. Stopping bot...");
stop();
return;
}
for (Player player : Bukkit.getOnlinePlayers()) {
User essentialsUser = EssentialsBridge.getEssentialsUser(player.getDisplayName());
if(!server.isPresent()) {
Log.warning("Discord server wasn't present in message, this may be a sign you've not properly configured the intents for your bot.");
return;
}
if(essentialsUser != null) {
if(essentialsUser.isVanished()) continue;
}
if(!user.isPresent()) {
Log.warning("Unable to get user of message author. This may be a sign you've not properly configured the intents for your bot.");
return;
}
PlayerRank rank = PlayerRank.fromSender(player);
if(!inGameRanks.contains(rank)) inGameRanks.add(rank);
}
Collections.sort(inGameRanks);
Collections.reverse(inGameRanks);
for (PlayerRank inGameRank : inGameRanks) {
List<String> inGame = inGameRank.getInGameUsernames();
if(inGame.size() > 0) {
builder.addField(String.format("%s (%s)", inGameRank.getPlural(), inGame.size()), String.join(", ", inGame));
}
}
CHANNEL.sendMessage(builder);
if (content.toLowerCase().startsWith(prefix)) {
COMMAND_MANAGER.parse(content, user.get(), server.get(), message.getChannel(), prefix);
} else {
String format = MainConfig.getString(ConfigurationEntry.DISCORD_FORMAT);
format = format.replace("{TAG}", author.getDiscriminatedName());

View file

@ -0,0 +1,26 @@
package me.StevenLawson.TotalFreedomMod.discord.command;
import org.javacord.api.entity.message.Message;
import org.javacord.api.entity.message.MessageBuilder;
import org.javacord.api.entity.server.Server;
import org.javacord.api.entity.user.User;
import java.util.List;
public interface DiscordCommand {
/**
* Can the user execute the command?
* @param user The user who is attempting execution
* @param server Where the user is attempting execution from
* @return If it can be executed
*/
boolean canExecute(User user, Server server);
/**
* Execute the command, and return the results
* @param user The user who executed the command
* @param args The arguments they executed it with
* @return The results as a MessageBuilder
*/
MessageBuilder execute(User user, List<String> args);
}

View file

@ -0,0 +1,45 @@
package me.StevenLawson.TotalFreedomMod.discord.command;
import me.StevenLawson.TotalFreedomMod.discord.commands.HelpCommand;
import me.StevenLawson.TotalFreedomMod.discord.commands.ListCommand;
import org.javacord.api.entity.channel.TextChannel;
import org.javacord.api.entity.message.MessageBuilder;
import org.javacord.api.entity.message.embed.EmbedBuilder;
import org.javacord.api.entity.server.Server;
import org.javacord.api.entity.user.User;
import java.awt.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class DiscordCommandManager {
public List<ExecutableDiscordCommand> commands = new ArrayList<>();
public void init() {
commands.add(new ListCommand("list", "Gives a list of online players.", "Server Commands", Collections.singletonList("l"), 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) {
List<String> args = new ArrayList<>(Arrays.asList(content.split(prefix)));
args.remove(0);
String commandOrAlias = args.remove(0);
for (ExecutableDiscordCommand command : commands) {
if(command.command.equalsIgnoreCase(commandOrAlias) || command.aliases.contains(commandOrAlias.toLowerCase())) {
if(command.canExecute(user, server)) {
MessageBuilder messageBuilder = command.execute(user, args);
messageBuilder.send(channel);
} else {
EmbedBuilder errorEmbed = new EmbedBuilder();
errorEmbed.setTitle("Command error");
errorEmbed.setColor(Color.RED);
errorEmbed.setDescription("You don't have permission to execute this command.");
channel.sendMessage(errorEmbed);
}
}
}
}
}

View file

@ -0,0 +1,69 @@
package me.StevenLawson.TotalFreedomMod.discord.command;
import com.google.common.collect.ImmutableList;
import org.javacord.api.entity.message.MessageBuilder;
import org.javacord.api.entity.permission.Role;
import org.javacord.api.entity.server.Server;
import org.javacord.api.entity.user.User;
import java.util.ArrayList;
import java.util.List;
public class ExecutableDiscordCommand implements DiscordCommand {
public String command;
public String description;
public String category;
public List<String> aliases;
//TODO: Add support for more complex permissions
public boolean isAdmin;
/**
* Creates a command
* @param command The string the command is execute by, e.g. list
* @param description What the command does
* @param category The category of the command
* @param aliases Other strings that should execute the command
* @param isAdmin If the command should only be accessible by administrators
*/
public ExecutableDiscordCommand(String command, String description, String category, List<String> aliases, boolean isAdmin) {
this.command = command;
this.description = description;
this.category = category;
this.aliases = ImmutableList.copyOf(aliases);
this.isAdmin = isAdmin;
}
/**
* Creates a command
* @param command The string the command is execute by, e.g. list
* @param description What the command does
* @param category The category of the command
* @param isAdmin If the command should only be accessible by administrators
*/
public ExecutableDiscordCommand(String command, String description, String category, boolean isAdmin) {
this.command = command;
this.description = description;
this.category = category;
this.aliases = ImmutableList.copyOf(new ArrayList<>());
this.isAdmin = isAdmin;
}
public boolean canExecute(User user, Server server) {
if(this.isAdmin) {
for (Role role : user.getRoles(server)) {
if(role.getName().toLowerCase().contains("admin")) {
return true;
}
}
return false;
}
return true;
}
public MessageBuilder execute(User user, List<String> args) {
return new MessageBuilder().setContent("");
}
}

View file

@ -0,0 +1,50 @@
package me.StevenLawson.TotalFreedomMod.discord.commands;
import me.StevenLawson.TotalFreedomMod.config.ConfigurationEntry;
import me.StevenLawson.TotalFreedomMod.config.MainConfig;
import me.StevenLawson.TotalFreedomMod.discord.bridge.DiscordBridge;
import me.StevenLawson.TotalFreedomMod.discord.command.ExecutableDiscordCommand;
import org.javacord.api.entity.message.MessageBuilder;
import org.javacord.api.entity.message.embed.EmbedBuilder;
import org.javacord.api.entity.user.User;
import java.awt.*;
import java.util.*;
import java.util.List;
public class HelpCommand extends ExecutableDiscordCommand {
public HelpCommand(String command, String description, String category, boolean isAdmin) {
super(command, description, category, isAdmin);
}
@Override
public MessageBuilder execute(User user, List<String> args) {
EmbedBuilder embedBuilder = new EmbedBuilder();
embedBuilder.setColor(Color.GREEN);
embedBuilder.setTitle("Help Command");
Map<String, List<ExecutableDiscordCommand>> categories = new HashMap<>();
for (ExecutableDiscordCommand command : DiscordBridge.COMMAND_MANAGER.commands) {
if(!categories.containsKey(command.category)) {
categories.put(command.category, new ArrayList<>(Collections.singletonList(command)));
} else {
List<ExecutableDiscordCommand> commands = categories.get(command.category);
commands.add(command);
categories.put(command.category, commands);
}
}
for (String category : categories.keySet()) {
List<ExecutableDiscordCommand> commands = categories.get(category);
StringBuilder value = new StringBuilder();
for (ExecutableDiscordCommand command : commands) {
value.append(String.format("**%s%s** - %s%n", MainConfig.getString(ConfigurationEntry.DISCORD_PREFIX), command.command, command.description));
}
embedBuilder.addField(category, value.toString().trim(), false);
}
return new MessageBuilder().addEmbeds(embedBuilder);
}
}

View file

@ -0,0 +1,56 @@
package me.StevenLawson.TotalFreedomMod.discord.commands;
import me.StevenLawson.TotalFreedomMod.bridge.EssentialsBridge;
import me.StevenLawson.TotalFreedomMod.config.ConfigurationEntry;
import me.StevenLawson.TotalFreedomMod.config.MainConfig;
import me.StevenLawson.TotalFreedomMod.discord.command.ExecutableDiscordCommand;
import me.StevenLawson.TotalFreedomMod.player.PlayerRank;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.javacord.api.entity.message.MessageBuilder;
import org.javacord.api.entity.message.embed.EmbedBuilder;
import org.javacord.api.entity.user.User;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class ListCommand extends ExecutableDiscordCommand {
public ListCommand(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) {
EmbedBuilder builder = new EmbedBuilder()
.setTitle(String.format("Player List - %s", MainConfig.getString(ConfigurationEntry.SERVER_NAME)))
.setDescription(String.format("There are %s / %s online players", Bukkit.getOnlinePlayers().size(), Bukkit.getMaxPlayers()));
List<PlayerRank> inGameRanks = new ArrayList<>();
for (Player player : Bukkit.getOnlinePlayers()) {
com.earth2me.essentials.User essentialsUser = EssentialsBridge.getEssentialsUser(player.getDisplayName());
if(essentialsUser != null) {
if(essentialsUser.isVanished()) continue;
}
PlayerRank rank = PlayerRank.fromSender(player);
if(!inGameRanks.contains(rank)) inGameRanks.add(rank);
}
Collections.sort(inGameRanks);
Collections.reverse(inGameRanks);
for (PlayerRank inGameRank : inGameRanks) {
List<String> inGame = inGameRank.getInGameUsernames();
if(inGame.size() > 0) {
builder.addField(String.format("%s (%s)", inGameRank.getPlural(), inGame.size()), String.join(", ", inGame));
}
}
return new MessageBuilder().addEmbed(builder);
}
}

View file

@ -0,0 +1,107 @@
package me.StevenLawson.TotalFreedomMod.discord.sender;
import org.bukkit.Bukkit;
import org.bukkit.Server;
import org.bukkit.command.CommandSender;
import org.bukkit.permissions.PermissibleBase;
import org.bukkit.permissions.Permission;
import org.bukkit.permissions.PermissionAttachment;
import org.bukkit.permissions.PermissionAttachmentInfo;
import org.bukkit.plugin.Plugin;
import org.javacord.api.entity.user.User;
import java.util.Set;
public class DiscordCommandSender implements CommandSender {
private final User user;
protected final PermissibleBase perm;
public DiscordCommandSender(User user) {
this.user = user;
this.perm = new PermissibleBase(this);
}
@Override
public void sendMessage(String s) {
user.sendMessage(s);
}
@Override
public void sendMessage(String[] strings) {
user.sendMessage(String.join("", strings));
}
@Override
public Server getServer() {
return Bukkit.getServer();
}
@Override
public String getName() {
return user.getDiscriminatedName();
}
@Override
public boolean isPermissionSet(String s) {
return this.perm.isPermissionSet(s);
}
@Override
public boolean isPermissionSet(Permission permission) {
return this.perm.isPermissionSet(permission);
}
@Override
public boolean hasPermission(String s) {
return this.perm.hasPermission(s);
}
@Override
public boolean hasPermission(Permission permission) {
return this.perm.hasPermission(permission);
}
@Override
public PermissionAttachment addAttachment(Plugin plugin, String s, boolean b) {
return this.perm.addAttachment(plugin, s, b);
}
@Override
public PermissionAttachment addAttachment(Plugin plugin) {
return this.perm.addAttachment(plugin);
}
@Override
public PermissionAttachment addAttachment(Plugin plugin, String s, boolean b, int i) {
return this.perm.addAttachment(plugin, s, b, i);
}
@Override
public PermissionAttachment addAttachment(Plugin plugin, int i) {
return this.perm.addAttachment(plugin, i);
}
@Override
public void removeAttachment(PermissionAttachment permissionAttachment) {
this.perm.removeAttachment(permissionAttachment);
}
@Override
public void recalculatePermissions() {
this.perm.recalculatePermissions();
}
@Override
public Set<PermissionAttachmentInfo> getEffectivePermissions() {
return this.perm.getEffectivePermissions();
}
@Override
public boolean isOp() {
return true;
}
@Override
public void setOp(boolean b) {
}
}

View file

@ -5,7 +5,7 @@ import me.StevenLawson.TotalFreedomMod.Server;
import me.StevenLawson.TotalFreedomMod.TotalFreedomMod;
import me.StevenLawson.TotalFreedomMod.admin.AdminList;
import me.StevenLawson.TotalFreedomMod.ban.BanManager;
import me.StevenLawson.TotalFreedomMod.bridge.DiscordBridge;
import me.StevenLawson.TotalFreedomMod.discord.bridge.DiscordBridge;
import me.StevenLawson.TotalFreedomMod.command.CommandBlocker;
import me.StevenLawson.TotalFreedomMod.commands.Command_landmine;
import me.StevenLawson.TotalFreedomMod.config.ConfigurationEntry;

View file

@ -5,7 +5,7 @@ import me.StevenLawson.TotalFreedomMod.TotalFreedomMod;
import me.StevenLawson.TotalFreedomMod.admin.AdminList;
import me.StevenLawson.TotalFreedomMod.ban.Ban;
import me.StevenLawson.TotalFreedomMod.ban.BanManager;
import me.StevenLawson.TotalFreedomMod.bridge.DiscordBridge;
import me.StevenLawson.TotalFreedomMod.discord.bridge.DiscordBridge;
import me.StevenLawson.TotalFreedomMod.config.Configuration;
import me.StevenLawson.TotalFreedomMod.config.ConfigurationEntry;
import me.StevenLawson.TotalFreedomMod.player.Player;