diff --git a/src/main/java/com/beanbeanjuice/cafebot/CafeBot.java b/src/main/java/com/beanbeanjuice/cafebot/CafeBot.java index 096c87d1..946aa839 100644 --- a/src/main/java/com/beanbeanjuice/cafebot/CafeBot.java +++ b/src/main/java/com/beanbeanjuice/cafebot/CafeBot.java @@ -18,15 +18,14 @@ import com.beanbeanjuice.cafebot.commands.generic.*; import com.beanbeanjuice.cafebot.commands.interaction.*; import com.beanbeanjuice.cafebot.commands.moderation.ClearChatCommand; +import com.beanbeanjuice.cafebot.commands.settings.AICommand; +import com.beanbeanjuice.cafebot.commands.settings.welcome.WelcomeCommand; import com.beanbeanjuice.cafebot.commands.social.MemberCountCommand; import com.beanbeanjuice.cafebot.commands.social.vent.VentCommand; import com.beanbeanjuice.cafebot.commands.twitch.TwitchCommand; import com.beanbeanjuice.cafebot.utility.commands.CommandHandler; import com.beanbeanjuice.cafebot.utility.helper.Helper; -import com.beanbeanjuice.cafebot.utility.listeners.AIResponseListener; -import com.beanbeanjuice.cafebot.utility.listeners.BotAddListener; -import com.beanbeanjuice.cafebot.utility.listeners.BotRemoveListener; -import com.beanbeanjuice.cafebot.utility.listeners.CountingListener; +import com.beanbeanjuice.cafebot.utility.listeners.*; import com.beanbeanjuice.cafebot.utility.logging.LogLevel; import com.beanbeanjuice.cafebot.utility.logging.LogManager; import com.beanbeanjuice.cafeapi.wrapper.CafeAPI; @@ -45,6 +44,7 @@ import net.dv8tion.jda.api.entities.Activity; import net.dv8tion.jda.api.entities.MessageEmbed; import net.dv8tion.jda.api.entities.User; +import net.dv8tion.jda.api.hooks.ListenerAdapter; import net.dv8tion.jda.api.requests.GatewayIntent; import net.dv8tion.jda.api.requests.restaction.CacheRestAction; import org.jetbrains.annotations.NotNull; @@ -186,7 +186,6 @@ private void setupCommands() { // Fun new AvatarCommand(this), - new AICommand(this), new BannerCommand(this), new BirthdayCommand(this), new MemeCommand(this), @@ -249,7 +248,11 @@ private void setupCommands() { new TwitchCommand(this), // Moderation - new ClearChatCommand(this) + new ClearChatCommand(this), + + // Settings + new AICommand(this), + new WelcomeCommand(this) // new EmbedCommand(this) ); @@ -259,6 +262,10 @@ private void setupCommands() { this.twitchHandler = new TwitchHandler(System.getenv("CAFEBOT_TWITCH_ACCESS_TOKEN"), this); } + public void addEventListener(final ListenerAdapter listener) { + this.JDA.addEventListener(listener); + } + private void setupListeners() { this.aiResponseListener = new AIResponseListener(this); this.JDA.addEventListener( @@ -267,7 +274,8 @@ private void setupListeners() { new CountingListener(this), new HelpListener(commandHandler, helpHandler), new TicTacToeListener(cafeAPI.getWinStreaksEndpoint()), - aiResponseListener + aiResponseListener, + new WelcomeListener(this) ); } diff --git a/src/main/java/com/beanbeanjuice/cafebot/commands/basic/EmbedCommand.java b/src/main/java/com/beanbeanjuice/cafebot/commands/basic/EmbedCommand.java deleted file mode 100644 index b0e13e04..00000000 --- a/src/main/java/com/beanbeanjuice/cafebot/commands/basic/EmbedCommand.java +++ /dev/null @@ -1,96 +0,0 @@ -package com.beanbeanjuice.cafebot.commands.basic; - -import com.beanbeanjuice.cafebot.CafeBot; -import com.beanbeanjuice.cafebot.utility.commands.Command; -import com.beanbeanjuice.cafebot.utility.commands.ISubCommand; -import net.dv8tion.jda.api.EmbedBuilder; -import net.dv8tion.jda.api.events.interaction.ModalInteractionEvent; -import net.dv8tion.jda.api.interactions.commands.build.OptionData; -import net.dv8tion.jda.api.interactions.components.text.TextInput; -import net.dv8tion.jda.api.interactions.components.text.TextInputStyle; -import net.dv8tion.jda.api.interactions.modals.Modal; -import net.dv8tion.jda.api.interactions.modals.ModalMapping; - -import java.util.Optional; - -public class EmbedCommand extends Command implements ISubCommand { - - public EmbedCommand(final CafeBot cafeBot) { - super(cafeBot); - } - - @Override - public void handleModal(ModalInteractionEvent event) { - Optional subjectMapping = Optional.ofNullable(event.getValue("subject")); - Optional bodyMapping = Optional.ofNullable(event.getValue("body")); - - EmbedBuilder embedBuilder = new EmbedBuilder(); - - subjectMapping.ifPresent((mapping) -> embedBuilder.setTitle(mapping.getAsString())); - bodyMapping.ifPresent((mapping) -> embedBuilder.setDescription(mapping.getAsString())); - - event.getChannel().sendMessageEmbeds(embedBuilder.build()).queue(); - event.getHook().sendMessage("Successfully created the embed!").queue(); - } - - @Override - public String getName() { - return "embed"; - } - - @Override - public String getDescription() { - return "Create an embed in the current channel."; - } - - @Override - public OptionData[] getOptions() { - return new OptionData[0]; - } - - - -// @Override -// public Permission[] getPermissions() { -// return new Permission[] { -// Permission.MESSAGE_EMBED_LINKS, -// Permission.MANAGE_CHANNEL -// }; -// } - -// @Override -// public boolean isNSFW() { -// return false; -// } -// -// @Override -// public boolean allowDM() { -// return true; -// } - - @Override - public boolean isModal() { - return true; - } - - @Override - public Modal getModal() { - TextInput subject = TextInput.create("subject", "Subject", TextInputStyle.SHORT) - .setPlaceholder("Subject of this ticket") - .setMinLength(10) - .setMaxLength(100) // or setRequiredRange(10, 100) - .build(); - - TextInput body = TextInput.create("body", "Body", TextInputStyle.PARAGRAPH) - .setPlaceholder("Your concerns go here") - .setMinLength(30) - .setMaxLength(1000) - .build(); - - return Modal.create(this.getName(), this.getName().toUpperCase()) - .addActionRow(subject) - .addActionRow(body) - .build(); - } - -} diff --git a/src/main/java/com/beanbeanjuice/cafebot/commands/fun/AICommand.java b/src/main/java/com/beanbeanjuice/cafebot/commands/settings/AICommand.java similarity index 98% rename from src/main/java/com/beanbeanjuice/cafebot/commands/fun/AICommand.java rename to src/main/java/com/beanbeanjuice/cafebot/commands/settings/AICommand.java index c40967d6..345603df 100644 --- a/src/main/java/com/beanbeanjuice/cafebot/commands/fun/AICommand.java +++ b/src/main/java/com/beanbeanjuice/cafebot/commands/settings/AICommand.java @@ -1,4 +1,4 @@ -package com.beanbeanjuice.cafebot.commands.fun; +package com.beanbeanjuice.cafebot.commands.settings; import com.beanbeanjuice.cafeapi.wrapper.endpoints.guilds.GuildInformationType; import com.beanbeanjuice.cafebot.CafeBot; diff --git a/src/main/java/com/beanbeanjuice/cafebot/commands/settings/welcome/WelcomeCommand.java b/src/main/java/com/beanbeanjuice/cafebot/commands/settings/welcome/WelcomeCommand.java new file mode 100644 index 00000000..853fb7c1 --- /dev/null +++ b/src/main/java/com/beanbeanjuice/cafebot/commands/settings/welcome/WelcomeCommand.java @@ -0,0 +1,75 @@ +package com.beanbeanjuice.cafebot.commands.settings.welcome; + +import com.beanbeanjuice.cafebot.CafeBot; +import com.beanbeanjuice.cafebot.commands.settings.welcome.channel.WelcomeChannelRemoveSubCommand; +import com.beanbeanjuice.cafebot.commands.settings.welcome.channel.WelcomeChannelSetSubCommand; +import com.beanbeanjuice.cafebot.commands.settings.welcome.message.WelcomeMessageRemoveSubCommand; +import com.beanbeanjuice.cafebot.commands.settings.welcome.message.WelcomeMessageSetSubCommand; +import com.beanbeanjuice.cafebot.utility.commands.*; +import net.dv8tion.jda.api.Permission; + +public class WelcomeCommand extends Command implements ICommand { + + public WelcomeCommand(final CafeBot cafeBot) { + super(cafeBot); + } + + @Override + public String getName() { + return "welcome"; + } + + @Override + public String getDescription() { + return "Edit the welcome settings!"; + } + + @Override + public CommandCategory getCategory() { + return CommandCategory.SETTINGS; + } + + @Override + public Permission[] getPermissions() { + return new Permission[] { + Permission.MANAGE_SERVER + }; + } + + @Override + public boolean isEphemeral() { + return true; + } + + @Override + public boolean isNSFW() { + return false; + } + + @Override + public boolean allowDM() { + return false; + } + + @Override + public ISubCommand[] getSubCommands() { + return ICommand.super.getSubCommands(); + } + + @Override + public SubCommandGroup[] getSubCommandGroups() { + SubCommandGroup channelGroup = new SubCommandGroup("channel", "Edit the welcome channel."); + channelGroup.addSubCommands(new ISubCommand[] { + new WelcomeChannelSetSubCommand(cafeBot), + new WelcomeChannelRemoveSubCommand(cafeBot) + }); + + SubCommandGroup messageGroup = new SubCommandGroup("message", "Edit the welcome message."); + messageGroup.addSubCommands(new ISubCommand[] { + new WelcomeMessageSetSubCommand(cafeBot), + new WelcomeMessageRemoveSubCommand(cafeBot) + }); + + return new SubCommandGroup[] { channelGroup, messageGroup }; + } +} diff --git a/src/main/java/com/beanbeanjuice/cafebot/commands/settings/welcome/channel/WelcomeChannelRemoveSubCommand.java b/src/main/java/com/beanbeanjuice/cafebot/commands/settings/welcome/channel/WelcomeChannelRemoveSubCommand.java new file mode 100644 index 00000000..de9043b3 --- /dev/null +++ b/src/main/java/com/beanbeanjuice/cafebot/commands/settings/welcome/channel/WelcomeChannelRemoveSubCommand.java @@ -0,0 +1,44 @@ +package com.beanbeanjuice.cafebot.commands.settings.welcome.channel; + +import com.beanbeanjuice.cafeapi.wrapper.endpoints.guilds.GuildInformationType; +import com.beanbeanjuice.cafebot.CafeBot; +import com.beanbeanjuice.cafebot.utility.commands.Command; +import com.beanbeanjuice.cafebot.utility.commands.ISubCommand; +import com.beanbeanjuice.cafebot.utility.helper.Helper; +import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; + +public class WelcomeChannelRemoveSubCommand extends Command implements ISubCommand { + + public WelcomeChannelRemoveSubCommand(final CafeBot cafeBot) { + super(cafeBot); + } + + @Override + public void handle(SlashCommandInteractionEvent event) { + cafeBot.getCafeAPI().getGuildsEndpoint().updateGuildInformation(event.getGuild().getId(), GuildInformationType.WELCOME_CHANNEL_ID, "0") + .thenAcceptAsync((ignored) -> { + event.getHook().sendMessageEmbeds(Helper.successEmbed( + "Welcome Channel Removed", + "The welcome channel has been removed!" + )).queue(); + }) + .exceptionallyAsync((e) -> { + event.getHook().sendMessageEmbeds(Helper.errorEmbed( + "Error Removing Welcome Channel", + String.format("There was an error setting the welcome channel: %s", e.getMessage()) + )).queue(); + return null; + }); + } + + @Override + public String getName() { + return "remove"; + } + + @Override + public String getDescription() { + return "Remove the welcome channel."; + } + +} diff --git a/src/main/java/com/beanbeanjuice/cafebot/commands/settings/welcome/channel/WelcomeChannelSetSubCommand.java b/src/main/java/com/beanbeanjuice/cafebot/commands/settings/welcome/channel/WelcomeChannelSetSubCommand.java new file mode 100644 index 00000000..cd17b2d7 --- /dev/null +++ b/src/main/java/com/beanbeanjuice/cafebot/commands/settings/welcome/channel/WelcomeChannelSetSubCommand.java @@ -0,0 +1,60 @@ +package com.beanbeanjuice.cafebot.commands.settings.welcome.channel; + +import com.beanbeanjuice.cafeapi.wrapper.endpoints.guilds.GuildInformationType; +import com.beanbeanjuice.cafebot.CafeBot; +import com.beanbeanjuice.cafebot.utility.commands.Command; +import com.beanbeanjuice.cafebot.utility.commands.ISubCommand; +import com.beanbeanjuice.cafebot.utility.helper.Helper; +import net.dv8tion.jda.api.entities.channel.unions.GuildChannelUnion; +import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; +import net.dv8tion.jda.api.interactions.commands.OptionMapping; +import net.dv8tion.jda.api.interactions.commands.OptionType; +import net.dv8tion.jda.api.interactions.commands.build.OptionData; + +import java.util.Optional; + +public class WelcomeChannelSetSubCommand extends Command implements ISubCommand { + + public WelcomeChannelSetSubCommand(final CafeBot cafeBot) { + super(cafeBot); + } + + @Override + public void handle(SlashCommandInteractionEvent event) { + Optional channelMapping = Optional.ofNullable(event.getOption("channel")); + GuildChannelUnion channel = channelMapping.map(OptionMapping::getAsChannel).orElse((GuildChannelUnion) event.getChannel()); + + cafeBot.getCafeAPI().getGuildsEndpoint().updateGuildInformation(event.getGuild().getId(), GuildInformationType.WELCOME_CHANNEL_ID, channel.getId()) + .thenAcceptAsync((ignored) -> { + event.getHook().sendMessageEmbeds(Helper.successEmbed( + "Welcome Channel Set", + String.format("The welcome channel has been set to %s.", channel.getAsMention()) + )).queue(); + }) + .exceptionallyAsync((e) -> { + event.getHook().sendMessageEmbeds(Helper.errorEmbed( + "Error Setting Welcome Channel", + String.format("There was an error setting the welcome channel: %s", e.getMessage()) + )).queue(); + return null; + }); + } + + @Override + public String getName() { + return "set"; + } + + @Override + public String getDescription() { + return "Set the welcome channel!"; + } + + @Override + public OptionData[] getOptions() { + return new OptionData[] { + new OptionData(OptionType.CHANNEL, "channel", "The channel to set the welcome channel to.", false) + }; + } + +} diff --git a/src/main/java/com/beanbeanjuice/cafebot/commands/settings/welcome/message/WelcomeMessageModalListener.java b/src/main/java/com/beanbeanjuice/cafebot/commands/settings/welcome/message/WelcomeMessageModalListener.java new file mode 100644 index 00000000..bca135da --- /dev/null +++ b/src/main/java/com/beanbeanjuice/cafebot/commands/settings/welcome/message/WelcomeMessageModalListener.java @@ -0,0 +1,52 @@ +package com.beanbeanjuice.cafebot.commands.settings.welcome.message; + +import com.beanbeanjuice.cafeapi.wrapper.endpoints.welcomes.GuildWelcome; +import com.beanbeanjuice.cafeapi.wrapper.endpoints.welcomes.WelcomesEndpoint; +import com.beanbeanjuice.cafebot.utility.helper.Helper; +import com.beanbeanjuice.cafebot.utility.listeners.WelcomeListener; +import net.dv8tion.jda.api.events.interaction.ModalInteractionEvent; +import net.dv8tion.jda.api.hooks.ListenerAdapter; + +import java.util.Map; + +public class WelcomeMessageModalListener extends ListenerAdapter { + + private final WelcomesEndpoint welcomesEndpoint; + + public WelcomeMessageModalListener(final WelcomesEndpoint welcomesEndpoint) { + this.welcomesEndpoint = welcomesEndpoint; + } + + @Override + public void onModalInteraction(ModalInteractionEvent event) { + if (!event.getModalId().startsWith("cafeBot:modal:welcome:message:")) return; + + Map values = Helper.modalValuesToMap(event.getValues()); + + GuildWelcome guildWelcome = new GuildWelcome( + event.getGuild().getId(), + values.get("description"), + values.get("thumbnail-url"), + values.get("image-url"), + values.get("message") + ); + + welcomesEndpoint.updateGuildWelcome(guildWelcome) + .exceptionallyComposeAsync((e) -> welcomesEndpoint.createGuildWelcome(guildWelcome)) + .thenAcceptAsync((ignored) -> { + guildWelcome.getMessage().ifPresentOrElse( + (message) -> event.reply(message.replace("{user}", event.getUser().getAsMention())).addEmbeds(WelcomeListener.getWelcomeEmbed(guildWelcome, event.getUser())).setEphemeral(true).queue(), + () -> event.replyEmbeds(WelcomeListener.getWelcomeEmbed(guildWelcome, event.getUser())).setEphemeral(true).queue() + ); + }) + .exceptionallyAsync((e) -> { + event.replyEmbeds(Helper.errorEmbed( + "Error Setting Welcome Message", + String.format("There was an error setting the welcome message: %s", e.getMessage()) + )).queue(); + return null; + }); + + } + +} diff --git a/src/main/java/com/beanbeanjuice/cafebot/commands/settings/welcome/message/WelcomeMessageRemoveSubCommand.java b/src/main/java/com/beanbeanjuice/cafebot/commands/settings/welcome/message/WelcomeMessageRemoveSubCommand.java new file mode 100644 index 00000000..44a0660b --- /dev/null +++ b/src/main/java/com/beanbeanjuice/cafebot/commands/settings/welcome/message/WelcomeMessageRemoveSubCommand.java @@ -0,0 +1,42 @@ +package com.beanbeanjuice.cafebot.commands.settings.welcome.message; + +import com.beanbeanjuice.cafebot.CafeBot; +import com.beanbeanjuice.cafebot.utility.commands.Command; +import com.beanbeanjuice.cafebot.utility.commands.ISubCommand; +import com.beanbeanjuice.cafebot.utility.helper.Helper; +import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; + +public class WelcomeMessageRemoveSubCommand extends Command implements ISubCommand { + + public WelcomeMessageRemoveSubCommand(final CafeBot cafeBot) { + super(cafeBot); + } + + @Override + public void handle(SlashCommandInteractionEvent event) { + cafeBot.getCafeAPI().getWelcomesEndpoint().deleteGuildWelcome(event.getGuild().getId()) + .thenAcceptAsync((ignored) -> { + event.getHook().sendMessageEmbeds(Helper.successEmbed( + "Welcome Message Removed", + "The welcome message has been successfully removed." + )).queue(); + }) + .exceptionallyAsync((e) -> { + event.getHook().sendMessageEmbeds(Helper.errorEmbed( + "Error Removing Welcome Message", + String.format("There was an error removing the welcome message: %s", e.getMessage()) + )).queue(); + return null; + }); + } + + @Override + public String getName() { + return "remove"; + } + + @Override + public String getDescription() { + return "Remove the welcome message."; + } +} diff --git a/src/main/java/com/beanbeanjuice/cafebot/commands/settings/welcome/message/WelcomeMessageSetSubCommand.java b/src/main/java/com/beanbeanjuice/cafebot/commands/settings/welcome/message/WelcomeMessageSetSubCommand.java new file mode 100644 index 00000000..9d5048b6 --- /dev/null +++ b/src/main/java/com/beanbeanjuice/cafebot/commands/settings/welcome/message/WelcomeMessageSetSubCommand.java @@ -0,0 +1,75 @@ +package com.beanbeanjuice.cafebot.commands.settings.welcome.message; + +import com.beanbeanjuice.cafebot.CafeBot; +import com.beanbeanjuice.cafebot.utility.commands.Command; +import com.beanbeanjuice.cafebot.utility.commands.ISubCommand; +import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; +import net.dv8tion.jda.api.interactions.components.ActionRow; +import net.dv8tion.jda.api.interactions.components.text.TextInput; +import net.dv8tion.jda.api.interactions.components.text.TextInputStyle; +import net.dv8tion.jda.api.interactions.modals.Modal; + +public class WelcomeMessageSetSubCommand extends Command implements ISubCommand { + + public WelcomeMessageSetSubCommand(final CafeBot cafeBot) { + super(cafeBot); + + cafeBot.addEventListener(new WelcomeMessageModalListener(cafeBot.getCafeAPI().getWelcomesEndpoint())); + } + + @Override + public void handle(SlashCommandInteractionEvent event) { + TextInput title = TextInput.create("title", "Title", TextInputStyle.SHORT) + .setPlaceholder("Welcome to the server, {user}!") + .setRequired(false) + .build(); + + TextInput description = TextInput.create("description", "Description", TextInputStyle.PARAGRAPH) + .setPlaceholder("Welcome {user} to the server!\nThese are our rules...") + .setRequired(false) + .build(); + + TextInput message = TextInput.create("message", "Message", TextInputStyle.PARAGRAPH) + .setPlaceholder("@SomeRole, {user} just joined!") + .setRequired(false) + .build(); + + TextInput largeImage = TextInput.create("image-url", "Large Image", TextInputStyle.SHORT) + .setPlaceholder("https://some.image.url") + .setRequired(false) + .build(); + + TextInput smallImage = TextInput.create("thumbnail-url", "Small Image", TextInputStyle.SHORT) + .setPlaceholder("https://some.image.url") + .setRequired(false) + .build(); + + Modal modal = Modal.create("cafeBot:modal:welcome:message:" + this.getName(), "Edit Welcome Message Content") + .addComponents( + ActionRow.of(title), + ActionRow.of(description), + ActionRow.of(message), + ActionRow.of(largeImage), + ActionRow.of(smallImage) + ) + .build(); + + event.replyModal(modal).queue(); + } + + @Override + public String getName() { + return "set"; + } + + @Override + public String getDescription() { + return "Set the welcome message."; + } + + @Override + public boolean isModal() { + return true; + } + +} diff --git a/src/main/java/com/beanbeanjuice/cafebot/utility/commands/CommandCategory.java b/src/main/java/com/beanbeanjuice/cafebot/utility/commands/CommandCategory.java index f7269114..9a808bed 100644 --- a/src/main/java/com/beanbeanjuice/cafebot/utility/commands/CommandCategory.java +++ b/src/main/java/com/beanbeanjuice/cafebot/utility/commands/CommandCategory.java @@ -11,7 +11,8 @@ public enum CommandCategory { INTERACTION("Hugs, waves, slaps, and more!", "https://cdn.beanbeanjuice.com/images/cafeBot/category_type/interaction.png"), TWITCH("Commands used for twitch.", "https://cdn.beanbeanjuice.com/images/cafeBot/category_type/twitch.jpg"), SOCIAL("Hmm... I just need to let it out... you know?", "https://cdn.beanbeanjuice.com/images/cafeBot/category_type/social.gif"), - MODERATION("Commands used for moderation.", "https://cdn.beanbeanjuice.com/images/cafeBot/category_type/moderation.png"); + MODERATION("Commands used for moderation.", "https://cdn.beanbeanjuice.com/images/cafeBot/category_type/moderation.png"), + SETTINGS("Commands used for bot settings.", "https://cdn.beanbeanjuice.com/images/cafeBot/category_type/settings.gif"); @Getter private final String description; @Getter private final String link; diff --git a/src/main/java/com/beanbeanjuice/cafebot/utility/commands/CommandHandler.java b/src/main/java/com/beanbeanjuice/cafebot/utility/commands/CommandHandler.java index 22e0f655..f9a6deb6 100644 --- a/src/main/java/com/beanbeanjuice/cafebot/utility/commands/CommandHandler.java +++ b/src/main/java/com/beanbeanjuice/cafebot/utility/commands/CommandHandler.java @@ -118,24 +118,16 @@ private void handleSubCommandWithoutGroup(final String subCommandName, final ICo } private void handleSubCommand(final ISubCommand subCommand, final ICommand command, final SlashCommandInteractionEvent event) { - if (subCommand.isModal()) { - event.replyModal(subCommand.getModal()).queue(); - } else { - event.deferReply(command.isEphemeral()).queue(); - subCommand.handle(event); - } + if (!subCommand.isModal()) event.deferReply(command.isEphemeral()).queue(); + subCommand.handle(event); cafeBot.increaseCommandsRun(); } private void handleCommand(final ICommand command, final SlashCommandInteractionEvent event) { - if (command.isModal()) { - event.replyModal(command.getModal()).queue(); - } else { - event.deferReply(command.isEphemeral()).queue(); - command.handle(event); - } + if (!command.isModal()) event.deferReply(command.isEphemeral()).queue(); + command.handle(event); cafeBot.increaseCommandsRun(); } diff --git a/src/main/java/com/beanbeanjuice/cafebot/utility/commands/ICommand.java b/src/main/java/com/beanbeanjuice/cafebot/utility/commands/ICommand.java index fd0325f7..e6a8ce77 100644 --- a/src/main/java/com/beanbeanjuice/cafebot/utility/commands/ICommand.java +++ b/src/main/java/com/beanbeanjuice/cafebot/utility/commands/ICommand.java @@ -12,7 +12,6 @@ public interface ICommand { default void handle(SlashCommandInteractionEvent event) { }; - default void handleModal(ModalInteractionEvent event) { }; String getName(); @@ -35,6 +34,5 @@ public interface ICommand { default SubCommandGroup[] getSubCommandGroups() { return new SubCommandGroup[0]; } default boolean isModal() { return false; } - default Modal getModal() { return null; } } diff --git a/src/main/java/com/beanbeanjuice/cafebot/utility/commands/ISubCommand.java b/src/main/java/com/beanbeanjuice/cafebot/utility/commands/ISubCommand.java index 7d5e01f9..1df3a9f8 100644 --- a/src/main/java/com/beanbeanjuice/cafebot/utility/commands/ISubCommand.java +++ b/src/main/java/com/beanbeanjuice/cafebot/utility/commands/ISubCommand.java @@ -1,17 +1,14 @@ package com.beanbeanjuice.cafebot.utility.commands; -import net.dv8tion.jda.api.events.interaction.ModalInteractionEvent; import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; import net.dv8tion.jda.api.interactions.commands.build.OptionData; -import net.dv8tion.jda.api.interactions.modals.Modal; import java.util.ArrayList; import java.util.HashMap; public interface ISubCommand { - default void handle(SlashCommandInteractionEvent event) { } - default void handleModal(ModalInteractionEvent event) { } + void handle(SlashCommandInteractionEvent event); String getName(); @@ -23,6 +20,4 @@ default void handleModal(ModalInteractionEvent event) { } default boolean isModal() { return false; } - default Modal getModal() { return null; } - } diff --git a/src/main/java/com/beanbeanjuice/cafebot/utility/helper/Helper.java b/src/main/java/com/beanbeanjuice/cafebot/utility/helper/Helper.java index 96f110a0..c546b282 100644 --- a/src/main/java/com/beanbeanjuice/cafebot/utility/helper/Helper.java +++ b/src/main/java/com/beanbeanjuice/cafebot/utility/helper/Helper.java @@ -6,12 +6,17 @@ import net.dv8tion.jda.api.entities.*; import net.dv8tion.jda.api.entities.channel.ChannelType; import net.dv8tion.jda.api.entities.channel.unions.MessageChannelUnion; +import net.dv8tion.jda.api.events.interaction.ModalInteractionEvent; +import net.dv8tion.jda.api.interactions.modals.ModalMapping; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.awt.*; import java.io.File; import java.io.IOException; +import java.util.HashMap; +import java.util.List; +import java.util.Map; import java.util.Random; import java.util.concurrent.TimeUnit; @@ -186,19 +191,10 @@ public static double getRandomDouble(final int minimum, final int maximum) { return minimum + new Random().nextDouble() * maximum; } - /** - * Private message's a specified {@link User}. - * @param user The {@link User} to be messaged. - * @param message The contents of the message. - */ - public static void pmUser(@NotNull User user, @NotNull String message) { - user.openPrivateChannel().flatMap(channel -> channel.sendMessage(message)).queue(); + public static void pmUser(final User user, final MessageEmbed embed) { + user.openPrivateChannel().flatMap(channel -> channel.sendMessageEmbeds(embed)).queue(); } - - - - /** * Generates a Random Alpha-Numeric {@link String}. * @param n The length of the random alphanumeric {@link String}. @@ -294,4 +290,10 @@ public static String[] removeCommaSpace(String input) { return input.split(",\\s*"); } + public static Map modalValuesToMap(final List mappings) { + Map modalMap = new HashMap<>(); + mappings.forEach((mapping) -> modalMap.put(mapping.getId(), mapping.getAsString())); + return modalMap; + } + } diff --git a/src/main/java/com/beanbeanjuice/cafebot/utility/listeners/WelcomeListener.java b/src/main/java/com/beanbeanjuice/cafebot/utility/listeners/WelcomeListener.java new file mode 100644 index 00000000..18ac9c2c --- /dev/null +++ b/src/main/java/com/beanbeanjuice/cafebot/utility/listeners/WelcomeListener.java @@ -0,0 +1,75 @@ +package com.beanbeanjuice.cafebot.utility.listeners; + +import com.beanbeanjuice.cafeapi.wrapper.endpoints.guilds.GuildInformation; +import com.beanbeanjuice.cafeapi.wrapper.endpoints.guilds.GuildInformationType; +import com.beanbeanjuice.cafeapi.wrapper.endpoints.welcomes.GuildWelcome; +import com.beanbeanjuice.cafebot.CafeBot; +import com.beanbeanjuice.cafebot.utility.helper.Helper; +import net.dv8tion.jda.api.EmbedBuilder; +import net.dv8tion.jda.api.entities.MessageEmbed; +import net.dv8tion.jda.api.entities.User; +import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; +import net.dv8tion.jda.api.events.guild.member.GuildMemberJoinEvent; +import net.dv8tion.jda.api.hooks.ListenerAdapter; + +import java.util.concurrent.CompletableFuture; + +public class WelcomeListener extends ListenerAdapter { + + private final CafeBot cafeBot; + + public WelcomeListener(final CafeBot cafeBot) { + this.cafeBot = cafeBot; + } + + @Override + public void onGuildMemberJoin(GuildMemberJoinEvent event) { + String guildID = event.getGuild().getId(); + + CompletableFuture informationFuture = cafeBot.getCafeAPI().getGuildsEndpoint().getGuildInformation(guildID); + CompletableFuture welcomeFuture = cafeBot.getCafeAPI().getWelcomesEndpoint().getGuildWelcome(guildID); + + informationFuture.thenCombineAsync(welcomeFuture, (information, welcome) -> { + if (information.getSetting(GuildInformationType.WELCOME_CHANNEL_ID).equals("0")) return false; + + TextChannel channel = event.getGuild().getTextChannelById(information.getSetting(GuildInformationType.WELCOME_CHANNEL_ID)); + if (channel == null) return false; + + welcome.getMessage().ifPresentOrElse( + (message) -> channel.sendMessage(message.replace("{user}", event.getUser().getAsMention())).addEmbeds(getWelcomeEmbed(welcome, event.getUser())).queue(), + () -> channel.sendMessageEmbeds(getWelcomeEmbed(welcome, event.getUser())).queue() + ); + + return true; + }); + } + + public static MessageEmbed getWelcomeEmbed(final GuildWelcome guildWelcome, final User joiner) { + EmbedBuilder embedBuilder = new EmbedBuilder(); + + guildWelcome.getDescription().ifPresent((description) -> embedBuilder.setDescription(parseDescription(description, joiner))); + + // Attempts to set the thumbnail URL. + guildWelcome.getThumbnailURL().ifPresent((thumbnailURL) -> { + try { embedBuilder.setThumbnail(thumbnailURL); } + catch (IllegalArgumentException ignored) { } + }); + + // Attempts to set the image URL. + guildWelcome.getImageURL().ifPresent((imageURL) -> { + try { embedBuilder.setImage(imageURL); } + catch (IllegalArgumentException ignored) { } + }); + + embedBuilder.setColor(Helper.getRandomColor()); + embedBuilder.setAuthor(joiner.getName(), joiner.getAvatarUrl(), joiner.getAvatarUrl()); + return embedBuilder.build(); + } + + private static String parseDescription(String description, final User joiner) { + description = description.replace("{user}", joiner.getAsMention()); + description = description.replace("\\n", "\n"); + return description; + } + +}