From cf5833f57b15d0a30e393c3733c9f14a60480050 Mon Sep 17 00:00:00 2001 From: limited_dev Date: Mon, 10 Oct 2022 12:42:31 +0200 Subject: [PATCH] fix: Messages are now send on time into the proper time Management channels feat: started checking for double users & bots in searchscrim command, made it impossible for DestinyCast to add the Bot to the Server chore: added TODO.md --- TODO.md | 3 ++ .../java/de/limited_dev/lil_judd/Main.java | 2 + .../lil_judd/commands/SearchScrimCommand.java | 42 +++++++++++++++++-- .../commands/SetupFeatureCommand.java | 2 +- .../lil_judd/features/ScrimMaker.java | 6 +-- .../lil_judd/features/TimePlanner.java | 27 +++++++----- .../components/scrims/GuildInQueue.java | 24 ++++++----- .../listeners/GuildStateListener.java | 5 ++- .../SlashCommandInteractionListener.java | 5 +-- .../lil_judd/util/SlashCommandHelper.java | 10 ++--- 10 files changed, 88 insertions(+), 38 deletions(-) create mode 100644 TODO.md diff --git a/TODO.md b/TODO.md new file mode 100644 index 0000000..dd37b52 --- /dev/null +++ b/TODO.md @@ -0,0 +1,3 @@ +# TODO +- add Map Translator +- make Scrim searcher toggleable \ No newline at end of file diff --git a/src/main/java/de/limited_dev/lil_judd/Main.java b/src/main/java/de/limited_dev/lil_judd/Main.java index 7fc41bf..20264f1 100644 --- a/src/main/java/de/limited_dev/lil_judd/Main.java +++ b/src/main/java/de/limited_dev/lil_judd/Main.java @@ -16,6 +16,7 @@ import net.dv8tion.jda.api.OnlineStatus; import net.dv8tion.jda.api.entities.Activity; import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.interactions.commands.build.Commands; +import net.dv8tion.jda.api.requests.GatewayIntent; import java.io.IOException; @@ -36,6 +37,7 @@ public class Main { jda = JDABuilder.createDefault(tokenManager.getToken()) .setActivity(Activity.watching("You")) .setStatus(OnlineStatus.DO_NOT_DISTURB) + .enableIntents(GatewayIntent.GUILD_MEMBERS) .build(); CommandManager.registerCommands(); diff --git a/src/main/java/de/limited_dev/lil_judd/commands/SearchScrimCommand.java b/src/main/java/de/limited_dev/lil_judd/commands/SearchScrimCommand.java index 078a5e7..5481c4f 100644 --- a/src/main/java/de/limited_dev/lil_judd/commands/SearchScrimCommand.java +++ b/src/main/java/de/limited_dev/lil_judd/commands/SearchScrimCommand.java @@ -1,5 +1,6 @@ package de.limited_dev.lil_judd.commands; +import de.limited_dev.lil_judd.Main; import de.limited_dev.lil_judd.commands.components.Command; import de.limited_dev.lil_judd.features.ScrimMaker; import de.limited_dev.lil_judd.util.EmbeddedMessageHelper; @@ -8,6 +9,9 @@ import net.dv8tion.jda.api.entities.MessageEmbed; import net.dv8tion.jda.api.entities.User; import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; +import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; + public class SearchScrimCommand extends Command { public SearchScrimCommand() { super("searchscrim", "Search for a scrim", null); @@ -19,11 +23,27 @@ public class SearchScrimCommand extends Command { User u2 = event.getOption("player2").getAsUser(); User u3 = event.getOption("player3").getAsUser(); User u4 = event.getOption("player4").getAsUser(); + List users = new CopyOnWriteArrayList<>(){{ + add(u1); + add(u2); + add(u3); + add(u4); + }}; Guild g = event.getGuild(); int div = event.getOption("div").getAsInt(); - ScrimMaker.EnterQueue(g, event.getChannel().asTextChannel(), div, u1, u2, u3, u4); - //MessageEmbed eb = EmbeddedMessageHelper.getEmbeddedMessageAutomated(false, "Adding...", "We are adding you to the queue", null, false ).build(); - //event.replyEmbeds(eb).queue(); + if(ScrimMaker.IsInGameOrQueue(g)){ + EmbeddedMessageHelper.sendSimpleOneLiner(event, "The Guild is already in a match", "You cannot join another.", null); + return; + } + if(ContainsAUserTwice(users)){ + EmbeddedMessageHelper.sendSimpleOneLiner(event, "You added a user twice.", "Please only add every user once.", null); + return; + } + if(ContainsABot(users)){ + EmbeddedMessageHelper.sendSimpleOneLiner(event, "You added a bot", "You cannot add a bot as user.", null); + return; + } + ScrimMaker.EnterQueue(g, event.getChannel().asTextChannel(), div, users); EmbeddedMessageHelper.sendSimpleOneLiner(event, " Queuing... ", g.getName() + " has been added to the Scrim Queue for Division " + div + "\n\n" + "Player 1 >> " + u1.getAsMention() + "\n" + @@ -32,4 +52,20 @@ public class SearchScrimCommand extends Command { "Player 4 >> " + u4.getAsMention(), null); } + + private boolean ContainsAUserTwice(List users){ + int counter = 0; + for (User u : users) + for (User us : users) + if(u.getIdLong() == us.getIdLong()) + ++counter; + return counter != 4; + } + + private boolean ContainsABot(List users){ + for (User u : users) + if(u.isBot() || u.isSystem()) + return true; + return false; + } } diff --git a/src/main/java/de/limited_dev/lil_judd/commands/SetupFeatureCommand.java b/src/main/java/de/limited_dev/lil_judd/commands/SetupFeatureCommand.java index da87e67..dd95b2e 100644 --- a/src/main/java/de/limited_dev/lil_judd/commands/SetupFeatureCommand.java +++ b/src/main/java/de/limited_dev/lil_judd/commands/SetupFeatureCommand.java @@ -14,7 +14,7 @@ public class SetupFeatureCommand extends Command { @Override public void onSlashCommandInteraction(SlashCommandInteractionEvent event) { - if(!event.getGuild().getMemberById(event.getUser().getId()).hasPermission(Permission.ADMINISTRATOR)){ + if(!event.getGuild().getMemberById(event.getUser().getIdLong()).hasPermission(Permission.ADMINISTRATOR)){ EmbeddedMessageHelper.sendSimpleOneLiner(event, "You do not have the Permission", "Sorry, but you don't have the Permission to run this command", null); return; } diff --git a/src/main/java/de/limited_dev/lil_judd/features/ScrimMaker.java b/src/main/java/de/limited_dev/lil_judd/features/ScrimMaker.java index cb01401..0119106 100644 --- a/src/main/java/de/limited_dev/lil_judd/features/ScrimMaker.java +++ b/src/main/java/de/limited_dev/lil_judd/features/ScrimMaker.java @@ -13,7 +13,7 @@ public class ScrimMaker { private static List queueList = new CopyOnWriteArrayList<>(); private static List gameList = new CopyOnWriteArrayList<>(); - public static void EnterQueue(Guild g, TextChannel channel, int div, User u1, User u2, User u3, User u4){ + public static void EnterQueue(Guild g, TextChannel channel, int div, List users){ if(IsInGameOrQueue(g)){ EmbeddedMessageHelper.sendSimpleOneLiner(g, channel.getIdLong(), "Error", "You are already in a scrim or are already searching. Please stop searching first.", null); return; @@ -23,12 +23,12 @@ public class ScrimMaker { if(gq.getDiv() == div || b){ b = true; - EnterGame(gq, new GuildInQueue(g, channel, div, u1, u2, u3, u4)); + EnterGame(gq, new GuildInQueue(g, channel, div, users)); } } if(b) return; - queueList.add(new GuildInQueue(g, channel, div, u1, u2, u3, u4)); + queueList.add(new GuildInQueue(g, channel, div, users)); } public static void LeaveQueue(Guild g){ diff --git a/src/main/java/de/limited_dev/lil_judd/features/TimePlanner.java b/src/main/java/de/limited_dev/lil_judd/features/TimePlanner.java index 0f596ff..dff4e1b 100644 --- a/src/main/java/de/limited_dev/lil_judd/features/TimePlanner.java +++ b/src/main/java/de/limited_dev/lil_judd/features/TimePlanner.java @@ -26,6 +26,9 @@ import java.util.concurrent.TimeUnit; public class TimePlanner { private static final GuildTimePlannerStorage gtps = GuildTimePlannerStorage.getInstance(); private static final Logger lgr = Main.getLgr(); + private static final int hour = 8; + private static final int minute = 0; + private static final int second = 0; public static GuildTimePlannerStorage getGtps() { return gtps; @@ -36,7 +39,7 @@ public class TimePlanner { ZonedDateTime now = ZonedDateTime.now(ZoneId.of("Europe/Berlin")); - ZonedDateTime nextNotifyDay = now.plusDays(DayUtil.getDelay(now.getDayOfWeek().name())).withHour(8).withMinute(0).withSecond(0); + ZonedDateTime nextNotifyDay = now.plusDays(DayUtil.getDelay(now.getDayOfWeek().name())).withHour(hour).withMinute(minute).withSecond(second); //ZonedDateTime nextNotifyDay = now.plusSeconds(10); lgr.info(nextNotifyDay.getDayOfWeek() + ", " + nextNotifyDay.getDayOfMonth() + "." + nextNotifyDay.getMonth() + "." + nextNotifyDay.getYear() + " / " + nextNotifyDay.getHour() + ":" + nextNotifyDay.getMinute() + ":" + nextNotifyDay.getSecond()); @@ -44,8 +47,8 @@ public class TimePlanner { // if it's already past the time (in this case 8:05) the first lesson will be scheduled for the next day if (now.compareTo(nextNotifyDay) > 0) { - nextNotifyDay = nextNotifyDay.plusDays(7).withHour(8).withMinute(0); - lgr.info("See you next week"); + nextNotifyDay = nextNotifyDay.plusDays(7).withHour(hour).withMinute(minute).withSecond(second); + lgr.info("Its already after 8am. Setting time to next Week"); } // duration between now and the beginning of the next first lesson @@ -65,20 +68,22 @@ public class TimePlanner { Thread thr = new Thread(){ @Override public void run() { - ZonedDateTime then = ZonedDateTime.now(ZoneId.of("Europe/Berlin")); for(Long l : gs) { + ZonedDateTime then = ZonedDateTime.now(ZoneId.of("Europe/Berlin")).withHour(hour).withMinute(minute).withSecond(second); EmbeddedMessageHelper.sendSimpleOneLiner(jda.getGuildById(l), gtps.guildsWithPlanner.get(l), "Timeplanning System", "Do you have time on the following Days?", null); try { - Thread.sleep(2000); + Thread.sleep(4000); } catch (InterruptedException e) { throw new RuntimeException(e); } for(int i = 0; i < 7; ++i){ - then = then.plusDays(i); + if(i != 0) + then = then.plusDays(1).withHour(hour).withMinute(minute).withSecond(second); MessageEmbed eb = EmbeddedMessageHelper.getEmbeddedMessageAutomated(false, null, then.getDayOfWeek() + ", " + then.getDayOfMonth() + "." + then.getMonthValue() + "." + then.getYear(), null, false).build(); jda.getGuildById(l).getTextChannelById(gtps.guildsWithPlanner.get(l)).sendMessageEmbeds(eb).queue(message -> addReactions(message)); + lgr.info(gs + ": " + then.getDayOfWeek() + ", " + then.getDayOfMonth() + "." + then.getMonthValue() + "." + then.getYear()); try { - Thread.sleep(2000); + Thread.sleep(5000); } catch (InterruptedException e) { throw new RuntimeException(e); } @@ -93,25 +98,25 @@ public class TimePlanner { private void addReactions(Message msg){ try { - Thread.sleep(500); + Thread.sleep(1000); } catch (InterruptedException e) { throw new RuntimeException(e); } msg.addReaction(Emoji.fromUnicode("✅")).queue(); try { - Thread.sleep(500); + Thread.sleep(1000); } catch (InterruptedException e) { throw new RuntimeException(e); } msg.addReaction(Emoji.fromFormatted("❌")).queue(); try { - Thread.sleep(500); + Thread.sleep(1000); } catch (InterruptedException e) { throw new RuntimeException(e); } msg.addReaction(Emoji.fromFormatted("❓")).queue(); try { - Thread.sleep(500); + Thread.sleep(1000); } catch (InterruptedException e) { throw new RuntimeException(e); } diff --git a/src/main/java/de/limited_dev/lil_judd/features/components/scrims/GuildInQueue.java b/src/main/java/de/limited_dev/lil_judd/features/components/scrims/GuildInQueue.java index 2167c0b..24bf240 100644 --- a/src/main/java/de/limited_dev/lil_judd/features/components/scrims/GuildInQueue.java +++ b/src/main/java/de/limited_dev/lil_judd/features/components/scrims/GuildInQueue.java @@ -18,28 +18,32 @@ public class GuildInQueue { private final long startedSearching; private long startedScrim; - public GuildInQueue(Guild g, TextChannel responseChannel, int div, User u1, User u2, User u3, User u4){ + public GuildInQueue(Guild g, TextChannel responseChannel, int div, List users){ this.g = g; this.responseChannel = responseChannel; this.div = div; - this.users = new CopyOnWriteArrayList<>(){{ - add(u1); - add(u2); - add(u3); - add(u4); - }}; + this.users = users; this.startedSearching = System.currentTimeMillis(); } public void startScrimWith(GuildInQueue g2){ this.enemyGuildInQueue = g2; this.startedScrim = System.currentTimeMillis(); - EmbeddedMessageHelper.sendSimpleOneLiner(this.g, this.responseChannel.getIdLong(), "Team found.", "A Scrim has been found!\nTime spent queuing: " + TimeUtil.getTimeFormatedRaw(this.startedScrim - this.startedSearching) + "\nYou will play against: " + this.enemyGuildInQueue.getGuild().getName() + "\n" + - "Please communicate with the other team with /msgscrim ; end the scrim with /endscrim", this.enemyGuildInQueue.getGuild().getIconUrl(), 2); + EmbeddedMessageHelper.sendSimpleOneLiner(this.g, this.responseChannel.getIdLong(), + "Match found.", "***A Scrim has been found!***\n" + + "Time spent queuing: " + TimeUtil.getTimeFormatedRaw(this.startedScrim - this.startedSearching) + "\n\n" + + "**Vs: " + this.enemyGuildInQueue.getGuild().getName() + "**\n" + + "Player 1 >> " + this.enemyGuildInQueue.getUsers().get(0).getAsMention() + "\n" + + "Player 2 >> " + this.enemyGuildInQueue.getUsers().get(1).getAsMention() + "\n" + + "Player 3 >> " + this.enemyGuildInQueue.getUsers().get(2).getAsMention() + "\n" + + "Player 4 >> " + this.enemyGuildInQueue.getUsers().get(3).getAsMention() + + "\n\n" + + "/msgscrim to message the other Team\n" + + "/endscrim to end the current scrim", this.enemyGuildInQueue.getGuild().getIconUrl(), 2); } public void endScrim(){ - EmbeddedMessageHelper.sendSimpleOneLiner(this.g, this.responseChannel.getIdLong(), "The Scrim has ended.", "The Scrim with " + this.enemyGuildInQueue.getGuild().getName() + " was ended.", this.enemyGuildInQueue.getGuild().getIconUrl()); + EmbeddedMessageHelper.sendSimpleOneLiner(this.g, this.responseChannel.getIdLong(), "The Scrim has ended.", "The Scrim with " + this.enemyGuildInQueue.getGuild().getName() + " ended.", this.enemyGuildInQueue.getGuild().getIconUrl()); } diff --git a/src/main/java/de/limited_dev/lil_judd/listeners/GuildStateListener.java b/src/main/java/de/limited_dev/lil_judd/listeners/GuildStateListener.java index 2b690e3..2285b06 100644 --- a/src/main/java/de/limited_dev/lil_judd/listeners/GuildStateListener.java +++ b/src/main/java/de/limited_dev/lil_judd/listeners/GuildStateListener.java @@ -15,8 +15,9 @@ public class GuildStateListener extends ListenerAdapter { @Override public void onGuildJoin(@NotNull GuildJoinEvent event) { lgr.info("I have been added to a guild."); - //Guild g = event.getGuild(); - //SlashCommandHelper.addSlashCommandsToGuild(g); + if(event.getGuild().getName().contains("Cast") || event.getGuild().getName().contains("cst")){ + event.getGuild().leave().queue(); + } } @Override diff --git a/src/main/java/de/limited_dev/lil_judd/listeners/SlashCommandInteractionListener.java b/src/main/java/de/limited_dev/lil_judd/listeners/SlashCommandInteractionListener.java index 1c764ae..9876103 100644 --- a/src/main/java/de/limited_dev/lil_judd/listeners/SlashCommandInteractionListener.java +++ b/src/main/java/de/limited_dev/lil_judd/listeners/SlashCommandInteractionListener.java @@ -12,10 +12,9 @@ public class SlashCommandInteractionListener extends ListenerAdapter { @Override public void onSlashCommandInteraction(@NotNull SlashCommandInteractionEvent event) { - for(Command c : CommandManager.getCommands()){ - Main.getLgr().info("New Command: /" + event.getName()); + Main.getLgr().info("New Command: /" + event.getName() + " in " + event.getGuild().getName()); + for(Command c : CommandManager.getCommands()) if(event.getName().equals(c.getName())) c.onSlashCommandInteraction(event); - } } } diff --git a/src/main/java/de/limited_dev/lil_judd/util/SlashCommandHelper.java b/src/main/java/de/limited_dev/lil_judd/util/SlashCommandHelper.java index fcb7df5..eff9144 100644 --- a/src/main/java/de/limited_dev/lil_judd/util/SlashCommandHelper.java +++ b/src/main/java/de/limited_dev/lil_judd/util/SlashCommandHelper.java @@ -1,10 +1,6 @@ package de.limited_dev.lil_judd.util; import de.limited_dev.lil_judd.Main; -import de.limited_dev.lil_judd.commands.components.Command; -import de.limited_dev.lil_judd.commands.components.CommandManager; -import de.limited_dev.lil_judd.commands.components.Option; -import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.interactions.commands.OptionType; import net.dv8tion.jda.api.interactions.commands.build.Commands; import net.dv8tion.jda.api.interactions.commands.build.OptionData; @@ -41,7 +37,11 @@ public class SlashCommandHelper { .addOptions( new OptionData(OptionType.STRING, "message", "Message content", true) ), - Commands.slash("scriminfo", "Show info about the scrim system.") + Commands.slash("scriminfo", "Show info about the scrim system."), + Commands.slash("translatemap", "Translate a Map from German to English") + .addOptions( + new OptionData(OptionType.STRING, "map", "Message content", true) + ) ).queue();