🚑 Use gateway indent and cache to fix guild selector

This commit is contained in:
SebClem 2020-11-18 19:25:07 +01:00
parent 7b90debc1b
commit 54b207db46
6 changed files with 64 additions and 49 deletions

View File

@ -151,6 +151,7 @@ public class BotListener extends ListenerAdapter {
@Override @Override
public void onMessageReceived(MessageReceivedEvent event) { public void onMessageReceived(MessageReceivedEvent event) {
if (!event.getAuthor().isBot()) { if (!event.getAuthor().isBot()) {
UserStatsUtils.getINSTANCE().addMessageCount(event.getMember()); UserStatsUtils.getINSTANCE().addMessageCount(event.getMember());
} }

View File

@ -14,6 +14,8 @@ import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.OnlineStatus; import net.dv8tion.jda.api.OnlineStatus;
import net.dv8tion.jda.api.entities.Activity; import net.dv8tion.jda.api.entities.Activity;
import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.requests.GatewayIntent;
import net.dv8tion.jda.api.utils.MemberCachePolicy;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
@ -44,7 +46,8 @@ public class Init {
logger.info("Connecting to Discord api..."); logger.info("Connecting to Discord api...");
//connection au bot //connection au bot
JDABuilder jdaB = JDABuilder.createDefault(token); JDABuilder jdaB = JDABuilder.createDefault(token).enableIntents(GatewayIntent.GUILD_MEMBERS)
.setMemberCachePolicy(MemberCachePolicy.ALL);
jdaB.setBulkDeleteSplittingEnabled(false); jdaB.setBulkDeleteSplittingEnabled(false);
jda = jdaB.build(); jda = jdaB.build();
jda = jda.awaitReady(); jda = jda.awaitReady();
@ -69,6 +72,7 @@ public class Init {
logger.info("Connected on " + jda.getGuilds().size() + " Guilds:"); logger.info("Connected on " + jda.getGuilds().size() + " Guilds:");
for (Guild server : jda.getGuilds()) { for (Guild server : jda.getGuilds()) {
server.loadMembers().get();
//on recupere les utilisateur //on recupere les utilisateur
logger.info("... " + server.getName() + " " + server.getMembers().size() + " Members"); logger.info("... " + server.getName() + " " + server.getMembers().size() + " Members");
} }

View File

@ -10,6 +10,7 @@ import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.ChannelType; import net.dv8tion.jda.api.entities.ChannelType;
import net.dv8tion.jda.api.entities.Member; import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.Message; import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.User;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.springframework.boot.ExitCodeGenerator; import org.springframework.boot.ExitCodeGenerator;
@ -31,6 +32,7 @@ public class MainBot {
public static HashMap<String, Commande> commandes = new HashMap<>(); public static HashMap<String, Commande> commandes = new HashMap<>();
public static HashMap<Member, ArrayList<Message>> historique =new HashMap<>(); public static HashMap<Member, ArrayList<Message>> historique =new HashMap<>();
public static HashMap<Member, Integer> message_compteur =new HashMap<>(); public static HashMap<Member, Integer> message_compteur =new HashMap<>();
public static HashMap<String, Integer> mutualGuildCount =new HashMap<>();
public static boolean roleFlag = false; public static boolean roleFlag = false;
public static HashMap<Member, UserSpamUtils> spamUtils = new HashMap<>(); public static HashMap<Member, UserSpamUtils> spamUtils = new HashMap<>();
public static JDA jda; public static JDA jda;

View File

@ -0,0 +1,15 @@
package net.Broken.Tools;
import net.Broken.MainBot;
import net.dv8tion.jda.api.entities.Guild;
import java.util.List;
public class CacheTools {
public static void loadAllGuildMembers(){
List<Guild> guilds = MainBot.jda.getGuilds();
for(Guild guild : guilds){
guild.loadMembers().get();
}
}
}

View File

@ -3,6 +3,7 @@ package net.Broken.webView;
import net.Broken.DB.Entity.UserEntity; import net.Broken.DB.Entity.UserEntity;
import net.Broken.DB.Repository.UserRepository; import net.Broken.DB.Repository.UserRepository;
import net.Broken.MainBot; import net.Broken.MainBot;
import net.Broken.Tools.CacheTools;
import net.Broken.Tools.SettingsUtils; import net.Broken.Tools.SettingsUtils;
import net.Broken.Tools.UserManager.Exceptions.UnknownTokenException; import net.Broken.Tools.UserManager.Exceptions.UnknownTokenException;
import net.Broken.Tools.UserManager.Stats.GuildStatsPack; import net.Broken.Tools.UserManager.Stats.GuildStatsPack;
@ -22,10 +23,6 @@ import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.ResponseStatus;
import javax.servlet.http.Cookie; import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
@ -51,15 +48,14 @@ public class GeneralWebView {
} }
@ResponseStatus(HttpStatus.FORBIDDEN) @ResponseStatus(HttpStatus.FORBIDDEN)
public class ForbiddenException extends RuntimeException {} public class ForbiddenException extends RuntimeException {
}
@RequestMapping("/") @RequestMapping("/")
public String music(Model model, HttpServletResponse response, HttpServletRequest request, @CookieValue(value = "guild", defaultValue = "1") String guildId, @CookieValue(value = "token", defaultValue = "") String token){ public String music(Model model, HttpServletResponse response, HttpServletRequest request, @CookieValue(value = "guild", defaultValue = "1") String guildId, @CookieValue(value = "token", defaultValue = "") String token) {
if(token.equals("")){ if (token.equals("")) {
model.addAttribute("isLogged", false); model.addAttribute("isLogged", false);
model.addAttribute("guild_name", ""); model.addAttribute("guild_name", "");
model.addAttribute("isAdmin", false); model.addAttribute("isAdmin", false);
@ -72,12 +68,12 @@ public class GeneralWebView {
UserEntity userE = userUtils.getUserWithApiToken(userRepository, token); UserEntity userE = userUtils.getUserWithApiToken(userRepository, token);
User user = MainBot.jda.getUserById(userE.getJdaId()); User user = MainBot.jda.getUserById(userE.getJdaId());
if(user == null) { if (user == null) {
model.addAttribute("noMutualGuilds", true); model.addAttribute("noMutualGuilds", true);
addGuildAndRedirect(model, token, guildId, new ArrayList<>()); addGuildAndRedirect(model, token, guildId, user);
} else { } else {
model.addAttribute("noMutualGuilds", false); model.addAttribute("noMutualGuilds", false);
addGuildAndRedirect(model, token, guildId, user.getMutualGuilds()); addGuildAndRedirect(model, token, guildId, user);
} }
model.addAttribute("isAdmin", SettingsUtils.getInstance().checkPermission(token, guildId)); model.addAttribute("isAdmin", SettingsUtils.getInstance().checkPermission(token, guildId));
@ -98,7 +94,7 @@ public class GeneralWebView {
model.addAttribute("redirect_url", System.getenv("OAUTH_URL")); model.addAttribute("redirect_url", System.getenv("OAUTH_URL"));
return CheckPage.getPageIfReady("login"); return CheckPage.getPageIfReady("login");
} catch (NumberFormatException e){ } catch (NumberFormatException e) {
logger.debug("Unknown guild, flush cookies"); logger.debug("Unknown guild, flush cookies");
Cookie cookie = new Cookie("guild", null); Cookie cookie = new Cookie("guild", null);
cookie.setPath("/"); cookie.setPath("/");
@ -108,33 +104,32 @@ public class GeneralWebView {
} }
} }
@RequestMapping("/loading") @RequestMapping("/loading")
public String loading(Model model){ public String loading(Model model) {
return "loading"; return "loading";
} }
@RequestMapping("/forgetPass") @RequestMapping("/forgetPass")
public String forgetPass(Model model){ public String forgetPass(Model model) {
return CheckPage.getPageIfReady("forgetPass"); return CheckPage.getPageIfReady("forgetPass");
} }
@RequestMapping("/oauthCallback") @RequestMapping("/oauthCallback")
public String oauthCallback(Model model){ public String oauthCallback(Model model) {
return "oauthCallback"; return "oauthCallback";
} }
@RequestMapping("/settings") @RequestMapping("/settings")
public String settings(Model model, @CookieValue(value = "guild", defaultValue = "") String guildId, @CookieValue(value = "token", defaultValue = "") String token){ public String settings(Model model, @CookieValue(value = "guild", defaultValue = "") String guildId, @CookieValue(value = "token", defaultValue = "") String token) {
SettingsUtils settingsUtils = SettingsUtils.getInstance(); SettingsUtils settingsUtils = SettingsUtils.getInstance();
if(settingsUtils.checkPermission(token, guildId)){ if (settingsUtils.checkPermission(token, guildId)) {
try { try {
UserEntity userE = userUtils.getUserWithApiToken(userRepository, token); UserEntity userE = userUtils.getUserWithApiToken(userRepository, token);
User user = MainBot.jda.getUserById(userE.getJdaId()); User user = MainBot.jda.getUserById(userE.getJdaId());
addGuildAndRedirect(model, token, guildId, user.getMutualGuilds()); addGuildAndRedirect(model, token, guildId, user);
Guild guild = MainBot.jda.getGuildById(guildId); Guild guild = MainBot.jda.getGuildById(guildId);
model.addAttribute("settings", SettingsUtils.getInstance().extractSettings(guild)); model.addAttribute("settings", SettingsUtils.getInstance().extractSettings(guild));
} catch (UnknownTokenException e) { } catch (UnknownTokenException e) {
@ -142,49 +137,43 @@ public class GeneralWebView {
} }
return CheckPage.getPageIfReady("settings"); return CheckPage.getPageIfReady("settings");
} } else
else
throw new ForbiddenException(); throw new ForbiddenException();
} }
@RequestMapping("/login") @RequestMapping("/login")
public String login(Model model, @CookieValue(value = "token", defaultValue = "") String token){ public String login(Model model, @CookieValue(value = "token", defaultValue = "") String token) {
model.addAttribute("redirect_url", System.getenv("OAUTH_URL")); model.addAttribute("redirect_url", System.getenv("OAUTH_URL"));
if(!token.equals("")){ if (!token.equals("")) {
return "redirect:/"; return "redirect:/";
} } else
else
return "login"; return "login";
} }
@RequestMapping("/rank") @RequestMapping("/rank")
public String login(Model model, @CookieValue(value = "token",defaultValue = "1") String token, @CookieValue(value = "guild", defaultValue = "") String cookieGuildId, @RequestParam(value = "guild", defaultValue = "") String praramGuildId){ public String login(Model model, @CookieValue(value = "token", defaultValue = "1") String token, @CookieValue(value = "guild", defaultValue = "") String cookieGuildId, @RequestParam(value = "guild", defaultValue = "") String praramGuildId) {
model.addAttribute("redirect_url", System.getenv("OAUTH_URL")); model.addAttribute("redirect_url", System.getenv("OAUTH_URL"));
try { try {
UserEntity userEntity = userUtils.getUserWithApiToken(userRepository, token); UserEntity userEntity = userUtils.getUserWithApiToken(userRepository, token);
GuildStatsPack stack; GuildStatsPack stack;
if(!cookieGuildId.equals("")){ if (!cookieGuildId.equals("")) {
stack = UserStatsUtils.getINSTANCE().getStatPack(userEntity, cookieGuildId); stack = UserStatsUtils.getINSTANCE().getStatPack(userEntity, cookieGuildId);
} } else
else
stack = null; stack = null;
model.addAttribute("stack", stack); model.addAttribute("stack", stack);
try { try {
UserEntity userE = userUtils.getUserWithApiToken(userRepository, token); UserEntity userE = userUtils.getUserWithApiToken(userRepository, token);
User user = MainBot.jda.getUserById(userE.getJdaId()); User user = MainBot.jda.getUserById(userE.getJdaId());
addGuildAndRedirect(model, token, cookieGuildId, user.getMutualGuilds()); addGuildAndRedirect(model, token, cookieGuildId, user);
} catch (UnknownTokenException e) { } catch (UnknownTokenException e) {
throw new ForbiddenException(); throw new ForbiddenException();
} }
return CheckPage.getPageIfReady("rank"); return CheckPage.getPageIfReady("rank");
} catch (UnknownTokenException e) { } catch (UnknownTokenException e) {
return "login"; // TODO Public rank return "login"; // TODO Public rank
@ -193,20 +182,24 @@ public class GeneralWebView {
} }
public static Model addGuildAndRedirect(Model model, String token, String guildId, User user) {
List<Guild> mutualGuilds = user.getMutualGuilds();
Integer lastCount = MainBot.mutualGuildCount.get(user.getId());
public static Model addGuildAndRedirect(Model model, String token, String guildId, List<Guild> mutualGuilds){ if (lastCount == null || lastCount != mutualGuilds.size()) {
Guild guild = MainBot.jda.getGuildById(guildId); LogManager.getLogger().debug("Guild miss match, re-cache users...");
if(guild != null){ CacheTools.loadAllGuildMembers();
model.addAttribute("guild_name", guild.getName()); mutualGuilds = user.getMutualGuilds();
model.addAttribute("guild_id", guild.getId()); MainBot.mutualGuildCount.put(user.getId(), mutualGuilds.size());
model.addAttribute("guild_icon", guild.getIconUrl() == null ? "https://discordapp.com/assets/dd4dbc0016779df1378e7812eabaa04d.png": guild.getIconUrl());
model.addAttribute("mutual_guilds", mutualGuilds);
model.addAttribute("isAdmin", SettingsUtils.getInstance().checkPermission(token, guildId));
} }
else{ Guild guild = MainBot.jda.getGuildById(guildId);
if (guild != null) {
model.addAttribute("guild_name", guild.getName());
model.addAttribute("guild_id", guild.getId());
model.addAttribute("guild_icon", guild.getIconUrl() == null ? "https://discordapp.com/assets/dd4dbc0016779df1378e7812eabaa04d.png" : guild.getIconUrl());
model.addAttribute("mutual_guilds", mutualGuilds);
model.addAttribute("isAdmin", SettingsUtils.getInstance().checkPermission(token, guildId));
} else {
model.addAttribute("guild_name", ""); model.addAttribute("guild_name", "");
model.addAttribute("guild_icon", ""); model.addAttribute("guild_icon", "");
} }

View File

@ -51,7 +51,7 @@ public class MusicWebView {
User user = MainBot.jda.getUserById(userE.getJdaId()); User user = MainBot.jda.getUserById(userE.getJdaId());
if(user == null) if(user == null)
return "redirect:/"; return "redirect:/";
GeneralWebView.addGuildAndRedirect(model, token, guildId, user.getMutualGuilds()); GeneralWebView.addGuildAndRedirect(model, token, guildId, user);
return CheckPage.getPageIfReady("music"); return CheckPage.getPageIfReady("music");
} catch (UnknownTokenException e) { } catch (UnknownTokenException e) {