🚑 Use gateway indent and cache to fix guild selector
This commit is contained in:
parent
7b90debc1b
commit
54b207db46
@ -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());
|
||||||
}
|
}
|
||||||
|
@ -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");
|
||||||
}
|
}
|
||||||
|
@ -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;
|
||||||
|
15
src/main/java/net/Broken/Tools/CacheTools.java
Normal file
15
src/main/java/net/Broken/Tools/CacheTools.java
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -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,10 +48,9 @@ public class GeneralWebView {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ResponseStatus(HttpStatus.FORBIDDEN)
|
@ResponseStatus(HttpStatus.FORBIDDEN)
|
||||||
public class ForbiddenException extends RuntimeException {}
|
public class ForbiddenException extends RuntimeException {
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@RequestMapping("/")
|
@RequestMapping("/")
|
||||||
@ -74,10 +70,10 @@ public class GeneralWebView {
|
|||||||
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));
|
||||||
@ -108,7 +104,6 @@ public class GeneralWebView {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -134,7 +129,7 @@ public class GeneralWebView {
|
|||||||
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,11 +137,8 @@ public class GeneralWebView {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return CheckPage.getPageIfReady("settings");
|
return CheckPage.getPageIfReady("settings");
|
||||||
}
|
} else
|
||||||
else
|
|
||||||
throw new ForbiddenException();
|
throw new ForbiddenException();
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -156,13 +148,11 @@ public class GeneralWebView {
|
|||||||
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"));
|
||||||
@ -173,14 +163,13 @@ public class GeneralWebView {
|
|||||||
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();
|
||||||
}
|
}
|
||||||
@ -193,10 +182,16 @@ 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());
|
||||||
|
if (lastCount == null || lastCount != mutualGuilds.size()) {
|
||||||
|
LogManager.getLogger().debug("Guild miss match, re-cache users...");
|
||||||
|
CacheTools.loadAllGuildMembers();
|
||||||
|
mutualGuilds = user.getMutualGuilds();
|
||||||
|
MainBot.mutualGuildCount.put(user.getId(), mutualGuilds.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public static Model addGuildAndRedirect(Model model, String token, String guildId, List<Guild> mutualGuilds){
|
|
||||||
Guild guild = MainBot.jda.getGuildById(guildId);
|
Guild guild = MainBot.jda.getGuildById(guildId);
|
||||||
if (guild != null) {
|
if (guild != null) {
|
||||||
model.addAttribute("guild_name", guild.getName());
|
model.addAttribute("guild_name", guild.getName());
|
||||||
@ -204,9 +199,7 @@ public class GeneralWebView {
|
|||||||
model.addAttribute("guild_icon", guild.getIconUrl() == null ? "https://discordapp.com/assets/dd4dbc0016779df1378e7812eabaa04d.png" : guild.getIconUrl());
|
model.addAttribute("guild_icon", guild.getIconUrl() == null ? "https://discordapp.com/assets/dd4dbc0016779df1378e7812eabaa04d.png" : guild.getIconUrl());
|
||||||
model.addAttribute("mutual_guilds", mutualGuilds);
|
model.addAttribute("mutual_guilds", mutualGuilds);
|
||||||
model.addAttribute("isAdmin", SettingsUtils.getInstance().checkPermission(token, guildId));
|
model.addAttribute("isAdmin", SettingsUtils.getInstance().checkPermission(token, guildId));
|
||||||
}
|
} else {
|
||||||
|
|
||||||
else{
|
|
||||||
model.addAttribute("guild_name", "");
|
model.addAttribute("guild_name", "");
|
||||||
model.addAttribute("guild_icon", "");
|
model.addAttribute("guild_icon", "");
|
||||||
}
|
}
|
||||||
|
@ -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) {
|
||||||
|
Loading…
Reference in New Issue
Block a user