diff --git a/src/main/java/net/Broken/BotListener.java b/src/main/java/net/Broken/BotListener.java index 15d7ea2..03214c4 100644 --- a/src/main/java/net/Broken/BotListener.java +++ b/src/main/java/net/Broken/BotListener.java @@ -8,10 +8,7 @@ import net.Broken.Tools.UserManager.Stats.UserStatsUtils; import net.Broken.audio.AudioM; import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.MessageBuilder; -import net.dv8tion.jda.api.entities.Guild; -import net.dv8tion.jda.api.entities.Message; -import net.dv8tion.jda.api.entities.Role; -import net.dv8tion.jda.api.entities.TextChannel; +import net.dv8tion.jda.api.entities.*; import net.dv8tion.jda.api.events.ReadyEvent; import net.dv8tion.jda.api.events.guild.GuildJoinEvent; import net.dv8tion.jda.api.events.guild.member.GuildMemberJoinEvent; @@ -29,7 +26,6 @@ import org.jetbrains.annotations.NotNull; import org.springframework.context.ApplicationContext; import java.awt.*; -import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -71,7 +67,7 @@ public class BotListener extends ListenerAdapter { String message = guildPref.getWelcomeMessage().replaceAll("@name", event.getMember().getAsMention()); logger.debug(message); chanel.sendMessage(message).queue(); - }else { + } else { logger.fatal("[" + event.getGuild().getName() + "] : Welcome chanel is null !"); } } @@ -123,8 +119,7 @@ public class BotListener extends ListenerAdapter { logger.debug("I'm alone, close audio connection."); AudioM.getInstance(event.getGuild()).stop(); } - } - else if (event.getMember().getUser() == MainBot.jda.getSelfUser()){ + } else if (event.getMember().getUser() == MainBot.jda.getSelfUser()) { AudioM.getInstance(event.getGuild()).clearLastButton(); } AutoVoiceChannel autoVoiceChannel = AutoVoiceChannel.getInstance(event.getGuild()); @@ -165,7 +160,29 @@ public class BotListener extends ListenerAdapter { HashMap commands = MainBot.slashCommands; super.onSlashCommand(event); if (commands.containsKey(event.getName())) { - commands.get(event.getName()).action(event); + SlashCommand commandRunner = commands.get(event.getName()); + // It's form private message + if (!event.isFromGuild()) { + if (commandRunner.isPrivateUsable()) { + commandRunner.action(event); + } else { + MessageEmbed message = EmbedMessageUtils.buildStandar(new EmbedBuilder() + .setTitle(":no_entry_sign: This command is not usable in private message") + .setColor(Color.red) + ); + event.replyEmbeds(message).setEphemeral(true).queue(); + } + } else { + if (!commandRunner.isNSFW() || event.getTextChannel().isNSFW()) { + commandRunner.action(event); + } else { + MessageEmbed message = EmbedMessageUtils.buildStandar(new EmbedBuilder() + .setTitle(":underage: This command is only usable in NSFW channels") + .setColor(Color.red) + ); + event.replyEmbeds(message).setEphemeral(true).queue(); + } + } } } @@ -206,5 +223,4 @@ public class BotListener extends ListenerAdapter { } - } diff --git a/src/main/java/net/Broken/Commands/ReportUsers.java b/src/main/java/net/Broken/Commands/ReportUsers.java index af70194..cc1298c 100644 --- a/src/main/java/net/Broken/Commands/ReportUsers.java +++ b/src/main/java/net/Broken/Commands/ReportUsers.java @@ -65,4 +65,6 @@ public class ReportUsers implements Commande { public boolean isNSFW() { return false; } + + } diff --git a/src/main/java/net/Broken/SlashCommand.java b/src/main/java/net/Broken/SlashCommand.java index 2984beb..c9a07c1 100644 --- a/src/main/java/net/Broken/SlashCommand.java +++ b/src/main/java/net/Broken/SlashCommand.java @@ -38,5 +38,9 @@ public interface SlashCommand { */ boolean isNSFW(); + boolean isPrivateUsable(); + + + boolean isDisableByDefault(); } diff --git a/src/main/java/net/Broken/SlashCommands/Cat.java b/src/main/java/net/Broken/SlashCommands/Cat.java index 30a9de7..85fb326 100644 --- a/src/main/java/net/Broken/SlashCommands/Cat.java +++ b/src/main/java/net/Broken/SlashCommands/Cat.java @@ -81,4 +81,14 @@ public class Cat implements SlashCommand { public boolean isNSFW() { return false; } + + @Override + public boolean isPrivateUsable() { + return true; + } + + @Override + public boolean isDisableByDefault() { + return false; + } } diff --git a/src/main/java/net/Broken/SlashCommands/Clear.java b/src/main/java/net/Broken/SlashCommands/Clear.java index 59da7f9..c171a0d 100644 --- a/src/main/java/net/Broken/SlashCommands/Clear.java +++ b/src/main/java/net/Broken/SlashCommands/Clear.java @@ -55,4 +55,14 @@ public class Clear implements SlashCommand { public boolean isNSFW() { return false; } + + @Override + public boolean isPrivateUsable() { + return false; + } + + @Override + public boolean isDisableByDefault() { + return true; + } } diff --git a/src/main/java/net/Broken/SlashCommands/Music.java b/src/main/java/net/Broken/SlashCommands/Music.java index 63f4638..30840d3 100644 --- a/src/main/java/net/Broken/SlashCommands/Music.java +++ b/src/main/java/net/Broken/SlashCommands/Music.java @@ -130,4 +130,14 @@ public class Music implements SlashCommand { public boolean isNSFW() { return false; } + + @Override + public boolean isPrivateUsable() { + return false; + } + + @Override + public boolean isDisableByDefault() { + return false; + } } diff --git a/src/main/java/net/Broken/Commands/Over18/Madame.java b/src/main/java/net/Broken/SlashCommands/Over18/Madame.java similarity index 80% rename from src/main/java/net/Broken/Commands/Over18/Madame.java rename to src/main/java/net/Broken/SlashCommands/Over18/Madame.java index 9701c17..08df614 100644 --- a/src/main/java/net/Broken/Commands/Over18/Madame.java +++ b/src/main/java/net/Broken/SlashCommands/Over18/Madame.java @@ -1,55 +1,18 @@ -package net.Broken.Commands.Over18; +package net.Broken.SlashCommands.Over18; -import net.Broken.Tools.Command.NoDev; -import net.Broken.Tools.Command.NumberedCommande; +import net.Broken.Tools.Command.NumberedSlashCommand; import net.Broken.Tools.FindContentOnWebPage; -import net.dv8tion.jda.api.events.message.MessageReceivedEvent; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import java.io.IOException; - -/** - * Madame command that return random picture from dites.bonjourmadame.fr - */ -@NoDev -public class Madame extends NumberedCommande { - public String HELP = "Yo really? Just type Madame to see some :cat:"; +public class Madame extends NumberedSlashCommand { Logger logger = LogManager.getLogger(); - MessageReceivedEvent event; public Madame() { super(LogManager.getLogger(), "https://www.bonjourmadame.fr/page/", "/"); } - - - @Override - public boolean isPrivateUsable() { - return false; - } - - @Override - public boolean isAdminCmd() { - return false; - } - - /** - * Determines if the command is usable only by bot level admin user - * - * @return boolean - */ - @Override - public boolean isBotAdminCmd() { - return false; - } - - @Override - public boolean isNSFW() { - return true; - } - - /** * Detect if picture link go to Tepeee * @@ -107,4 +70,29 @@ public class Madame extends NumberedCommande { imgUrl = removeParams(imgUrl); return imgUrl; } + + @Override + public String getDescription() { + return "Return random image from bonjourmadame.fr"; + } + + @Override + public boolean isBotAdminCmd() { + return false; + } + + @Override + public boolean isNSFW() { + return true; + } + + @Override + public boolean isPrivateUsable() { + return false; + } + + @Override + public boolean isDisableByDefault() { + return true; + } } diff --git a/src/main/java/net/Broken/SlashCommands/Rank.java b/src/main/java/net/Broken/SlashCommands/Rank.java index ff9b343..46e71ff 100644 --- a/src/main/java/net/Broken/SlashCommands/Rank.java +++ b/src/main/java/net/Broken/SlashCommands/Rank.java @@ -46,4 +46,14 @@ public class Rank implements SlashCommand { public boolean isNSFW() { return false; } + + @Override + public boolean isPrivateUsable() { + return false; + } + + @Override + public boolean isDisableByDefault() { + return false; + } } diff --git a/src/main/java/net/Broken/Tools/Command/NumberedCommande.java b/src/main/java/net/Broken/Tools/Command/NumberedCommande.java deleted file mode 100644 index 3466a1f..0000000 --- a/src/main/java/net/Broken/Tools/Command/NumberedCommande.java +++ /dev/null @@ -1,152 +0,0 @@ -package net.Broken.Tools.Command; - -import net.Broken.Commande; -import net.Broken.Tools.FindContentOnWebPage; -import net.Broken.Tools.LimitChecker; -import net.Broken.Tools.TrueRandom; -import net.dv8tion.jda.api.events.message.MessageReceivedEvent; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; - -import java.io.IOException; -import java.net.HttpURLConnection; -import java.net.URL; -import java.util.ArrayList; -import java.util.concurrent.LinkedBlockingQueue; - -/** - * Abstact class used for all command that need to find the max number of page on a web site. - */ -@Ignore -public abstract class NumberedCommande implements Commande { - protected int minNumber = 1; - protected int maxNumber = -1; - protected String baseURL; - protected String divClass; - protected String htmlType; - protected String urlSuffix; - protected LinkedBlockingQueue randomQueue = new LinkedBlockingQueue<>(); - private Logger logger = LogManager.getLogger(); - - /** - * Default constructor - * - * @param logger Logger used for logs - * @param baseURL WebSite base url - * @param divClass DivClass to search to extract image - * @param htmlType HTML tag to extract image (img) - */ - public NumberedCommande(Logger logger, String baseURL, String urlSuffix, String divClass, String htmlType) { - this.logger = logger; - this.baseURL = baseURL; - this.divClass = divClass; - this.htmlType = htmlType; - this.urlSuffix = urlSuffix; - try { - logger.debug("Checking max..."); - maxNumber = LimitChecker.doYourJob(baseURL, 2, urlSuffix); - logger.info("Limit is " + maxNumber); - } catch (IOException e) { - logger.catching(e); - } - } - - public NumberedCommande(Logger logger, String baseURL, String urlSuffix) { - this(logger, baseURL, urlSuffix, null, null); - - } - - @Override - public void action(String[] args, MessageReceivedEvent event) { - try { - if (args.length == 0) { - String result = poll(); - event.getTextChannel().sendMessage(event.getAuthor().getAsMention() + "\n" + result).queue(); - - } else { - switch (args[0].toLowerCase()) { - case "update": - logger.info("update commande from " + event.getMessage().getAuthor().getName()); - event.getTextChannel().sendMessage(event.getAuthor().getAsMention() + "\n:arrow_right: Updating...").queue(); - - int newNumber = maxNumber; - try { - newNumber = LimitChecker.doYourJob(baseURL, maxNumber, urlSuffix); - } catch (IOException e) { - e.printStackTrace(); - } - if (newNumber == maxNumber) { - event.getTextChannel().sendMessage(event.getAuthor().getAsMention() + "\n:arrow_right: 0 new picture found :cry:").queue(); - } else if (newNumber - maxNumber == 1) - event.getTextChannel().sendMessage(event.getAuthor().getAsMention() + "\n:arrow_right: " + (newNumber - maxNumber) + " picture has been found :kissing_heart:").queue(); - else - event.getTextChannel().sendMessage(event.getAuthor().getAsMention() + "\n:arrow_right: " + (newNumber - maxNumber) + " pictures has been found :kissing_heart:").queue(); - logger.info((newNumber - maxNumber) + " new image(s) found."); - maxNumber = newNumber; - break; - case "get": - if (args.length >= 2) { - - int number = -1; - try { - number = Integer.parseInt(args[1]); - URL url = new URL(baseURL + number + "-2/"); - HttpURLConnection huc = (HttpURLConnection) url.openConnection(); - huc.setRequestMethod("GET"); - huc.connect(); - int result = huc.getResponseCode(); - if (result == 200) { - event.getTextChannel().sendMessage(event.getAuthor().getAsMention() + "\n" + baseURL + number + urlSuffix).queue(); - } else { - event.getTextChannel().sendMessage(event.getAuthor().getAsMention() + "\n:warning: **__Error__** :warning:\n:arrow_right: Page not found (404)").queue(); - } - - } catch (NumberFormatException e) { - event.getTextChannel().sendMessage(event.getAuthor().getAsMention() + "\n:warning: **__Error__** :warning:\n:arrow_right: Bad argument. `//help " + this.toString().toLowerCase() + "` for more info ").queue(); - } catch (IOException e) { - logger.catching(e); - event.getTextChannel().sendMessage(event.getAuthor().getAsMention() + "\n:warning: **__Error__** :warning:\n:arrow_right: Internal error...").queue(); - } - } else { - event.getTextChannel().sendMessage(event.getAuthor().getAsMention() + "\n:warning: **__Error__** :warning:\n:arrow_right: Bad argument. `//help " + this.toString().toLowerCase() + "` for more info ").queue(); - logger.warn("Bad Argument: " + event.getMessage().getContentRaw() + " From " + event.getAuthor().getName()); - } - break; - default: - event.getTextChannel().sendMessage(event.getAuthor().getAsMention() + "\n:warning: **__Error__** :warning:\n:arrow_right: Bad argument. `//help " + this.toString().toLowerCase() + "` for more info ").queue(); - logger.warn("Bad Argument: " + event.getMessage().getContentRaw() + " From " + event.getAuthor().getName()); - break; - } - } - } catch (IOException e) { - logger.catching(e); - } - - - } - - - private void completeRandom() throws IOException { - TrueRandom trueRandom = TrueRandom.getINSTANCE(); - ArrayList numbers = trueRandom.getNumbers(minNumber, maxNumber); - - randomQueue.addAll(numbers); - - } - - protected void checkRandom() throws IOException { - logger.trace("Queue size: " + randomQueue.size()); - if (randomQueue.isEmpty()) { - logger.debug("Queue empty, update it."); - completeRandom(); - } - } - - public String poll() throws IOException { - checkRandom(); - int randomResult = randomQueue.poll(); - return FindContentOnWebPage.doYourJob(baseURL + randomResult + urlSuffix, divClass, htmlType); - } - - -} diff --git a/src/main/java/net/Broken/Tools/Command/NumberedSlashCommand.java b/src/main/java/net/Broken/Tools/Command/NumberedSlashCommand.java new file mode 100644 index 0000000..7e80e21 --- /dev/null +++ b/src/main/java/net/Broken/Tools/Command/NumberedSlashCommand.java @@ -0,0 +1,112 @@ +package net.Broken.Tools.Command; + +import net.Broken.Commande; +import net.Broken.SlashCommand; +import net.Broken.Tools.EmbedMessageUtils; +import net.Broken.Tools.FindContentOnWebPage; +import net.Broken.Tools.LimitChecker; +import net.Broken.Tools.TrueRandom; +import net.dv8tion.jda.api.entities.MessageEmbed; +import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; +import net.dv8tion.jda.api.events.message.MessageReceivedEvent; +import net.dv8tion.jda.api.interactions.commands.build.OptionData; +import net.dv8tion.jda.api.interactions.commands.build.SubcommandData; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import java.io.IOException; +import java.net.HttpURLConnection; +import java.net.URL; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.LinkedBlockingQueue; + +/** + * Abstact class used for all command that need to find the max number of page on a web site. + */ +@Ignore +public abstract class NumberedSlashCommand implements SlashCommand { + protected int minNumber = 1; + protected int maxNumber = -1; + protected String baseURL; + protected String divClass; + protected String htmlType; + protected String urlSuffix; + protected LinkedBlockingQueue randomQueue = new LinkedBlockingQueue<>(); + private Logger logger = LogManager.getLogger(); + + /** + * Default constructor + * + * @param logger Logger used for logs + * @param baseURL WebSite base url + * @param divClass DivClass to search to extract image + * @param htmlType HTML tag to extract image (img) + */ + public NumberedSlashCommand(Logger logger, String baseURL, String urlSuffix, String divClass, String htmlType) { + this.logger = logger; + this.baseURL = baseURL; + this.divClass = divClass; + this.htmlType = htmlType; + this.urlSuffix = urlSuffix; + try { + logger.debug("Checking max..."); + maxNumber = LimitChecker.doYourJob(baseURL, 2, urlSuffix); + logger.info("Limit is " + maxNumber); + } catch (IOException e) { + logger.catching(e); + } + } + + public NumberedSlashCommand(Logger logger, String baseURL, String urlSuffix) { + this(logger, baseURL, urlSuffix, null, null); + + } + + @Override + public void action(SlashCommandEvent event) { + event.deferReply().queue(); + try { + String result = poll(); + event.getHook().sendMessage(event.getMember().getAsMention() + "\n" + result).queue(); + } catch (IOException e) { + logger.catching(e); + MessageEmbed message = EmbedMessageUtils.getInternalError(); + event.getHook().setEphemeral(true).sendMessageEmbeds(message).queue(); + } + } + + + private void fillRandomQueue() throws IOException { + TrueRandom trueRandom = TrueRandom.getINSTANCE(); + ArrayList numbers = trueRandom.getNumbers(minNumber, maxNumber); + + randomQueue.addAll(numbers); + + } + + protected void checkRandom() throws IOException { + logger.trace("Queue size: " + randomQueue.size()); + if (randomQueue.isEmpty()) { + logger.debug("Queue empty, update it."); + fillRandomQueue(); + } + } + + public String poll() throws IOException { + checkRandom(); + int randomResult = randomQueue.poll(); + return FindContentOnWebPage.doYourJob(baseURL + randomResult + urlSuffix, divClass, htmlType); + } + + @Override + public List getOptions() { + return null; + } + + @Override + public List getSubcommands() { + return null; + } + +} diff --git a/src/main/java/net/Broken/Tools/Command/SlashCommandLoader.java b/src/main/java/net/Broken/Tools/Command/SlashCommandLoader.java index 0a0a061..2af7628 100644 --- a/src/main/java/net/Broken/Tools/Command/SlashCommandLoader.java +++ b/src/main/java/net/Broken/Tools/Command/SlashCommandLoader.java @@ -65,6 +65,7 @@ public class SlashCommandLoader { if (v.getSubcommands() != null) { command.addSubcommands(v.getSubcommands()); } + command.setDefaultEnabled(!v.isDisableByDefault()); commandListUpdateAction.addCommands(command); }); commandListUpdateAction.queue(); diff --git a/src/main/java/net/Broken/Tools/DayListener/Listeners/DailyMadame.java b/src/main/java/net/Broken/Tools/DayListener/Listeners/DailyMadame.java index eccddf9..3ec80bd 100644 --- a/src/main/java/net/Broken/Tools/DayListener/Listeners/DailyMadame.java +++ b/src/main/java/net/Broken/Tools/DayListener/Listeners/DailyMadame.java @@ -1,9 +1,9 @@ package net.Broken.Tools.DayListener.Listeners; -import net.Broken.Commands.Over18.Madame; import net.Broken.DB.Entity.GuildPreferenceEntity; import net.Broken.DB.Repository.GuildPreferenceRepository; import net.Broken.MainBot; +import net.Broken.SlashCommands.Over18.Madame; import net.Broken.SpringContext; import net.Broken.Tools.DayListener.NewDayListener; import net.Broken.Tools.FindContentOnWebPage; @@ -50,7 +50,7 @@ public class DailyMadame implements NewDayListener { } else { - Madame command = (Madame) MainBot.commandes.get("madame"); + Madame command = (Madame) MainBot.slashCommands.get("madame"); imgUrl = command.poll(); }