Bat/src/main/java/cc/fascinated/bat/service/UserService.java

104 lines
3.4 KiB
Java
Raw Normal View History

2024-06-24 13:56:01 +01:00
package cc.fascinated.bat.service;
import cc.fascinated.bat.common.TimerUtils;
2024-07-01 01:33:52 +01:00
import cc.fascinated.bat.event.EventListener;
2024-07-01 19:41:13 +01:00
import cc.fascinated.bat.model.BatGuild;
2024-06-25 11:55:26 +01:00
import cc.fascinated.bat.model.BatUser;
import com.mongodb.client.model.Filters;
import lombok.Getter;
2024-06-24 13:56:01 +01:00
import lombok.NonNull;
import lombok.extern.log4j.Log4j2;
2024-07-03 22:59:05 +01:00
import net.dv8tion.jda.api.entities.User;
2024-07-01 19:41:13 +01:00
import net.dv8tion.jda.api.events.guild.member.GuildMemberJoinEvent;
2024-07-01 19:40:32 +01:00
import net.dv8tion.jda.api.events.user.update.UserUpdateGlobalNameEvent;
import org.bson.Document;
2024-06-24 13:56:01 +01:00
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
2024-06-27 23:51:40 +01:00
import java.util.concurrent.TimeUnit;
2024-06-24 13:56:01 +01:00
/**
* @author Fascinated (fascinated7)
*/
2024-06-28 03:01:21 +01:00
@Service
@Log4j2(topic = "User Service")
2024-06-28 03:01:21 +01:00
@Getter
@DependsOn({"discordService", "mongoService"})
2024-07-01 01:33:52 +01:00
public class UserService implements EventListener {
private static final long SAVE_INTERVAL = TimeUnit.MINUTES.toMillis(5);
2024-06-24 13:56:01 +01:00
/**
* The cached users
2024-06-24 13:56:01 +01:00
*/
private final Map<String, BatUser> users = new HashMap<>();
2024-06-24 13:56:01 +01:00
@Autowired
public UserService() {
TimerUtils.scheduleRepeating(() -> {
long before = System.currentTimeMillis();
for (BatUser user : users.values()) {
user.save();
}
log.info("Saved {} users in {}ms", users.size(), System.currentTimeMillis() - before);
}, SAVE_INTERVAL, SAVE_INTERVAL);
2024-06-24 13:56:01 +01:00
}
/**
* Gets a user by its ID
2024-06-24 13:56:01 +01:00
*
* @param id The ID of the user
* @return The user
*/
public BatUser getUser(@NonNull String id) {
long before = System.currentTimeMillis();
// User is cached
if (users.containsKey(id)) {
return users.get(id);
}
2024-07-03 22:59:05 +01:00
User user = DiscordService.JDA.getUserById(id);
if (user == null) {
log.warn("Attempted to get user with ID \"{}\" but they do not exist", id);
return null;
}
if (user.isBot()) {
log.warn("Attempted to get user with ID \"{}\" but they are a bot", id);
2024-07-01 01:41:40 +01:00
return null;
}
// User is not cached
Document document = MongoService.INSTANCE.getUsersCollection().find(Filters.eq("_id", id)).first();
if (document != null) {
2024-07-03 22:59:05 +01:00
BatUser batUser = new BatUser(id, document);
users.put(id, batUser);
log.info("Loaded user \"{}\" in {}ms", batUser.getName(),System.currentTimeMillis() - before);
return batUser;
2024-06-24 13:56:01 +01:00
}
// New user
2024-07-03 22:59:05 +01:00
BatUser batUser = new BatUser(id, new Document());
users.put(id, batUser);
2024-07-04 03:17:07 +01:00
log.info("Created user \"{}\" - \"{}\"", batUser.getName(), batUser.getId());
2024-07-03 22:59:05 +01:00
return batUser;
2024-06-24 13:56:01 +01:00
}
2024-07-01 01:33:52 +01:00
@Override
2024-07-04 02:25:26 +01:00
public void onShutdown() {
2024-07-01 01:33:52 +01:00
log.info("Saving all users before shutdown...");
for (BatUser user : users.values()) {
user.save();
}
log.info("Saved all users.");
}
2024-07-01 19:40:32 +01:00
2024-07-01 19:41:13 +01:00
@Override
public void onGuildMemberJoin(@NonNull BatGuild guild, @NonNull BatUser user, @NonNull GuildMemberJoinEvent event) {
2024-07-04 02:25:26 +01:00
user.setGlobalName(event.getUser().getGlobalName()); // Ensure the user's name is up-to-date
2024-07-01 19:41:13 +01:00
}
2024-07-01 19:40:32 +01:00
@Override
public void onUserUpdateGlobalName(@NonNull BatUser user, String oldName, String newName, @NonNull UserUpdateGlobalNameEvent event) {
user.setGlobalName(newName);
}
2024-06-24 13:56:01 +01:00
}