forked from Fascinated/Bat
maybe fix some NPEs when calling events
This commit is contained in:
parent
c98d8d7e26
commit
3b3ea2b3cc
@ -54,14 +54,15 @@ public class BatUser extends ProfileHolder {
|
||||
*/
|
||||
private User user;
|
||||
|
||||
public BatUser(@NonNull String id, @NonNull org.bson.Document document) {
|
||||
public BatUser(@NonNull String id, User user, @NonNull org.bson.Document document) {
|
||||
this.id = id;
|
||||
this.document = document;
|
||||
boolean newAccount = this.document.isEmpty();
|
||||
this.createdAt = newAccount ? new Date() : document.getDate("createdAt");
|
||||
|
||||
User user = UserUtils.getUser(id);
|
||||
if (user != null) {
|
||||
// User was not passed through
|
||||
if (user == null) {
|
||||
user = UserUtils.getUser(id);
|
||||
this.user = user;
|
||||
this.globalName = user.getGlobalName();
|
||||
}
|
||||
|
@ -78,7 +78,7 @@ public class EventService extends ListenerAdapter {
|
||||
return;
|
||||
}
|
||||
BatGuild guild = guildService.getGuild(event.getGuild().getId());
|
||||
BatUser user = userService.getUser(event.getUser().getId());
|
||||
BatUser user = userService.getUser(event.getUser().getId(), event.getUser());
|
||||
|
||||
for (EventListener listener : LISTENERS) {
|
||||
listener.onGuildMemberJoin(guild, user, event);
|
||||
@ -91,7 +91,7 @@ public class EventService extends ListenerAdapter {
|
||||
return;
|
||||
}
|
||||
BatGuild guild = guildService.getGuild(event.getGuild().getId());
|
||||
BatUser user = userService.getUser(event.getUser().getId());
|
||||
BatUser user = userService.getUser(event.getUser().getId(), event.getUser());
|
||||
|
||||
for (EventListener listener : LISTENERS) {
|
||||
listener.onGuildMemberLeave(guild, user, event);
|
||||
@ -104,7 +104,7 @@ public class EventService extends ListenerAdapter {
|
||||
return;
|
||||
}
|
||||
BatGuild guild = guildService.getGuild(event.getGuild().getId());
|
||||
BatUser user = userService.getUser(event.getAuthor().getId());
|
||||
BatUser user = userService.getUser(event.getAuthor().getId(), event.getAuthor());
|
||||
|
||||
for (EventListener listener : LISTENERS) {
|
||||
listener.onGuildMessageReceive(guild, user, event);
|
||||
@ -128,7 +128,7 @@ public class EventService extends ListenerAdapter {
|
||||
return;
|
||||
}
|
||||
BatGuild guild = event.getGuild() != null ? guildService.getGuild(event.getGuild().getId()) : null;
|
||||
BatUser user = userService.getUser(event.getUser().getId());
|
||||
BatUser user = userService.getUser(event.getUser().getId(), event.getUser());
|
||||
|
||||
for (EventListener listener : LISTENERS) {
|
||||
listener.onStringSelectInteraction(guild, user, event);
|
||||
@ -141,7 +141,7 @@ public class EventService extends ListenerAdapter {
|
||||
return;
|
||||
}
|
||||
BatGuild guild = event.getGuild() != null ? guildService.getGuild(event.getGuild().getId()) : null;
|
||||
BatUser user = userService.getUser(event.getUser().getId());
|
||||
BatUser user = userService.getUser(event.getUser().getId(), event.getUser());
|
||||
|
||||
try {
|
||||
for (EventListener listener : LISTENERS) {
|
||||
@ -159,7 +159,7 @@ public class EventService extends ListenerAdapter {
|
||||
return;
|
||||
}
|
||||
BatGuild guild = event.getGuild() != null ? guildService.getGuild(event.getGuild().getId()) : null;
|
||||
BatUser user = userService.getUser(event.getUser().getId());
|
||||
BatUser user = userService.getUser(event.getUser().getId(), event.getUser());
|
||||
|
||||
try {
|
||||
for (EventListener listener : LISTENERS) {
|
||||
@ -176,7 +176,7 @@ public class EventService extends ListenerAdapter {
|
||||
if (event.getUser().isBot()) {
|
||||
return;
|
||||
}
|
||||
BatUser user = userService.getUser(event.getUser().getId());
|
||||
BatUser user = userService.getUser(event.getUser().getId(), event.getUser());
|
||||
|
||||
for (EventListener listener : LISTENERS) {
|
||||
listener.onUserUpdateGlobalName(user, event.getOldGlobalName(), event.getNewGlobalName(), event);
|
||||
@ -189,7 +189,7 @@ public class EventService extends ListenerAdapter {
|
||||
return;
|
||||
}
|
||||
BatGuild guild = guildService.getGuild(event.getGuild().getId());
|
||||
BatUser user = userService.getUser(event.getUser().getId());
|
||||
BatUser user = userService.getUser(event.getUser().getId(), event.getUser());
|
||||
|
||||
for (EventListener listener : LISTENERS) {
|
||||
listener.onGuildMemberUpdateNickname(guild, user, event.getOldNickname(), event.getNewNickname(), event);
|
||||
@ -202,7 +202,7 @@ public class EventService extends ListenerAdapter {
|
||||
return;
|
||||
}
|
||||
BatGuild guild = guildService.getGuild(event.getGuild().getId());
|
||||
BatUser user = userService.getUser(event.getUser().getId());
|
||||
BatUser user = userService.getUser(event.getUser().getId(), event.getUser());
|
||||
|
||||
for (EventListener listener : LISTENERS) {
|
||||
listener.onGuildMemberRoleAdd(guild, user, event.getRoles(), event);
|
||||
@ -215,7 +215,7 @@ public class EventService extends ListenerAdapter {
|
||||
return;
|
||||
}
|
||||
BatGuild guild = guildService.getGuild(event.getGuild().getId());
|
||||
BatUser user = userService.getUser(event.getUser().getId());
|
||||
BatUser user = userService.getUser(event.getUser().getId(), event.getUser());
|
||||
|
||||
for (EventListener listener : LISTENERS) {
|
||||
listener.onGuildMemberRoleRemove(guild, user, event.getRoles(), event);
|
||||
@ -246,7 +246,7 @@ public class EventService extends ListenerAdapter {
|
||||
return;
|
||||
}
|
||||
BatGuild guild = guildService.getGuild(event.getGuild().getId());
|
||||
BatUser user = userService.getUser(event.getUser().getId());
|
||||
BatUser user = userService.getUser(event.getUser().getId(), event.getUser());
|
||||
|
||||
for (EventListener listener : LISTENERS) {
|
||||
listener.onGuildMemberBan(guild, user, event);
|
||||
@ -259,7 +259,7 @@ public class EventService extends ListenerAdapter {
|
||||
return;
|
||||
}
|
||||
BatGuild guild = guildService.getGuild(event.getGuild().getId());
|
||||
BatUser user = userService.getUser(event.getUser().getId());
|
||||
BatUser user = userService.getUser(event.getUser().getId(), event.getUser());
|
||||
|
||||
for (EventListener listener : LISTENERS) {
|
||||
listener.onGuildMemberUnban(guild, user, event);
|
||||
@ -272,7 +272,7 @@ public class EventService extends ListenerAdapter {
|
||||
return;
|
||||
}
|
||||
BatGuild guild = guildService.getGuild(event.getGuild().getId());
|
||||
BatUser user = userService.getUser(event.getUser().getId());
|
||||
BatUser user = userService.getUser(event.getUser().getId(), event.getUser());
|
||||
|
||||
for (EventListener listener : LISTENERS) {
|
||||
listener.onGuildMemberTimeout(guild, user, event);
|
||||
@ -284,7 +284,7 @@ public class EventService extends ListenerAdapter {
|
||||
if (event.getUser().isBot()) {
|
||||
return;
|
||||
}
|
||||
BatUser user = userService.getUser(event.getUser().getId());
|
||||
BatUser user = userService.getUser(event.getUser().getId(), event.getUser());
|
||||
|
||||
for (EventListener listener : LISTENERS) {
|
||||
listener.onUserUpdateName(user, event.getOldName(), event.getNewName(), event);
|
||||
@ -296,7 +296,7 @@ public class EventService extends ListenerAdapter {
|
||||
if (event.getUser().isBot()) {
|
||||
return;
|
||||
}
|
||||
BatUser user = userService.getUser(event.getUser().getId());
|
||||
BatUser user = userService.getUser(event.getUser().getId(), event.getUser());
|
||||
|
||||
for (EventListener listener : LISTENERS) {
|
||||
listener.onUserUpdateAvatar(user, event.getOldAvatarUrl(), event.getNewAvatarUrl(), event);
|
||||
@ -309,7 +309,7 @@ public class EventService extends ListenerAdapter {
|
||||
return;
|
||||
}
|
||||
BatGuild guild = guildService.getGuild(event.getEntity().getGuild().getId());
|
||||
BatUser user = userService.getUser(event.getEntity().getUser().getId());
|
||||
BatUser user = userService.getUser(event.getEntity().getUser().getId(), event.getEntity().getUser());
|
||||
|
||||
for (EventListener listener : LISTENERS) {
|
||||
listener.onGuildVoiceUpdate(guild, user, event);
|
||||
|
@ -52,12 +52,34 @@ public class UserService implements EventListener {
|
||||
* @param id The ID of the user
|
||||
* @return The user
|
||||
*/
|
||||
public BatUser getUser(@NonNull String id) {
|
||||
public BatUser getUser(@NonNull String id, User user) {
|
||||
long before = System.currentTimeMillis();
|
||||
// User is cached
|
||||
if (users.containsKey(id)) {
|
||||
return users.get(id);
|
||||
}
|
||||
// User is not cached
|
||||
Document document = MongoService.INSTANCE.getUsersCollection().find(Filters.eq("_id", id)).first();
|
||||
if (document != null) {
|
||||
BatUser batUser = new BatUser(id, user, document);
|
||||
users.put(id, batUser);
|
||||
log.info("Loaded user \"{}\" in {}ms", batUser.getName(),System.currentTimeMillis() - before);
|
||||
return batUser;
|
||||
}
|
||||
// New user
|
||||
BatUser batUser = new BatUser(id, user, new Document());
|
||||
users.put(id, batUser);
|
||||
log.info("Created user \"{}\" - \"{}\"", batUser.getName(), batUser.getId());
|
||||
return batUser;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a user by its ID
|
||||
*
|
||||
* @param id The ID of the user
|
||||
* @return The user
|
||||
*/
|
||||
public BatUser getUser(@NonNull String id) {
|
||||
User user = DiscordService.JDA.getUserById(id);
|
||||
if (user == null) {
|
||||
log.warn("Attempted to get user with ID \"{}\" but they do not exist", id);
|
||||
@ -67,19 +89,7 @@ public class UserService implements EventListener {
|
||||
log.warn("Attempted to get user with ID \"{}\" but they are a bot", id);
|
||||
return null;
|
||||
}
|
||||
// User is not cached
|
||||
Document document = MongoService.INSTANCE.getUsersCollection().find(Filters.eq("_id", id)).first();
|
||||
if (document != null) {
|
||||
BatUser batUser = new BatUser(id, document);
|
||||
users.put(id, batUser);
|
||||
log.info("Loaded user \"{}\" in {}ms", batUser.getName(),System.currentTimeMillis() - before);
|
||||
return batUser;
|
||||
}
|
||||
// New user
|
||||
BatUser batUser = new BatUser(id, new Document());
|
||||
users.put(id, batUser);
|
||||
log.info("Created user \"{}\" - \"{}\"", batUser.getName(), batUser.getId());
|
||||
return batUser;
|
||||
return getUser(id, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
Loading…
Reference in New Issue
Block a user