Added sub commands for polls and did help command

This commit is contained in:
Teriuihi 2022-04-01 02:54:33 +02:00
parent 311348dcf6
commit c6bd15f141
15 changed files with 342 additions and 219 deletions

View File

@ -15,14 +15,13 @@ import java.net.URISyntaxException;
public class AltitudeBot {
private JDA jda;
private PermissionManager permissionManager;
private static AltitudeBot instance;
public static AltitudeBot getInstance() {
return instance;
}
public void main(String args[]) {
public void main(String[] args) {
instance = this;
Logger.info("Starting bot...");
initConfigs();
@ -36,7 +35,7 @@ public class AltitudeBot {
}
private void initListeners() {
jda.addEventListener(new CommandManager());
jda.addEventListener(new CommandManager(jda));
}
private void initConfigs() {
@ -54,10 +53,6 @@ public class AltitudeBot {
return (null);
}
public PermissionManager getPermissionManager() {
return permissionManager;
}
public JDA getJDA() {
return jda;
}

View File

@ -1,55 +1,109 @@
package com.alttd.commandManager;
import com.alttd.commandManager.commands.CommandHelp;
import com.alttd.commandManager.commands.PollCommand.CommandPoll;
import com.alttd.database.Database;
import com.alttd.util.Logger;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.TextChannel;
import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import org.jetbrains.annotations.NotNull;
import java.awt.*;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
public class CommandManager extends ListenerAdapter {
private final List<DiscordCommand> commands;
private final HashMap<String, List<ScopeInfo>> commandList = new HashMap<>();
public CommandManager() {
commands = List.of();
public CommandManager(JDA jda) {
commands = List.of(new CommandHelp(jda, this),
new CommandPoll(jda, this));
loadCommands();
}
@Override
public void onSlashCommandInteraction(SlashCommandInteractionEvent event) {
public void onSlashCommandInteraction(@NotNull SlashCommandInteractionEvent event) {
Optional<DiscordCommand> first = commands.stream()
.filter(discordCommand -> discordCommand.getName().equalsIgnoreCase(event.getCommandString()))
.findFirst();
if (first.isEmpty()) {
event.replyEmbeds(new EmbedBuilder()
.setTitle("Invalid command")
.setDescription("We couldn't find this command, please report this issue to a Teri")
.setColor(Color.RED)
.build())
.setEphemeral(true)
.queue();
return;
}
first.get().execute(event);
}
@Override
public void onCommandAutoCompleteInteraction(@NotNull CommandAutoCompleteInteractionEvent event) {
Optional<DiscordCommand> first = commands.stream()
.filter(discordCommand -> discordCommand.getName().equalsIgnoreCase(event.getCommandString()))
.findFirst();
if (first.isEmpty())
return;
first.get().suggest(event);
}
public List<DiscordCommand> getCommands() {
return commands;
}
public List<ScopeInfo> getActiveLocations(String command) { //TODO make this cache results
String sql = "SELECT FROM commands WHERE command_name = ?";
List<ScopeInfo> scopeInfoList = new ArrayList<>();
public List<DiscordCommand> getCommands(TextChannel textChannel) {
return commands.stream().filter(command -> {
List<ScopeInfo> scopeInfoList = commandList.get(command.getName());
for (ScopeInfo scopeInfo : scopeInfoList) {
switch (scopeInfo.getScope()) {
case GLOBAL -> {
return true;
}
case GUILD -> {
if (textChannel.getGuild().getIdLong() == scopeInfo.getId())
return true;
}
}
}
return false;
}).collect(Collectors.toList());
}
private void loadCommands() {
String sql = "SELECT * FROM commands";
try {
PreparedStatement statement = Database.getDatabase().getConnection().prepareStatement(sql);
statement.setString(1, command.toLowerCase());
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
String commandName = resultSet.getString("command_name");
List<ScopeInfo> scopeInfoList = commandList.getOrDefault(commandName, new ArrayList<>());
scopeInfoList.add(new ScopeInfo(
CommandScope.valueOf(resultSet.getString("scope")),
resultSet.getLong("location_id")));
commandList.put(commandName, scopeInfoList);
}
} catch (SQLException exception) {
Logger.sql(exception);
}
return scopeInfoList;
}
public List<ScopeInfo> getActiveLocations(String command) {
return commandList.getOrDefault(command, new ArrayList<>());
}
}

View File

@ -1,5 +1,6 @@
package com.alttd.commandManager;
import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
public abstract class DiscordCommand {
@ -12,6 +13,8 @@ public abstract class DiscordCommand {
public abstract void execute(SlashCommandInteractionEvent event);
public abstract void suggest(CommandAutoCompleteInteractionEvent event);
public abstract String getHelpMessage();
public String getExtendedHelpMessage() {

View File

@ -1,81 +1,121 @@
//package com.alttd.commandManager.commands;
//
//import com.alttd.AltitudeBot;
//import com.alttd.commandManager.CommandManager;
//import com.alttd.commandManager.DiscordCommand;
//import com.alttd.config.MessagesConfig;
//import com.alttd.permissions.PermissionManager;
//import com.alttd.templates.Parser;
//import com.alttd.templates.Template;
//import com.alttd.util.Logger;
//import com.alttd.util.Util;
//import net.dv8tion.jda.api.entities.Member;
//import net.dv8tion.jda.api.entities.PrivateChannel;
//import net.dv8tion.jda.api.entities.TextChannel;
//import net.dv8tion.jda.api.entities.User;
//
//import java.util.List;
//import java.util.Optional;
//
//public class CommandHelp extends DiscordCommand {
//
// private final CommandManager commandManager;
//
// public CommandHelp(CommandManager commandManager) {
// this.commandManager = commandManager;
// }
//
// @Override
// public String getName() {
// return "help";
// }
//
// @Override
// public String execute(String[] args, Member commandSource, TextChannel textChannel) {
// return execute(args, textChannel, commandSource.getIdLong(), textChannel.getGuild().getIdLong(), Util.getGroupIds(commandSource));
// }
//
// @Override
// public String execute(String[] args, User commandSource, TextChannel textChannel) {
// if (!(textChannel instanceof PrivateChannel))
// Logger.warning("Using User when executing command on Member: % Command: %", commandSource.getAsMention(), getName());
// return execute(args, textChannel, commandSource.getIdLong(), 0, null);
// }
//
// public String execute(String[] args, TextChannel textChannel, long userId, long guildId, List<Long> groupIds) {
// PermissionManager permissionManager = AltitudeBot.getInstance().getPermissionManager();
// StringBuilder helpMessage = new StringBuilder();
// if (args.length == 0) {
// commandManager.getCommands().stream()
// .filter(command -> permissionManager.hasPermission(
// textChannel,
// userId,
// groupIds,
// command.getPermission()))
// .forEach(command -> helpMessage.append(command.getHelpMessage()));
// } else {
// String arg = args[0].toLowerCase();
// Optional<DiscordCommand> first = commandManager.getCommands().stream()
// .filter(command -> command.getName().equals(arg)
// || command.getAliases().contains(arg)).findFirst();
// if (first.isEmpty())
// return Parser.parse(MessagesConfig.INVALID_COMMAND_ARGS,
// Template.of("args", arg),
// Template.of("command", getName()),
// Template.of("prefix", commandManager.getPrefix(guildId)));
// DiscordCommand discordCommand = first.get();
// helpMessage.append(discordCommand.getExtendedHelpMessage());
// }
// return Parser.parse(MessagesConfig.HELP_MESSAGE_TEMPLATE, Template.of("commands", helpMessage.toString()));
// }
//
// @Override
// public String getHelpMessage() {
// return MessagesConfig.HELP_HELP;
// }
//
// @Override
// public List<String> getAliases() {
// return null;
// }
//}
package com.alttd.commandManager.commands;
import com.alttd.commandManager.CommandManager;
import com.alttd.commandManager.DiscordCommand;
import com.alttd.config.MessagesConfig;
import com.alttd.permissions.PermissionManager;
import com.alttd.templates.Parser;
import com.alttd.templates.Template;
import com.alttd.util.Util;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.TextChannel;
import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.interactions.AutoCompleteQuery;
import net.dv8tion.jda.api.interactions.commands.OptionMapping;
import net.dv8tion.jda.api.interactions.commands.OptionType;
import net.dv8tion.jda.api.interactions.commands.build.Commands;
import net.dv8tion.jda.api.interactions.commands.build.SlashCommandData;
import java.awt.*;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
public class CommandHelp extends DiscordCommand {
private final CommandManager commandManager;
public CommandHelp(JDA jda, CommandManager commandManager) {
this.commandManager = commandManager;
SlashCommandData slashCommandData = Commands.slash(getName(), "Show info about all commands or a specific command.")
.addOption(OptionType.STRING, "command", "Command to get more info about", true , true);
Util.registerCommand(commandManager, jda, slashCommandData, getName());
}
@Override
public String getName() {
return "help";
}
@Override
public void execute(SlashCommandInteractionEvent event) {
PermissionManager permissionManager = PermissionManager.getInstance();
if (permissionManager.hasPermission(event.getTextChannel(), event.getIdLong(), Util.getGroupIds(event.getMember()), getPermission())) {
event.replyEmbeds(Util.noPermission(getName())).setEphemeral(true).queue();
return;
}
StringBuilder helpMessage = new StringBuilder();
List<OptionMapping> options = event.getOptions();
TextChannel textChannel = event.getTextChannel();
if (options.size() == 0) {
commandManager.getCommands(textChannel).stream()
.filter(command -> permissionManager.hasPermission(
textChannel,
event.getUser().getIdLong(),
Util.getGroupIds(event.getMember()),
command.getPermission()))
.forEach(command -> helpMessage.append(command.getHelpMessage()));
} else {
OptionMapping optionMapping = event.getOption("command");
if (optionMapping == null) {
event.replyEmbeds(new EmbedBuilder()
.setTitle("Error")
.setDescription("Missing command option")
.setColor(Color.RED)
.build())
.setEphemeral(true)
.queue();
return;
}
String command = optionMapping.getAsString();
Optional<DiscordCommand> first = commandManager.getCommands().stream()
.filter(discordCommand -> discordCommand.getName().equals(command)).findFirst();
if (first.isEmpty())
{
event.replyEmbeds(new EmbedBuilder()
.setTitle("Command Not Found")
.setDescription("Unable to find the `" + command + "` command.")
.setColor(Color.RED)
.build())
.setEphemeral(true)
.queue();
return;
}
helpMessage.append(first.get().getExtendedHelpMessage());
}
event.replyEmbeds(new EmbedBuilder()
.setTitle("Help")
.setDescription(Parser.parse(MessagesConfig.HELP_MESSAGE_TEMPLATE, Template.of("commands", helpMessage.toString())))
.setColor(Color.GREEN)
.build())
.setEphemeral(true)
.queue();
}
@Override
public void suggest(CommandAutoCompleteInteractionEvent event) {
AutoCompleteQuery focusedOption = event.getFocusedOption();
if (!focusedOption.getType().equals(OptionType.STRING) || !focusedOption.getName().equals("command"))
return;
String value = focusedOption.getValue().toLowerCase();
List<String> collect = commandManager.getCommands().stream()
.map(DiscordCommand::getName)
.filter(name -> name.toLowerCase().startsWith(value))
.collect(Collectors.toList());
for (int i = collect.size(); i > 25; i--) //Can only have 25 options
collect.remove(i - 1);
event.replyChoiceStrings(collect).queue();
}
@Override
public String getHelpMessage() {
return MessagesConfig.HELP_HELP;
}
}

View File

@ -2,27 +2,27 @@ package com.alttd.commandManager.commands.PollCommand;
import com.alttd.commandManager.CommandManager;
import com.alttd.commandManager.DiscordCommand;
import com.alttd.commandManager.ScopeInfo;
import com.alttd.commandManager.SubCommand;
import com.alttd.permissions.PermissionManager;
import com.alttd.util.Logger;
import com.alttd.util.OptionMappingParsing;
import com.alttd.util.Util;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.GuildMessageChannel;
import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.interactions.commands.OptionType;
import net.dv8tion.jda.api.interactions.commands.build.Commands;
import net.dv8tion.jda.api.interactions.commands.build.SlashCommandData;
import net.dv8tion.jda.api.interactions.commands.build.SubcommandData;
import net.dv8tion.jda.api.requests.RestAction;
import java.awt.*;
import java.util.HashMap;
public class CommandPoll extends DiscordCommand {
private final CommandManager commandManager;
private final HashMap<String, SubCommand> subCommandMap = new HashMap<>();
public CommandPoll(JDA jda, CommandManager commandManager) {
this.commandManager = commandManager;
SlashCommandData slashCommandData = Commands.slash(getName(), "Create, edit, and manage polls")
.addSubcommands(
new SubcommandData("add", "Add a new poll to a channel")
@ -54,21 +54,15 @@ public class CommandPoll extends DiscordCommand {
new SubcommandData("results", "Get the results for a poll")
.addOption(OptionType.CHANNEL, "channel", "Channel this poll is in", true, true)
.addOption(OptionType.INTEGER, "message_id", "Id of the poll you want the results for", true));
for (ScopeInfo info : commandManager.getActiveLocations(getName())) {
switch (info.getScope()) {
case GLOBAL -> jda.updateCommands().addCommands(slashCommandData).queue();
case GUILD -> {
Guild guildById = jda.getGuildById(info.getId());
if (guildById == null)
{
Logger.warning("Tried to add command % to invalid guild %", getName(), String.valueOf(info.getId()));
continue;
}
guildById.updateCommands().addCommands(slashCommandData).queue(RestAction.getDefaultSuccess(), Util::handleFailure);
}
case USER -> Logger.warning("Tried to add command % to user, this is not implemented yet since I don't know how this should work.");
}
}
Util.registerSubcommands(subCommandMap, new SubCommandAdd(this),
new SubCommandAddButton(this),
new SubCommandClose(this),
new SubCommandEditDescription(this),
new SubCommandEditTitle(this),
new SubCommandOpen(this),
new SubCommandRemoveButton(this),
new SubCommandResults(this));
Util.registerCommand(commandManager, jda, slashCommandData, getName());
}
@Override
@ -78,12 +72,11 @@ public class CommandPoll extends DiscordCommand {
@Override
public void execute(SlashCommandInteractionEvent event) {
if (event.getGuild() == null || event.getMember() == null)
{
if (event.getGuild() == null || event.getMember() == null) {
event.replyEmbeds(Util.guildOnlyCommand(getName())).setEphemeral(true).queue();
return;
}
if (PermissionManager.getInstance().hasPermission(event.getTextChannel(), event.getMember(), getPermission())) {
if (PermissionManager.getInstance().hasPermission(event.getTextChannel(), event.getIdLong(), Util.getGroupIds(event.getMember()), getPermission())) {
event.replyEmbeds(Util.noPermission(getName())).setEphemeral(true).queue();
return;
}
@ -94,89 +87,24 @@ public class CommandPoll extends DiscordCommand {
return;
}
switch (subcommandName) {
case "add" -> {
GuildMessageChannel channel = OptionMappingParsing.getGuildChannel("channel", event, getName());
if (channel == null)
return;
String title = OptionMappingParsing.getString("title", event, getName());
if (title == null)
SubCommand subCommand = subCommandMap.get(subcommandName);
if (subCommand == null) {
event.replyEmbeds(new EmbedBuilder()
.setTitle("Subcommand not found")
.setDescription("Unable to find subcommand `" + subcommandName + "`")
.setColor(Color.RED)
.build())
.setEphemeral(true)
.queue();
return;
}
case "edit_title" -> {
GuildMessageChannel channel = OptionMappingParsing.getGuildChannel("channel", event, getName());
if (channel == null)
return;
Long messageId = OptionMappingParsing.getLong("message_id", event, getName());
if (messageId == null)
return;
String title = OptionMappingParsing.getString("title", event, getName());
if (title == null)
return;
}
case "edit_description" -> {
GuildMessageChannel channel = OptionMappingParsing.getGuildChannel("channel", event, getName());
if (channel == null)
return;
Long messageId = OptionMappingParsing.getLong("message_id", event, getName());
if (messageId == null)
return;
String description = OptionMappingParsing.getString("description", event, getName());
if (description == null)
return;
}
case "add_button" -> {
GuildMessageChannel channel = OptionMappingParsing.getGuildChannel("channel", event, getName());
if (channel == null)
return;
Long messageId = OptionMappingParsing.getLong("message_id", event, getName());
if (messageId == null)
return;
Long rowLong = OptionMappingParsing.getLong("button_row", event, getName());
if (rowLong == null)
return;
int row = rowLong.intValue();
String buttonName = OptionMappingParsing.getString("button_name", event, getName());
if (buttonName == null)
return;
}
case "remove_button" -> {
GuildMessageChannel channel = OptionMappingParsing.getGuildChannel("channel", event, getName());
if (channel == null)
return;
Long messageId = OptionMappingParsing.getLong("message_id", event, getName());
if (messageId == null)
return;
String buttonName = OptionMappingParsing.getString("button_name", event, getName());
if (buttonName == null)
return;
}
case "open" -> {
GuildMessageChannel channel = OptionMappingParsing.getGuildChannel("channel", event, getName());
if (channel == null)
return;
Long messageId = OptionMappingParsing.getLong("message_id", event, getName());
if (messageId == null)
return;
}
case "close" -> {
GuildMessageChannel channel = OptionMappingParsing.getGuildChannel("channel", event, getName());
if (channel == null)
return;
Long messageId = OptionMappingParsing.getLong("message_id", event, getName());
if (messageId == null)
return;
}
case "results" -> {
GuildMessageChannel channel = OptionMappingParsing.getGuildChannel("channel", event, getName());
if (channel == null)
return;
Long messageId = OptionMappingParsing.getLong("message_id", event, getName());
if (messageId == null)
return;
}
default -> throw new IllegalStateException("Unexpected value: " + subcommandName);
subCommand.execute(event);
}
@Override
public void suggest(CommandAutoCompleteInteractionEvent event) {
}
@Override

View File

@ -2,6 +2,8 @@ package com.alttd.commandManager.commands.PollCommand;
import com.alttd.commandManager.DiscordCommand;
import com.alttd.commandManager.SubCommand;
import com.alttd.util.OptionMappingParsing;
import net.dv8tion.jda.api.entities.GuildMessageChannel;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
public class SubCommandAdd extends SubCommand {
@ -17,7 +19,12 @@ public class SubCommandAdd extends SubCommand {
@Override
public void execute(SlashCommandInteractionEvent event) {
GuildMessageChannel channel = OptionMappingParsing.getGuildChannel("channel", event, getName());
if (channel == null)
return;
String title = OptionMappingParsing.getString("title", event, getName());
if (title == null)
return;
}
@Override

View File

@ -2,6 +2,8 @@ package com.alttd.commandManager.commands.PollCommand;
import com.alttd.commandManager.DiscordCommand;
import com.alttd.commandManager.SubCommand;
import com.alttd.util.OptionMappingParsing;
import net.dv8tion.jda.api.entities.GuildMessageChannel;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
public class SubCommandAddButton extends SubCommand {
@ -16,7 +18,19 @@ public class SubCommandAddButton extends SubCommand {
@Override
public void execute(SlashCommandInteractionEvent event) {
GuildMessageChannel channel = OptionMappingParsing.getGuildChannel("channel", event, getName());
if (channel == null)
return;
Long messageId = OptionMappingParsing.getLong("message_id", event, getName());
if (messageId == null)
return;
Long rowLong = OptionMappingParsing.getLong("button_row", event, getName());
if (rowLong == null)
return;
int row = rowLong.intValue();
String buttonName = OptionMappingParsing.getString("button_name", event, getName());
if (buttonName == null)
return;
}
@Override

View File

@ -2,6 +2,8 @@ package com.alttd.commandManager.commands.PollCommand;
import com.alttd.commandManager.DiscordCommand;
import com.alttd.commandManager.SubCommand;
import com.alttd.util.OptionMappingParsing;
import net.dv8tion.jda.api.entities.GuildMessageChannel;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
public class SubCommandClose extends SubCommand {
@ -16,7 +18,12 @@ public class SubCommandClose extends SubCommand {
@Override
public void execute(SlashCommandInteractionEvent event) {
GuildMessageChannel channel = OptionMappingParsing.getGuildChannel("channel", event, getName());
if (channel == null)
return;
Long messageId = OptionMappingParsing.getLong("message_id", event, getName());
if (messageId == null)
return;
}
@Override

View File

@ -2,6 +2,8 @@ package com.alttd.commandManager.commands.PollCommand;
import com.alttd.commandManager.DiscordCommand;
import com.alttd.commandManager.SubCommand;
import com.alttd.util.OptionMappingParsing;
import net.dv8tion.jda.api.entities.GuildMessageChannel;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
public class SubCommandEditDescription extends SubCommand {
@ -16,7 +18,15 @@ public class SubCommandEditDescription extends SubCommand {
@Override
public void execute(SlashCommandInteractionEvent event) {
GuildMessageChannel channel = OptionMappingParsing.getGuildChannel("channel", event, getName());
if (channel == null)
return;
Long messageId = OptionMappingParsing.getLong("message_id", event, getName());
if (messageId == null)
return;
String description = OptionMappingParsing.getString("description", event, getName());
if (description == null)
return;
}
@Override

View File

@ -2,6 +2,8 @@ package com.alttd.commandManager.commands.PollCommand;
import com.alttd.commandManager.DiscordCommand;
import com.alttd.commandManager.SubCommand;
import com.alttd.util.OptionMappingParsing;
import net.dv8tion.jda.api.entities.GuildMessageChannel;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
public class SubCommandEditTitle extends SubCommand {
@ -16,7 +18,15 @@ public class SubCommandEditTitle extends SubCommand {
@Override
public void execute(SlashCommandInteractionEvent event) {
GuildMessageChannel channel = OptionMappingParsing.getGuildChannel("channel", event, getName());
if (channel == null)
return;
Long messageId = OptionMappingParsing.getLong("message_id", event, getName());
if (messageId == null)
return;
String title = OptionMappingParsing.getString("title", event, getName());
if (title == null)
return;
}
@Override

View File

@ -2,6 +2,8 @@ package com.alttd.commandManager.commands.PollCommand;
import com.alttd.commandManager.DiscordCommand;
import com.alttd.commandManager.SubCommand;
import com.alttd.util.OptionMappingParsing;
import net.dv8tion.jda.api.entities.GuildMessageChannel;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
public class SubCommandOpen extends SubCommand {
@ -16,7 +18,12 @@ public class SubCommandOpen extends SubCommand {
@Override
public void execute(SlashCommandInteractionEvent event) {
GuildMessageChannel channel = OptionMappingParsing.getGuildChannel("channel", event, getName());
if (channel == null)
return;
Long messageId = OptionMappingParsing.getLong("message_id", event, getName());
if (messageId == null)
return;
}
@Override

View File

@ -2,6 +2,8 @@ package com.alttd.commandManager.commands.PollCommand;
import com.alttd.commandManager.DiscordCommand;
import com.alttd.commandManager.SubCommand;
import com.alttd.util.OptionMappingParsing;
import net.dv8tion.jda.api.entities.GuildMessageChannel;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
public class SubCommandRemoveButton extends SubCommand {
@ -16,7 +18,15 @@ public class SubCommandRemoveButton extends SubCommand {
@Override
public void execute(SlashCommandInteractionEvent event) {
GuildMessageChannel channel = OptionMappingParsing.getGuildChannel("channel", event, getName());
if (channel == null)
return;
Long messageId = OptionMappingParsing.getLong("message_id", event, getName());
if (messageId == null)
return;
String buttonName = OptionMappingParsing.getString("button_name", event, getName());
if (buttonName == null)
return;
}
@Override

View File

@ -2,6 +2,8 @@ package com.alttd.commandManager.commands.PollCommand;
import com.alttd.commandManager.DiscordCommand;
import com.alttd.commandManager.SubCommand;
import com.alttd.util.OptionMappingParsing;
import net.dv8tion.jda.api.entities.GuildMessageChannel;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
public class SubCommandResults extends SubCommand {
@ -16,7 +18,12 @@ public class SubCommandResults extends SubCommand {
@Override
public void execute(SlashCommandInteractionEvent event) {
GuildMessageChannel channel = OptionMappingParsing.getGuildChannel("channel", event, getName());
if (channel == null)
return;
Long messageId = OptionMappingParsing.getLong("message_id", event, getName());
if (messageId == null)
return;
}
@Override

View File

@ -16,14 +16,13 @@ public class MessagesConfig extends AbstractConfig {
messagesConfig.readConfig(MessagesConfig.class, null);
}
public static String HELP_HELP = "`<prefix>help`: Shows help menu";
public static String HELP_HELP = "`/help`: Shows help menu";
public static String HELP_MESSAGE_TEMPLATE = "<commands>";
private static void loadHelp() {
HELP_HELP = messagesConfig.getString("help.help", HELP_HELP);
HELP_MESSAGE_TEMPLATE = messagesConfig.getString("help.message-template", HELP_MESSAGE_TEMPLATE);
}
private static void loadPollHelp() {
}

View File

@ -1,22 +1,31 @@
package com.alttd.util;
import com.alttd.commandManager.CommandManager;
import com.alttd.commandManager.DiscordCommand;
import com.alttd.commandManager.ScopeInfo;
import com.alttd.commandManager.SubCommand;
import com.alttd.config.MessagesConfig;
import com.alttd.templates.Parser;
import com.alttd.templates.Template;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.entities.Role;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.*;
import net.dv8tion.jda.api.interactions.commands.Command;
import net.dv8tion.jda.api.interactions.commands.OptionMapping;
import net.dv8tion.jda.api.interactions.commands.SlashCommandInteraction;
import net.dv8tion.jda.api.interactions.commands.build.SlashCommandData;
import net.dv8tion.jda.api.requests.RestAction;
import java.awt.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;
public class Util {
public static List<Long> getGroupIds(Member member) {
if (member == null)
return new ArrayList<>();
return member.getRoles().stream()
.map(Role::getIdLong)
.collect(Collectors.toList());
@ -54,4 +63,27 @@ public class Util {
}
return embedBuilder.build();
}
public static void registerCommand(CommandManager commandManager, JDA jda, SlashCommandData slashCommandData, String commandName) {
for (ScopeInfo info : commandManager.getActiveLocations(commandName)) {
switch (info.getScope()) {
case GLOBAL -> jda.updateCommands().addCommands(slashCommandData).queue();
case GUILD -> {
Guild guildById = jda.getGuildById(info.getId());
if (guildById == null)
{
Logger.warning("Tried to add command % to invalid guild %", commandName, String.valueOf(info.getId()));
continue;
}
guildById.updateCommands().addCommands(slashCommandData).queue(RestAction.getDefaultSuccess(), Util::handleFailure);
}
case USER -> Logger.warning("Tried to add command % to user, this is not implemented yet since I don't know how this should work.");
}
}
}
public static void registerSubcommands(HashMap<String, SubCommand> subCommandMap, SubCommand... subCommands) {
for (SubCommand subCommand : subCommands)
subCommandMap.put(subCommand.getName(), subCommand);
}
}