did a lot of work on poll command

This commit is contained in:
2022-03-31 22:08:50 +02:00
parent ba55956bbf
commit 8f45b7039a
25 changed files with 826 additions and 116 deletions
@@ -1,9 +1,16 @@
package com.alttd.commandManager;
import com.alttd.database.Database;
import com.alttd.util.Logger;
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.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;
@@ -11,27 +18,38 @@ import java.util.List;
public class CommandManager extends ListenerAdapter {
private final List<DiscordCommand> commands;
private final HashMap<Long, String> commandPrefixes;
public CommandManager() {
commands = List.of();
commandPrefixes = null;//TODO query;
}
@Override
public void onMessageReceived(@NotNull MessageReceivedEvent event) {
String[] s = event.getMessage().getContentRaw().split(" ");
if (s.length < 1)
return;
String command = s[0];
String[] args = Arrays.copyOfRange(s, 1, s.length);
public void onSlashCommandInteraction(SlashCommandInteractionEvent event) {
}
public List<DiscordCommand> getCommands() {
return commands;
}
public String getPrefix(long guildId) {
return commandPrefixes.getOrDefault(guildId, "!");
public List<ScopeInfo> getActiveLocations(String command) { //TODO make this cache results
String sql = "SELECT FROM commands WHERE command_name = ?";
List<ScopeInfo> scopeInfoList = new ArrayList<>();
try {
PreparedStatement statement = Database.getDatabase().getConnection().prepareStatement(sql);
statement.setString(1, command.toLowerCase());
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
scopeInfoList.add(new ScopeInfo(
CommandScope.valueOf(resultSet.getString("scope")),
resultSet.getLong("location_id")));
}
} catch (SQLException exception) {
Logger.sql(exception);
}
return scopeInfoList;
}
}
@@ -0,0 +1,5 @@
package com.alttd.commandManager;
public enum CommandScope {
GLOBAL, GUILD, USER
}
@@ -1,10 +1,6 @@
package com.alttd.commandManager;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.TextChannel;
import net.dv8tion.jda.api.entities.User;
import java.util.List;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
public abstract class DiscordCommand {
@@ -14,9 +10,7 @@ public abstract class DiscordCommand {
return "command." + getName();
}
public abstract String execute(String[] args, Member commandSource, TextChannel textChannel);
public abstract String execute(String[] args, User commandSource, TextChannel textChannel);
public abstract void execute(SlashCommandInteractionEvent event);
public abstract String getHelpMessage();
@@ -24,6 +18,4 @@ public abstract class DiscordCommand {
return getHelpMessage();
}
public abstract List<String> getAliases();
}
@@ -0,0 +1,20 @@
package com.alttd.commandManager;
public class ScopeInfo {
CommandScope scope;
long id;
public ScopeInfo(CommandScope scope, long id) {
this.scope = scope;
this.id = id;
}
public CommandScope getScope() {
return scope;
}
public long getId() {
return id;
}
}
@@ -0,0 +1,27 @@
package com.alttd.commandManager;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
public abstract class SubCommand {
private final DiscordCommand parent;
protected SubCommand(DiscordCommand parent) {
this.parent = parent;
}
public DiscordCommand getParent() {
return parent;
}
public abstract String getName();
public String getPermission() {
return getParent().getPermission() + "." + getName();
}
public abstract void execute(SlashCommandInteractionEvent event);
public abstract String getHelpMessage();
}
@@ -1,4 +0,0 @@
package com.alttd.commandManager.commands;
public class CommandEmbed {
}
@@ -1,81 +1,81 @@
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.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;
// }
//}
@@ -0,0 +1,186 @@
package com.alttd.commandManager.commands.PollCommand;
import com.alttd.commandManager.CommandManager;
import com.alttd.commandManager.DiscordCommand;
import com.alttd.commandManager.ScopeInfo;
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.JDA;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.GuildMessageChannel;
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;
public class CommandPoll extends DiscordCommand {
private final CommandManager commandManager;
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")
.addOption(OptionType.CHANNEL, "channel", "Channel this poll should go into", true, true)
.addOption(OptionType.STRING, "title", "Title of the embed (max 256 characters)", true),
new SubcommandData("edit_title", "Edit the title of a poll")
.addOption(OptionType.CHANNEL, "channel", "Channel this poll is in", true, true)
.addOption(OptionType.INTEGER, "message_id", "Id of the poll you're editing", true)
.addOption(OptionType.STRING, "title", "The new title for the poll (max 256 characters)", true),
new SubcommandData("edit_description", "Edit the description of a poll")
.addOption(OptionType.CHANNEL, "channel", "Channel this poll is in", true, true)
.addOption(OptionType.INTEGER, "message_id", "Id of the poll you're editing", true)
.addOption(OptionType.STRING, "description", "The new description for the poll (max 2048 characters)", true),
new SubcommandData("add_button", "Add a button to a poll")
.addOption(OptionType.CHANNEL, "channel", "Channel this poll is in", true, true)
.addOption(OptionType.INTEGER, "message_id", "Id of the poll you're adding a button to", true)
.addOption(OptionType.INTEGER, "button_row", "Row the button should go in (1-5)", true)
.addOption(OptionType.STRING, "button_name", "Name of the button you're adding"),
new SubcommandData("remove_button", "Remove a button from a poll")
.addOption(OptionType.CHANNEL, "channel", "Channel this poll is in", true, true)
.addOption(OptionType.INTEGER, "message_id", "Id of the poll you're removing a button from", true)
.addOption(OptionType.STRING, "button_name", "Name of the button you're removing"),
new SubcommandData("open", "Open a poll")
.addOption(OptionType.CHANNEL, "channel", "Channel this poll is in", true, true)
.addOption(OptionType.INTEGER, "message_id", "Id of the poll you're opening", true),
new SubcommandData("close", "Close a poll")
.addOption(OptionType.CHANNEL, "channel", "Channel this poll is in", true, true)
.addOption(OptionType.INTEGER, "message_id", "Id of the poll you're closing", true),
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.");
}
}
}
@Override
public String getName() {
return "poll";
}
@Override
public void execute(SlashCommandInteractionEvent event) {
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())) {
event.replyEmbeds(Util.noPermission(getName())).setEphemeral(true).queue();
return;
}
String subcommandName = event.getInteraction().getSubcommandName();
if (subcommandName == null) {
Logger.severe("No subcommand found for %", getName());
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)
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);
}
}
@Override
public String getHelpMessage() {
return null;
}
}
@@ -0,0 +1,27 @@
package com.alttd.commandManager.commands.PollCommand;
import com.alttd.commandManager.DiscordCommand;
import com.alttd.commandManager.SubCommand;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
public class SubCommandAdd extends SubCommand {
protected SubCommandAdd(DiscordCommand parent) {
super(parent);
}
@Override
public String getName() {
return "add";
}
@Override
public void execute(SlashCommandInteractionEvent event) {
}
@Override
public String getHelpMessage() {
return null;
}
}
@@ -0,0 +1,26 @@
package com.alttd.commandManager.commands.PollCommand;
import com.alttd.commandManager.DiscordCommand;
import com.alttd.commandManager.SubCommand;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
public class SubCommandAddButton extends SubCommand {
protected SubCommandAddButton(DiscordCommand parent) {
super(parent);
}
@Override
public String getName() {
return "add_button";
}
@Override
public void execute(SlashCommandInteractionEvent event) {
}
@Override
public String getHelpMessage() {
return null;
}
}
@@ -0,0 +1,26 @@
package com.alttd.commandManager.commands.PollCommand;
import com.alttd.commandManager.DiscordCommand;
import com.alttd.commandManager.SubCommand;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
public class SubCommandClose extends SubCommand {
protected SubCommandClose(DiscordCommand parent) {
super(parent);
}
@Override
public String getName() {
return "close";
}
@Override
public void execute(SlashCommandInteractionEvent event) {
}
@Override
public String getHelpMessage() {
return null;
}
}
@@ -0,0 +1,26 @@
package com.alttd.commandManager.commands.PollCommand;
import com.alttd.commandManager.DiscordCommand;
import com.alttd.commandManager.SubCommand;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
public class SubCommandEditDescription extends SubCommand {
protected SubCommandEditDescription(DiscordCommand parent) {
super(parent);
}
@Override
public String getName() {
return "edit_description";
}
@Override
public void execute(SlashCommandInteractionEvent event) {
}
@Override
public String getHelpMessage() {
return null;
}
}
@@ -0,0 +1,26 @@
package com.alttd.commandManager.commands.PollCommand;
import com.alttd.commandManager.DiscordCommand;
import com.alttd.commandManager.SubCommand;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
public class SubCommandEditTitle extends SubCommand {
protected SubCommandEditTitle(DiscordCommand parent) {
super(parent);
}
@Override
public String getName() {
return "edit_title";
}
@Override
public void execute(SlashCommandInteractionEvent event) {
}
@Override
public String getHelpMessage() {
return null;
}
}
@@ -0,0 +1,26 @@
package com.alttd.commandManager.commands.PollCommand;
import com.alttd.commandManager.DiscordCommand;
import com.alttd.commandManager.SubCommand;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
public class SubCommandOpen extends SubCommand {
protected SubCommandOpen(DiscordCommand parent) {
super(parent);
}
@Override
public String getName() {
return "open";
}
@Override
public void execute(SlashCommandInteractionEvent event) {
}
@Override
public String getHelpMessage() {
return null;
}
}
@@ -0,0 +1,26 @@
package com.alttd.commandManager.commands.PollCommand;
import com.alttd.commandManager.DiscordCommand;
import com.alttd.commandManager.SubCommand;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
public class SubCommandRemoveButton extends SubCommand {
protected SubCommandRemoveButton(DiscordCommand parent) {
super(parent);
}
@Override
public String getName() {
return "remove_button";
}
@Override
public void execute(SlashCommandInteractionEvent event) {
}
@Override
public String getHelpMessage() {
return null;
}
}
@@ -0,0 +1,26 @@
package com.alttd.commandManager.commands.PollCommand;
import com.alttd.commandManager.DiscordCommand;
import com.alttd.commandManager.SubCommand;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
public class SubCommandResults extends SubCommand {
protected SubCommandResults(DiscordCommand parent) {
super(parent);
}
@Override
public String getName() {
return "results";
}
@Override
public void execute(SlashCommandInteractionEvent event) {
}
@Override
public String getHelpMessage() {
return null;
}
}