From 4c892abf881b089bff2d56411aaaab941cd14a37 Mon Sep 17 00:00:00 2001 From: SebClem Date: Tue, 27 Sep 2022 11:32:33 +0200 Subject: [PATCH] :hammer: Add daily member cache --- .../Api/Controllers/GuildController.java | 19 +++++++----- .../Broken/Audio/GuildAudioBotService.java | 30 ++++++++++++------- .../java/net/Broken/Audio/TrackScheduler.java | 16 +++++----- .../java/net/Broken/SlashCommands/Cat.java | 27 ++++++++--------- .../Tools/Command/NumberedSlashCommand.java | 14 ++++----- .../DayListener/Listeners/DailyMadame.java | 2 +- .../Listeners/DailyMemberCache.java | 26 ++++++++++++++++ .../Broken/Tools/FindContentOnWebPage.java | 5 ---- .../java/net/Broken/Tools/LimitChecker.java | 6 ++-- 9 files changed, 88 insertions(+), 57 deletions(-) create mode 100644 src/main/java/net/Broken/Tools/DayListener/Listeners/DailyMemberCache.java diff --git a/src/main/java/net/Broken/Api/Controllers/GuildController.java b/src/main/java/net/Broken/Api/Controllers/GuildController.java index a0bc11e..cf84cc1 100644 --- a/src/main/java/net/Broken/Api/Controllers/GuildController.java +++ b/src/main/java/net/Broken/Api/Controllers/GuildController.java @@ -1,18 +1,21 @@ package net.Broken.Api.Controllers; +import java.util.List; + +import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.security.core.Authentication; +import org.springframework.web.bind.annotation.CrossOrigin; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import net.Broken.Api.Data.InviteLink; import net.Broken.Api.Data.Guild.Channel; import net.Broken.Api.Data.Guild.Guild; import net.Broken.Api.Data.Guild.Role; -import net.Broken.Api.Data.InviteLink; import net.Broken.Api.Security.Data.JwtPrincipal; import net.Broken.Api.Services.GuildService; -import net.Broken.MainBot; -import net.dv8tion.jda.api.Permission; -import org.springframework.security.access.prepost.PreAuthorize; -import org.springframework.security.core.Authentication; -import org.springframework.web.bind.annotation.*; - -import java.util.List; @RestController @RequestMapping("/api/v2/guild") diff --git a/src/main/java/net/Broken/Audio/GuildAudioBotService.java b/src/main/java/net/Broken/Audio/GuildAudioBotService.java index 2c4bde2..b9cbbd4 100644 --- a/src/main/java/net/Broken/Audio/GuildAudioBotService.java +++ b/src/main/java/net/Broken/Audio/GuildAudioBotService.java @@ -1,5 +1,18 @@ package net.Broken.Audio; +import java.awt.Color; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.UUID; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Future; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + import com.sedmelluq.discord.lavaplayer.player.AudioLoadResultHandler; import com.sedmelluq.discord.lavaplayer.player.AudioPlayerManager; import com.sedmelluq.discord.lavaplayer.player.DefaultAudioPlayerManager; @@ -8,23 +21,20 @@ import com.sedmelluq.discord.lavaplayer.tools.FriendlyException; import com.sedmelluq.discord.lavaplayer.track.AudioPlaylist; import com.sedmelluq.discord.lavaplayer.track.AudioTrack; import com.sedmelluq.discord.lavaplayer.track.AudioTrackInfo; -import net.Broken.MainBot; + import net.Broken.Tools.EmbedMessageUtils; import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.MessageBuilder; -import net.dv8tion.jda.api.entities.*; +import net.dv8tion.jda.api.entities.Emoji; +import net.dv8tion.jda.api.entities.Guild; +import net.dv8tion.jda.api.entities.Member; +import net.dv8tion.jda.api.entities.Message; +import net.dv8tion.jda.api.entities.User; +import net.dv8tion.jda.api.entities.VoiceChannel; import net.dv8tion.jda.api.events.interaction.GenericInteractionCreateEvent; import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; import net.dv8tion.jda.api.interactions.components.ActionRow; import net.dv8tion.jda.api.interactions.components.Button; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; - -import java.awt.*; -import java.util.*; -import java.util.List; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.Future; public class GuildAudioBotService { diff --git a/src/main/java/net/Broken/Audio/TrackScheduler.java b/src/main/java/net/Broken/Audio/TrackScheduler.java index 7ee4433..4a3cddd 100644 --- a/src/main/java/net/Broken/Audio/TrackScheduler.java +++ b/src/main/java/net/Broken/Audio/TrackScheduler.java @@ -1,20 +1,22 @@ package net.Broken.Audio; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.BlockingDeque; +import java.util.concurrent.LinkedBlockingDeque; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + import com.sedmelluq.discord.lavaplayer.player.AudioPlayer; import com.sedmelluq.discord.lavaplayer.player.event.AudioEventAdapter; import com.sedmelluq.discord.lavaplayer.tools.FriendlyException; import com.sedmelluq.discord.lavaplayer.track.AudioTrack; import com.sedmelluq.discord.lavaplayer.track.AudioTrackEndReason; import com.sedmelluq.discord.lavaplayer.track.AudioTrackInfo; + import net.Broken.MainBot; import net.dv8tion.jda.api.entities.Guild; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; - -import java.util.ArrayList; -import java.util.List; -import java.util.concurrent.BlockingDeque; -import java.util.concurrent.LinkedBlockingDeque; /** * This class schedules tracks for the audio player. It contains the queue of tracks. diff --git a/src/main/java/net/Broken/SlashCommands/Cat.java b/src/main/java/net/Broken/SlashCommands/Cat.java index 315e273..eae1581 100644 --- a/src/main/java/net/Broken/SlashCommands/Cat.java +++ b/src/main/java/net/Broken/SlashCommands/Cat.java @@ -1,29 +1,26 @@ package net.Broken.SlashCommands; +import java.io.IOException; +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.util.HashMap; +import java.util.List; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; + import net.Broken.SlashCommand; import net.Broken.Tools.EmbedMessageUtils; import net.dv8tion.jda.api.MessageBuilder; import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; 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.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.net.URI; -import java.net.URL; -import java.net.URLConnection; -import java.net.http.HttpClient; -import java.net.http.HttpRequest; -import java.net.http.HttpResponse; -import java.nio.charset.StandardCharsets; -import java.util.HashMap; -import java.util.List; /** * Command that return a random picture of cat. diff --git a/src/main/java/net/Broken/Tools/Command/NumberedSlashCommand.java b/src/main/java/net/Broken/Tools/Command/NumberedSlashCommand.java index adb0cc5..453df13 100644 --- a/src/main/java/net/Broken/Tools/Command/NumberedSlashCommand.java +++ b/src/main/java/net/Broken/Tools/Command/NumberedSlashCommand.java @@ -1,5 +1,12 @@ package net.Broken.Tools.Command; +import java.io.IOException; +import java.util.List; +import java.util.concurrent.LinkedBlockingQueue; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + import net.Broken.SlashCommand; import net.Broken.Tools.EmbedMessageUtils; import net.Broken.Tools.FindContentOnWebPage; @@ -9,13 +16,6 @@ import net.dv8tion.jda.api.entities.MessageEmbed; import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; 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.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. 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 4ecb78c..800cdde 100644 --- a/src/main/java/net/Broken/Tools/DayListener/Listeners/DailyMadame.java +++ b/src/main/java/net/Broken/Tools/DayListener/Listeners/DailyMadame.java @@ -78,7 +78,7 @@ public class DailyMadame implements NewDayListener { } } } - } catch (IOException |InterruptedException e) { + } catch (IOException | InterruptedException e) { logger.catching(e); } } diff --git a/src/main/java/net/Broken/Tools/DayListener/Listeners/DailyMemberCache.java b/src/main/java/net/Broken/Tools/DayListener/Listeners/DailyMemberCache.java new file mode 100644 index 0000000..fcb8c2b --- /dev/null +++ b/src/main/java/net/Broken/Tools/DayListener/Listeners/DailyMemberCache.java @@ -0,0 +1,26 @@ +package net.Broken.Tools.DayListener.Listeners; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import net.Broken.MainBot; +import net.Broken.Tools.DayListener.NewDayListener; +import net.dv8tion.jda.api.JDA; +import net.dv8tion.jda.api.entities.Guild; + +public class DailyMemberCache implements NewDayListener { + + Logger logger = LogManager.getLogger(); + + @Override + public void onNewDay() { + JDA jda = MainBot.jda; + logger.info("[DAILY] Connected on " + jda.getGuilds().size() + " Guilds:"); + for (Guild server : jda.getGuilds()) { + server.loadMembers().get(); + logger.info("... " + server.getName() + " " + server.getMembers().size() + " Members"); + } + + } + +} diff --git a/src/main/java/net/Broken/Tools/FindContentOnWebPage.java b/src/main/java/net/Broken/Tools/FindContentOnWebPage.java index 1b8d899..fe5d66e 100644 --- a/src/main/java/net/Broken/Tools/FindContentOnWebPage.java +++ b/src/main/java/net/Broken/Tools/FindContentOnWebPage.java @@ -1,15 +1,10 @@ package net.Broken.Tools; -import java.io.BufferedReader; import java.io.IOException; -import java.io.InputStreamReader; import java.net.URI; -import java.net.URL; -import java.net.URLConnection; import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; -import java.nio.charset.StandardCharsets; public class FindContentOnWebPage { /** diff --git a/src/main/java/net/Broken/Tools/LimitChecker.java b/src/main/java/net/Broken/Tools/LimitChecker.java index 1471cc6..11a9ef6 100644 --- a/src/main/java/net/Broken/Tools/LimitChecker.java +++ b/src/main/java/net/Broken/Tools/LimitChecker.java @@ -1,11 +1,10 @@ package net.Broken.Tools; +import java.io.IOException; + import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import java.io.IOException; -import java.net.URL; - /** * Find max webPage for web site like baseURL.com/number-2/ */ @@ -23,7 +22,6 @@ public class LimitChecker { */ public static int doYourJob(String baseURL, int minNumber, String suffix) throws IOException { int number = minNumber; - URL u = null; boolean redirected = false; Redirection redirection = new Redirection();