[FEAT] New Commands system

This commit is contained in:
2023-08-06 02:37:27 +02:00
parent 5eb1ca1c29
commit 3a405a5ba4
11 changed files with 115 additions and 30 deletions

View File

@@ -1,6 +1,6 @@
version_major=0
version_minor=0
version_patch=15
version_patch=16
shade_group=com.hypherionmc.sdlink.shaded.

View File

@@ -24,7 +24,7 @@ public class SDLinkConfig extends ModuleConfig {
// DO NOT REMOVE TRANSIENT HERE... OTHERWISE, THE STUPID CONFIG LIBRARY
// WILL TRY TO WRITE THESE TO THE CONFIG
public transient static SDLinkConfig INSTANCE;
public transient static int configVer = 6;
public transient static int configVer = 7;
@Path("general")
@SpecComment("General Mod Config")
@@ -58,9 +58,9 @@ public class SDLinkConfig extends ModuleConfig {
@SpecComment("Enable or Disable certain bot commands")
public BotCommandsConfig botCommands = new BotCommandsConfig();
@Path("linkedCommands")
@Path("minecraftCommands")
@SpecComment("Execute Minecraft commands in Discord")
public LinkedCommandsConfig linkedCommands = new LinkedCommandsConfig();
public MinecraftCommands linkedCommands = new MinecraftCommands();
@Path("ignoredMessages")
@SpecComment("Configure messages that will be ignored when relaying to discord")

View File

@@ -4,17 +4,11 @@
*/
package com.hypherionmc.sdlink.core.config.impl;
import me.hypherionmc.moonconfig.core.conversion.Path;
import me.hypherionmc.moonconfig.core.conversion.SpecComment;
import java.util.ArrayList;
import java.util.List;
/**
* @author HypherionSA
* Main Config Structure to control Discord -> MC Commands
*/
public class LinkedCommandsConfig {
/*public class LinkedCommandsConfig {
@Path("enabled")
@SpecComment("Should linked commands be enabled")
@@ -39,3 +33,4 @@ public class LinkedCommandsConfig {
}
}
*/

View File

@@ -0,0 +1,31 @@
package com.hypherionmc.sdlink.core.config.impl;
import me.hypherionmc.moonconfig.core.conversion.Path;
import me.hypherionmc.moonconfig.core.conversion.SpecComment;
import java.util.ArrayList;
import java.util.List;
public class MinecraftCommands {
@Path("enabled")
@SpecComment("Allow executing Minecraft commands from Discord")
public boolean enabled = false;
@Path("prefix")
@SpecComment("Command Prefix. For example ?weather clear")
public String prefix = "?";
@Path("permissions")
@SpecComment("List of command permissions")
public List<Command> permissions = new ArrayList<>();
public static class Command {
public String role = "0";
public List<String> commands = new ArrayList<>();
public int permissionLevel = 1;
}
}

View File

@@ -12,7 +12,6 @@ import com.hypherionmc.sdlink.core.discord.commands.slash.linking.ConfirmAccount
import com.hypherionmc.sdlink.core.discord.commands.slash.linking.LinkAccountCommand;
import com.hypherionmc.sdlink.core.discord.commands.slash.linking.UnlinkAccountSlashCommand;
import com.hypherionmc.sdlink.core.discord.commands.slash.linking.ViewLinkedAccountsCommand;
import com.hypherionmc.sdlink.core.discord.commands.slash.mc.MCSlashCommand;
import com.hypherionmc.sdlink.core.discord.commands.slash.whitelist.ConfirmWhitelistSlashCommand;
import com.hypherionmc.sdlink.core.discord.commands.slash.whitelist.UnWhitelistAccountSlashCommand;
import com.hypherionmc.sdlink.core.discord.commands.slash.whitelist.ViewWhitelistedAccountsSlashCommand;
@@ -70,7 +69,7 @@ public class CommandManager {
}
if (SDLinkConfig.INSTANCE.linkedCommands.enabled) {
commands.add(new MCSlashCommand());
//commands.add(new MCSlashCommand());
}
}

View File

@@ -4,8 +4,10 @@
*/
package com.hypherionmc.sdlink.core.discord.commands.slash.mc;
/*import com.hypherionmc.sdlink.core.accounts.MinecraftAccount;
import com.hypherionmc.sdlink.core.config.SDLinkConfig;
import com.hypherionmc.sdlink.core.config.impl.LinkedCommandsConfig;
import com.hypherionmc.sdlink.core.database.SDLinkAccount;
import com.hypherionmc.sdlink.core.discord.commands.slash.SDLinkSlashCommand;
import com.hypherionmc.sdlink.core.managers.RoleManager;
import com.hypherionmc.sdlink.core.messaging.Result;
@@ -17,8 +19,11 @@ import net.dv8tion.jda.api.interactions.commands.OptionType;
import net.dv8tion.jda.api.interactions.commands.build.OptionData;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import static com.hypherionmc.sdlink.core.managers.DatabaseManager.sdlinkDatabase;
public class MCSlashCommand extends SDLinkSlashCommand {
public MCSlashCommand() {
@@ -64,18 +69,22 @@ public class MCSlashCommand extends SDLinkSlashCommand {
if (!args5.isEmpty())
args.append(" ").append(args5);
sdlinkDatabase.reloadCollection("accounts");
List<SDLinkAccount> accounts = sdlinkDatabase.findAll(SDLinkAccount.class);
Optional<SDLinkAccount> account = accounts.stream().filter(u -> u.getDiscordID().equals(event.getMember().getId())).findFirst();
linkedCommand.ifPresent(command -> {
if (!command.discordRole.isEmpty()) {
Role role = RoleManager.getCommandRoles().isEmpty() ? null : RoleManager.getCommandRoles().get(command.discordCommand);
boolean userRole = role != null && event.getMember().getRoles().stream().anyMatch(r -> r.getIdLong() == role.getIdLong());
if (userRole) {
executeCommand(event, command, args.toString(), event.getMember());
executeCommand(event, command, args.toString(), event.getMember(), account.orElse(null));
} else {
event.reply("You need the " + role.getName() + " role to perform this action").setEphemeral(true).queue();
}
} else {
executeCommand(event, command, args.toString(), event.getMember());
executeCommand(event, command, args.toString(), event.getMember(), account.orElse(null));
}
});
@@ -88,8 +97,8 @@ public class MCSlashCommand extends SDLinkSlashCommand {
}
}
private void executeCommand(SlashCommandEvent event, LinkedCommandsConfig.Command mcCommand, String args, Member member) {
Result result = SDLinkPlatform.minecraftHelper.executeMinecraftCommand(mcCommand.mcCommand, args, member);
private void executeCommand(SlashCommandEvent event, LinkedCommandsConfig.Command mcCommand, String args, Member member, SDLinkAccount account) {
Result result = SDLinkPlatform.minecraftHelper.executeMinecraftCommand(mcCommand.mcCommand, args, member, account);
event.reply(result.getMessage()).setEphemeral(true).queue();
}
}
}*/

View File

@@ -9,6 +9,7 @@ import com.hypherionmc.sdlink.core.discord.BotController;
import com.hypherionmc.sdlink.core.discord.commands.slash.general.ServerStatusSlashCommand;
import com.hypherionmc.sdlink.core.discord.hooks.BotReadyHooks;
import com.hypherionmc.sdlink.core.discord.hooks.DiscordMessageHooks;
import com.hypherionmc.sdlink.core.discord.hooks.MinecraftCommandHook;
import com.hypherionmc.sdlink.core.events.SDLinkReadyEvent;
import com.hypherionmc.sdlink.core.managers.CacheManager;
import com.hypherionmc.sdlink.core.managers.ChannelManager;
@@ -48,6 +49,7 @@ public class DiscordEventHandler extends ListenerAdapter {
if (!event.isFromGuild())
return;
MinecraftCommandHook.discordMessageEvent(event);
DiscordMessageHooks.discordMessageEvent(event);
}

View File

@@ -28,6 +28,9 @@ public class DiscordMessageHooks {
if (event.getAuthor().isBot() && SDLinkConfig.INSTANCE.chatConfig.ignoreBots)
return;
if (SDLinkConfig.INSTANCE.linkedCommands.enabled && !SDLinkConfig.INSTANCE.linkedCommands.permissions.isEmpty() && event.getMessage().getContentRaw().startsWith(SDLinkConfig.INSTANCE.linkedCommands.prefix))
return;
if (SDLinkConfig.INSTANCE.generalConfig.debugging) {
BotController.INSTANCE.getLogger().info("Sending Message from {}: {}", event.getAuthor().getName(), event.getMessage().getContentStripped());
}

View File

@@ -0,0 +1,44 @@
package com.hypherionmc.sdlink.core.discord.hooks;
import com.hypherionmc.sdlink.core.config.SDLinkConfig;
import com.hypherionmc.sdlink.core.database.SDLinkAccount;
import com.hypherionmc.sdlink.core.services.SDLinkPlatform;
import net.dv8tion.jda.api.entities.ISnowflake;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import static com.hypherionmc.sdlink.core.managers.DatabaseManager.sdlinkDatabase;
public class MinecraftCommandHook {
public static void discordMessageEvent(MessageReceivedEvent event) {
if (!SDLinkConfig.INSTANCE.linkedCommands.enabled || SDLinkConfig.INSTANCE.linkedCommands.permissions.isEmpty())
return;
if (!event.getMessage().getContentRaw().startsWith(SDLinkConfig.INSTANCE.linkedCommands.prefix))
return;
Set<Long> roles = event.getMember().getRoles().stream().map(ISnowflake::getIdLong).collect(Collectors.toSet());
roles.add(event.getMember().getIdLong());
roles.add(0L);
sdlinkDatabase.reloadCollection("accounts");
List<SDLinkAccount> accounts = sdlinkDatabase.findAll(SDLinkAccount.class);
Optional<SDLinkAccount> account = accounts.stream().filter(u -> u.getDiscordID().equals(event.getMember().getId())).findFirst();
Integer permLevel = SDLinkConfig.INSTANCE.linkedCommands.permissions.stream().filter(r -> roles.contains(Long.parseLong(r.role))).map(r -> r.permissionLevel).max(Integer::compareTo).orElse(-1);
List<String> commands = SDLinkConfig.INSTANCE.linkedCommands.permissions.stream().filter(c -> roles.contains(Long.parseLong(c.role))).flatMap(c -> c.commands.stream()).filter(s -> !s.isEmpty()).toList();
String raw = event.getMessage().getContentRaw().substring(SDLinkConfig.INSTANCE.linkedCommands.prefix.length());
if (commands.stream().anyMatch(raw::startsWith)) {
SDLinkPlatform.minecraftHelper.executeMinecraftCommand(raw, Integer.MAX_VALUE, event, account.orElse(null));
} else {
SDLinkPlatform.minecraftHelper.executeMinecraftCommand(raw, permLevel, event, account.orElse(null));
}
}
}

View File

@@ -9,7 +9,6 @@ import com.hypherionmc.sdlink.core.discord.BotController;
import com.hypherionmc.sdlink.core.util.SystemUtils;
import net.dv8tion.jda.api.entities.Role;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@@ -23,7 +22,7 @@ public class RoleManager {
private static Role whitelistedRole;
private static Role linkedRole;
private static final HashMap<String, Role> commandRoles = new HashMap<>();
//private static final HashMap<String, Role> commandRoles = new HashMap<>();
private static final Set<Role> autoWhitelistRoles = new HashSet<>();
/**
@@ -49,17 +48,17 @@ public class RoleManager {
});
}
if (SDLinkConfig.INSTANCE.linkedCommands.enabled) {
/*if (SDLinkConfig.INSTANCE.linkedCommands.enabled) {
commandRoles.clear();
SDLinkConfig.INSTANCE.linkedCommands.commands.forEach(cmd -> {
if (!cmd.discordRole.isEmpty()) {
Role role = getRole(errCount, builder, cmd.discordCommand + " usage", cmd.discordRole);
SDLinkConfig.INSTANCE.linkedCommands.permissions.forEach(cmd -> {
if (!cmd.role.isEmpty() && !cmd.role.equals("0")) {
Role role = getRole(errCount, builder, "Command Usage", cmd.role);
if (role != null) {
commandRoles.putIfAbsent(cmd.discordCommand, role);
cmd.commands.forEach(c -> commandRoles.putIfAbsent(c, role));
}
}
});
}
}*/
}
/**
@@ -106,9 +105,9 @@ public class RoleManager {
return whitelistedRole;
}
public static HashMap<String, Role> getCommandRoles() {
return commandRoles;
}
//public static HashMap<String, Role> getCommandRoles() {
//return commandRoles;
//}
public static Set<Role> getAutoWhitelistRoles() {
return autoWhitelistRoles;

View File

@@ -5,9 +5,12 @@
package com.hypherionmc.sdlink.core.services.helpers;
import com.hypherionmc.sdlink.core.accounts.MinecraftAccount;
import com.hypherionmc.sdlink.core.database.SDLinkAccount;
import com.hypherionmc.sdlink.core.messaging.Result;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import org.apache.commons.lang3.tuple.Pair;
import org.jetbrains.annotations.Nullable;
import java.util.List;
@@ -27,7 +30,7 @@ public interface IMinecraftHelper {
List<MinecraftAccount> getOnlinePlayers();
long getServerUptime();
String getServerVersion();
Result executeMinecraftCommand(String command, String args, Member member);
void executeMinecraftCommand(String command, int permLevel, MessageReceivedEvent event, @Nullable SDLinkAccount account);
boolean isOnlineMode();
}