Added embed templates and basic validation for channels

This commit is contained in:
Stijn 2022-04-08 21:56:58 +02:00
parent c44264b78c
commit 27122538e4

View File

@ -1,7 +1,6 @@
package com.alttd.util; package com.alttd.util;
import com.alttd.commandManager.CommandManager; import com.alttd.commandManager.CommandManager;
import com.alttd.commandManager.DiscordCommand;
import com.alttd.commandManager.ScopeInfo; import com.alttd.commandManager.ScopeInfo;
import com.alttd.commandManager.SubCommand; import com.alttd.commandManager.SubCommand;
import com.alttd.config.MessagesConfig; import com.alttd.config.MessagesConfig;
@ -10,15 +9,14 @@ import com.alttd.templates.Template;
import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.*; 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.OptionMapping;
import net.dv8tion.jda.api.interactions.commands.SlashCommandInteraction; import net.dv8tion.jda.api.interactions.commands.SlashCommandInteraction;
import net.dv8tion.jda.api.interactions.commands.build.SlashCommandData; import net.dv8tion.jda.api.interactions.commands.build.SlashCommandData;
import net.dv8tion.jda.api.requests.RestAction; import net.dv8tion.jda.api.requests.RestAction;
import org.jetbrains.annotations.NotNull;
import java.awt.*; import java.awt.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -74,6 +72,30 @@ public class Util {
.build(); .build();
} }
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();
}
public static void registerCommand(CommandManager commandManager, JDA jda, SlashCommandData slashCommandData, String commandName) { public static void registerCommand(CommandManager commandManager, JDA jda, SlashCommandData slashCommandData, String commandName) {
for (ScopeInfo info : commandManager.getActiveLocations(commandName)) { for (ScopeInfo info : commandManager.getActiveLocations(commandName)) {
switch (info.getScope()) { switch (info.getScope()) {
@ -82,7 +104,7 @@ public class Util {
Guild guildById = jda.getGuildById(info.getId()); Guild guildById = jda.getGuildById(info.getId());
if (guildById == null) if (guildById == null)
{ {
Logger.warning("Tried to add command % to invalid guild %", commandName, String.valueOf(info.getId())); Logger.warning("Tried to add command % to invalid guild %.", commandName, String.valueOf(info.getId()));
continue; continue;
} }
guildById.updateCommands().addCommands(slashCommandData).queue(RestAction.getDefaultSuccess(), Util::handleFailure); guildById.updateCommands().addCommands(slashCommandData).queue(RestAction.getDefaultSuccess(), Util::handleFailure);
@ -96,4 +118,38 @@ public class Util {
for (SubCommand subCommand : subCommands) for (SubCommand subCommand : subCommands)
subCommandMap.put(subCommand.getName(), subCommand); subCommandMap.put(subCommand.getName(), subCommand);
} }
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);
}
} }