From 390dc9da02d78e14a9d6ae3ede4114197da9151b Mon Sep 17 00:00:00 2001 From: Mikusch Date: Mon, 22 Jul 2019 11:02:40 +0200 Subject: [PATCH 1/3] Made channel search generic --- .../commons/utils/FinderUtil.java | 88 ++++--------------- 1 file changed, 17 insertions(+), 71 deletions(-) diff --git a/commons/src/main/java/com/jagrosh/jdautilities/commons/utils/FinderUtil.java b/commons/src/main/java/com/jagrosh/jdautilities/commons/utils/FinderUtil.java index 77e8689c..c8b54915 100644 --- a/commons/src/main/java/com/jagrosh/jdautilities/commons/utils/FinderUtil.java +++ b/commons/src/main/java/com/jagrosh/jdautilities/commons/utils/FinderUtil.java @@ -441,7 +441,7 @@ else if(DISCORD_ID.matcher(query).matches()) return Collections.singletonList(tc); } - return genericTextChannelSearch(query, guild.getTextChannelCache()); + return genericGuildChannelSearch(query, guild.getTextChannelCache()); } private static List jdaTextChannelSearch(String query, JDA jda, boolean useShardManager) @@ -464,34 +464,7 @@ else if(DISCORD_ID.matcher(query).matches()) return Collections.singletonList(tc); } - return genericTextChannelSearch(query, manager != null? manager.getTextChannelCache() : jda.getTextChannelCache()); - } - - private static List genericTextChannelSearch(String query, SnowflakeCacheView cache) - { - ArrayList exact = new ArrayList<>(); - ArrayList wrongcase = new ArrayList<>(); - ArrayList startswith = new ArrayList<>(); - ArrayList contains = new ArrayList<>(); - String lowerquery = query.toLowerCase(); - cache.forEach((tc) -> { - String name = tc.getName(); - if(name.equals(query)) - exact.add(tc); - else if(name.equalsIgnoreCase(query) && exact.isEmpty()) - wrongcase.add(tc); - else if(name.toLowerCase().startsWith(lowerquery) && wrongcase.isEmpty()) - startswith.add(tc); - else if(name.toLowerCase().contains(lowerquery) && startswith.isEmpty()) - contains.add(tc); - }); - if(!exact.isEmpty()) - return Collections.unmodifiableList(exact); - if(!wrongcase.isEmpty()) - return Collections.unmodifiableList(wrongcase); - if(!startswith.isEmpty()) - return Collections.unmodifiableList(startswith); - return Collections.unmodifiableList(contains); + return genericGuildChannelSearch(query, manager != null? manager.getTextChannelCache() : jda.getTextChannelCache()); } /** @@ -557,7 +530,7 @@ public static List findVoiceChannels(String query, Guild guild) if(vc!=null) return Collections.singletonList(vc); } - return genericVoiceChannelSearch(query, guild.getVoiceChannelCache()); + return genericGuildChannelSearch(query, guild.getVoiceChannelCache()); } private static List jdaVoiceChannelSearch(String query, JDA jda, boolean useShardManager) @@ -571,34 +544,7 @@ private static List jdaVoiceChannelSearch(String query, JDA jda, b return Collections.singletonList(vc); } - return genericVoiceChannelSearch(query, manager != null? manager.getVoiceChannelCache() : jda.getVoiceChannelCache()); - } - - private static List genericVoiceChannelSearch(String query, SnowflakeCacheView cache) - { - ArrayList exact = new ArrayList<>(); - ArrayList wrongcase = new ArrayList<>(); - ArrayList startswith = new ArrayList<>(); - ArrayList contains = new ArrayList<>(); - String lowerquery = query.toLowerCase(); - cache.forEach((vc) -> { - String name = vc.getName(); - if(name.equals(query)) - exact.add(vc); - else if(name.equalsIgnoreCase(query) && exact.isEmpty()) - wrongcase.add(vc); - else if(name.toLowerCase().startsWith(lowerquery) && wrongcase.isEmpty()) - startswith.add(vc); - else if(name.toLowerCase().contains(lowerquery) && startswith.isEmpty()) - contains.add(vc); - }); - if(!exact.isEmpty()) - return Collections.unmodifiableList(exact); - if(!wrongcase.isEmpty()) - return Collections.unmodifiableList(wrongcase); - if(!startswith.isEmpty()) - return Collections.unmodifiableList(startswith); - return Collections.unmodifiableList(contains); + return genericGuildChannelSearch(query, manager != null? manager.getVoiceChannelCache() : jda.getVoiceChannelCache()); } /** @@ -665,7 +611,7 @@ public static List findCategories(String query, Guild guild) return Collections.singletonList(cat); } - return genericCategorySearch(query, guild.getCategoryCache()); + return genericGuildChannelSearch(query, guild.getCategoryCache()); } private static List jdaCategorySearch(String query, JDA jda, boolean useShardManager) @@ -679,26 +625,26 @@ private static List jdaCategorySearch(String query, JDA jda, boolean u return Collections.singletonList(cat); } - return genericCategorySearch(query, jda.getCategoryCache()); + return genericGuildChannelSearch(query, jda.getCategoryCache()); } - private static List genericCategorySearch(String query, SnowflakeCacheView cache) + private static List genericGuildChannelSearch(String query, SnowflakeCacheView cache) { - ArrayList exact = new ArrayList<>(); - ArrayList wrongcase = new ArrayList<>(); - ArrayList startswith = new ArrayList<>(); - ArrayList contains = new ArrayList<>(); + ArrayList exact = new ArrayList<>(); + ArrayList wrongcase = new ArrayList<>(); + ArrayList startswith = new ArrayList<>(); + ArrayList contains = new ArrayList<>(); String lowerquery = query.toLowerCase(); - cache.forEach(cat -> { - String name = cat.getName(); + cache.forEach((c) -> { + String name = c.getName(); if(name.equals(query)) - exact.add(cat); + exact.add(c); else if(name.equalsIgnoreCase(query) && exact.isEmpty()) - wrongcase.add(cat); + wrongcase.add(c); else if(name.toLowerCase().startsWith(lowerquery) && wrongcase.isEmpty()) - startswith.add(cat); + startswith.add(c); else if(name.toLowerCase().contains(lowerquery) && startswith.isEmpty()) - contains.add(cat); + contains.add(c); }); if(!exact.isEmpty()) return Collections.unmodifiableList(exact); From 65727b4d461762c33e53b80998792f20f85dd278 Mon Sep 17 00:00:00 2001 From: Mikusch Date: Mon, 22 Jul 2019 11:04:04 +0200 Subject: [PATCH 2/3] Add Store Channel search --- .../commons/utils/FinderUtil.java | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/commons/src/main/java/com/jagrosh/jdautilities/commons/utils/FinderUtil.java b/commons/src/main/java/com/jagrosh/jdautilities/commons/utils/FinderUtil.java index c8b54915..7561a06e 100644 --- a/commons/src/main/java/com/jagrosh/jdautilities/commons/utils/FinderUtil.java +++ b/commons/src/main/java/com/jagrosh/jdautilities/commons/utils/FinderUtil.java @@ -628,6 +628,55 @@ private static List jdaCategorySearch(String query, JDA jda, boolean u return genericGuildChannelSearch(query, jda.getCategoryCache()); } + /** + * Queries a provided instance of {@link net.dv8tion.jda.api.JDA JDA} for + * {@link net.dv8tion.jda.api.entities.StoreChannel StoreChannels}.

+ * + *

The standard search does not follow any special cases. + * + * @param query + * The String query to search by + * @param jda + * The instance of JDA to search from + * + * @return A possibly-empty {@link java.util.List List} of StoreChannels found by the query from the provided JDA instance. + */ + public static List findStoreChannels(String query, JDA jda) + { + if(DISCORD_ID.matcher(query).matches()) + { + StoreChannel sc = jda.getStoreChannelById(query); + if(sc!=null) + return Collections.singletonList(sc); + } + + return genericGuildChannelSearch(query, jda.getStoreChannelCache()); + } + + /** + * Queries a provided {@link net.dv8tion.jda.api.entities.Guild Guild} for + * {@link net.dv8tion.jda.api.entities.StoreChannel StoreChannels}. + * + *

The standard search does not follow any special cases. + * + * @param query + * The String query to search by + * @param guild + * The Guild to search from + * + * @return A possibly-empty {@link java.util.List List} of StoreChannels found by the query from the provided Guild. + */ + public static List findStoreChannels(String query, Guild guild) + { + if(DISCORD_ID.matcher(query).matches()) + { + StoreChannel sc = guild.getStoreChannelById(query); + if(sc!=null) + return Collections.singletonList(sc); + } + return genericGuildChannelSearch(query, guild.getStoreChannelCache()); + } + private static List genericGuildChannelSearch(String query, SnowflakeCacheView cache) { ArrayList exact = new ArrayList<>(); From 270bf1eeab205b631ea0c4eed84f12dc468d166c Mon Sep 17 00:00:00 2001 From: Mikusch Date: Mon, 22 Jul 2019 12:49:14 +0200 Subject: [PATCH 3/3] Add search by mention --- .../jdautilities/commons/utils/FinderUtil.java | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/commons/src/main/java/com/jagrosh/jdautilities/commons/utils/FinderUtil.java b/commons/src/main/java/com/jagrosh/jdautilities/commons/utils/FinderUtil.java index 7561a06e..a72a8fd5 100644 --- a/commons/src/main/java/com/jagrosh/jdautilities/commons/utils/FinderUtil.java +++ b/commons/src/main/java/com/jagrosh/jdautilities/commons/utils/FinderUtil.java @@ -643,7 +643,14 @@ private static List jdaCategorySearch(String query, JDA jda, boolean u */ public static List findStoreChannels(String query, JDA jda) { - if(DISCORD_ID.matcher(query).matches()) + Matcher channelMention = CHANNEL_MENTION.matcher(query); + if(channelMention.matches()) + { + StoreChannel tc = jda.getStoreChannelById(channelMention.group(1)); + if(tc!=null) + return Collections.singletonList(tc); + } + else if(DISCORD_ID.matcher(query).matches()) { StoreChannel sc = jda.getStoreChannelById(query); if(sc!=null) @@ -668,7 +675,14 @@ public static List findStoreChannels(String query, JDA jda) */ public static List findStoreChannels(String query, Guild guild) { - if(DISCORD_ID.matcher(query).matches()) + Matcher channelMention = CHANNEL_MENTION.matcher(query); + if(channelMention.matches()) + { + StoreChannel tc = guild.getStoreChannelById(channelMention.group(1)); + if(tc!=null) + return Collections.singletonList(tc); + } + else if(DISCORD_ID.matcher(query).matches()) { StoreChannel sc = guild.getStoreChannelById(query); if(sc!=null)