2022-03-09 21:37:44 +00:00
|
|
|
package com.alttd.util;
|
|
|
|
|
|
2022-04-01 00:54:33 +00:00
|
|
|
import com.alttd.commandManager.CommandManager;
|
|
|
|
|
import com.alttd.commandManager.ScopeInfo;
|
2022-04-15 19:44:26 +00:00
|
|
|
import com.alttd.commandManager.SubOption;
|
2022-03-31 20:08:50 +00:00
|
|
|
import com.alttd.config.MessagesConfig;
|
2022-09-10 03:20:24 +00:00
|
|
|
import com.alttd.config.SettingsConfig;
|
2022-03-31 20:08:50 +00:00
|
|
|
import com.alttd.templates.Parser;
|
|
|
|
|
import com.alttd.templates.Template;
|
|
|
|
|
import net.dv8tion.jda.api.EmbedBuilder;
|
2022-04-01 00:54:33 +00:00
|
|
|
import net.dv8tion.jda.api.JDA;
|
|
|
|
|
import net.dv8tion.jda.api.entities.*;
|
2022-09-10 03:20:24 +00:00
|
|
|
import net.dv8tion.jda.api.interactions.commands.Command;
|
2022-03-31 20:08:50 +00:00
|
|
|
import net.dv8tion.jda.api.interactions.commands.OptionMapping;
|
|
|
|
|
import net.dv8tion.jda.api.interactions.commands.SlashCommandInteraction;
|
2022-04-11 01:29:59 +00:00
|
|
|
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
|
2022-04-01 00:54:33 +00:00
|
|
|
import net.dv8tion.jda.api.requests.RestAction;
|
2022-04-08 19:56:58 +00:00
|
|
|
import org.jetbrains.annotations.NotNull;
|
2022-03-09 21:37:44 +00:00
|
|
|
|
2022-03-31 20:08:50 +00:00
|
|
|
import java.awt.*;
|
2022-04-01 00:54:33 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.HashMap;
|
2022-03-09 21:37:44 +00:00
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.stream.Collectors;
|
2022-09-10 03:20:24 +00:00
|
|
|
import java.util.stream.Stream;
|
2022-03-09 21:37:44 +00:00
|
|
|
|
|
|
|
|
public class Util {
|
|
|
|
|
public static List<Long> getGroupIds(Member member) {
|
2022-04-01 00:54:33 +00:00
|
|
|
if (member == null)
|
|
|
|
|
return new ArrayList<>();
|
2022-03-09 21:37:44 +00:00
|
|
|
return member.getRoles().stream()
|
|
|
|
|
.map(Role::getIdLong)
|
|
|
|
|
.collect(Collectors.toList());
|
|
|
|
|
}
|
2022-03-31 20:08:50 +00:00
|
|
|
|
|
|
|
|
public static void handleFailure(Throwable failure) {
|
|
|
|
|
Logger.warning(failure.getMessage());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static MessageEmbed guildOnlyCommand(String commandName) {
|
|
|
|
|
return new EmbedBuilder()
|
|
|
|
|
.setTitle("Guild Only")
|
|
|
|
|
.setDescription(Parser.parse(MessagesConfig.GUILD_ONLY_MESSAGE, Template.of("command", commandName)))
|
|
|
|
|
.setColor(Color.RED)
|
|
|
|
|
.build();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static MessageEmbed noPermission(String commandName) {
|
|
|
|
|
return new EmbedBuilder()
|
|
|
|
|
.setTitle("No Permission")
|
|
|
|
|
.setDescription(Parser.parse(MessagesConfig.NO_PERMISSION_MESSAGE, Template.of("command", commandName)))
|
|
|
|
|
.setColor(Color.RED)
|
|
|
|
|
.build();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static MessageEmbed invalidCommand(String commandName, String error, SlashCommandInteraction interaction) {
|
|
|
|
|
EmbedBuilder embedBuilder = new EmbedBuilder()
|
|
|
|
|
.setTitle("Invalid Command")
|
|
|
|
|
.setDescription(Parser.parse(MessagesConfig.INVALID_COMMAND_ARGUMENTS,
|
|
|
|
|
Template.of("command", commandName),
|
|
|
|
|
Template.of("error", error)))
|
|
|
|
|
.setColor(Color.RED);
|
|
|
|
|
for (OptionMapping option : interaction.getOptions()) {
|
|
|
|
|
embedBuilder.addField(option.getName(), option.getAsString(), false);
|
|
|
|
|
}
|
|
|
|
|
return embedBuilder.build();
|
|
|
|
|
}
|
2022-04-01 00:54:33 +00:00
|
|
|
|
2022-04-08 17:51:12 +00:00
|
|
|
public static MessageEmbed invalidSubcommand(String subcommandName) {
|
|
|
|
|
return new EmbedBuilder()
|
|
|
|
|
.setTitle(MessagesConfig.INVALID_SUBCOMMAND)
|
|
|
|
|
.setDescription(Parser.parse(MessagesConfig.INVALID_SUBCOMMAND_DESC,
|
|
|
|
|
Template.of("subcommand", subcommandName)))
|
|
|
|
|
.setColor(Color.RED)
|
|
|
|
|
.build();
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-08 19:56:58 +00:00
|
|
|
public static MessageEmbed genericErrorEmbed(String title, String desc) {
|
|
|
|
|
return new EmbedBuilder()
|
|
|
|
|
.setTitle(title)
|
|
|
|
|
.setDescription(desc)
|
|
|
|
|
.setColor(Color.RED)
|
|
|
|
|
.build();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static MessageEmbed genericSuccessEmbed(String title, String desc) {
|
|
|
|
|
return new EmbedBuilder()
|
|
|
|
|
.setTitle(title)
|
|
|
|
|
.setDescription(desc)
|
|
|
|
|
.setColor(Color.GREEN)
|
|
|
|
|
.build();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static MessageEmbed genericWaitingEmbed(String title, String desc) {
|
|
|
|
|
return new EmbedBuilder()
|
|
|
|
|
.setTitle(title)
|
|
|
|
|
.setDescription(desc)
|
|
|
|
|
.setColor(Color.BLUE)
|
|
|
|
|
.build();
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-11 01:29:59 +00:00
|
|
|
public static void registerCommand(CommandManager commandManager, JDA jda, CommandData commandData, String commandName) {
|
2022-04-01 00:54:33 +00:00
|
|
|
for (ScopeInfo info : commandManager.getActiveLocations(commandName)) {
|
|
|
|
|
switch (info.getScope()) {
|
2022-09-10 03:20:24 +00:00
|
|
|
case GLOBAL -> {
|
|
|
|
|
if (SettingsConfig.DEBUG)
|
|
|
|
|
Logger.info("Loading command [" + commandName + "] on global.");
|
|
|
|
|
jda.upsertCommand(commandData).queue();
|
|
|
|
|
// jda.updateCommands().addCommands(commandData).queue();
|
|
|
|
|
}
|
|
|
|
|
case GUILD -> {
|
2022-04-01 00:54:33 +00:00
|
|
|
Guild guildById = jda.getGuildById(info.getId());
|
|
|
|
|
if (guildById == null)
|
|
|
|
|
{
|
2022-04-08 19:56:58 +00:00
|
|
|
Logger.warning("Tried to add command % to invalid guild %.", commandName, String.valueOf(info.getId()));
|
2022-04-01 00:54:33 +00:00
|
|
|
continue;
|
|
|
|
|
}
|
2022-09-10 03:20:24 +00:00
|
|
|
registerCommand(guildById, commandData, commandName);
|
2022-04-01 00:54:33 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-10 03:20:24 +00:00
|
|
|
public static void registerCommand(Guild guild, CommandData commandData, String commandName) {
|
|
|
|
|
if (SettingsConfig.DEBUG)
|
|
|
|
|
Logger.info("Loading command [" + commandName + "] on guild [" + guild.getName() + "].");
|
|
|
|
|
// guild.upsertCommand(commandData).queue();
|
|
|
|
|
guild.upsertCommand(commandData).queue(RestAction.getDefaultSuccess(), Util::handleFailure);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void deleteCommand(Guild guild, long id, String commandName) {
|
|
|
|
|
if (SettingsConfig.DEBUG)
|
|
|
|
|
Logger.info("Deleting command [" + commandName + "] on guild [" + guild.getName() + "].");
|
|
|
|
|
guild.deleteCommandById(id).queue();
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-15 19:44:26 +00:00
|
|
|
public static void registerSubOptions(HashMap<String, SubOption> subCommandMap, SubOption... subOptions) {
|
|
|
|
|
for (SubOption subOption : subOptions)
|
|
|
|
|
subCommandMap.put(subOption.getName(), subOption);
|
2022-04-01 00:54:33 +00:00
|
|
|
}
|
2022-04-08 19:56:58 +00:00
|
|
|
|
|
|
|
|
public static boolean validateGuildMessageChannel(SlashCommandInteraction interaction, GuildMessageChannel channel, ChannelType channelType, @NotNull Member member) {
|
|
|
|
|
if (channel == null) {
|
|
|
|
|
interaction.replyEmbeds(Util.genericErrorEmbed("Error", "Unable to find the TextChannel."))
|
|
|
|
|
.setEphemeral(true)
|
|
|
|
|
.queue();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (channelType != null && !channel.getType().equals(channelType)) {
|
|
|
|
|
interaction.replyEmbeds(Util.genericErrorEmbed("Error", "Please specify a " + channelType + " channel."))
|
|
|
|
|
.setEphemeral(true)
|
|
|
|
|
.queue();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (!channel.canTalk()) {
|
|
|
|
|
interaction.replyEmbeds(Util.genericErrorEmbed("Error", "I can't talk in this channel."))
|
|
|
|
|
.setEphemeral(true)
|
|
|
|
|
.queue();
|
|
|
|
|
}
|
|
|
|
|
if (!channel.canTalk(member)) {
|
|
|
|
|
interaction.replyEmbeds(Util.genericErrorEmbed("Error", "You can't talk in this channel."))
|
|
|
|
|
.setEphemeral(true)
|
|
|
|
|
.queue();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static EmbedBuilder getFirstEmbedBuilder(Message message) {
|
|
|
|
|
if (message.getEmbeds().isEmpty())
|
|
|
|
|
return null;
|
|
|
|
|
MessageEmbed messageEmbed = message.getEmbeds().get(0);
|
|
|
|
|
return new EmbedBuilder(messageEmbed);
|
|
|
|
|
}
|
2022-09-10 03:20:24 +00:00
|
|
|
|
|
|
|
|
public static Long parseLong(String message_id) {
|
|
|
|
|
long l;
|
|
|
|
|
try {
|
|
|
|
|
l = Long.parseLong(message_id);
|
|
|
|
|
} catch (NumberFormatException ignored) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return l;
|
|
|
|
|
}
|
2022-03-09 21:37:44 +00:00
|
|
|
}
|