AltitudeBot/src/main/java/com/alttd/util/Util.java

90 lines
3.8 KiB
Java
Raw Normal View History

2022-03-09 21:37:44 +00:00
package com.alttd.util;
import com.alttd.commandManager.CommandManager;
import com.alttd.commandManager.DiscordCommand;
import com.alttd.commandManager.ScopeInfo;
import com.alttd.commandManager.SubCommand;
2022-03-31 20:08:50 +00:00
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.JDA;
import net.dv8tion.jda.api.entities.*;
2022-03-31 20:08:50 +00:00
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;
2022-03-09 21:37:44 +00:00
2022-03-31 20:08:50 +00:00
import java.awt.*;
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;
public class Util {
public static List<Long> getGroupIds(Member member) {
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();
}
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);
}
2022-03-09 21:37:44 +00:00
}