From 597dac6b827284d7c2f3f50863ca06d963004ff7 Mon Sep 17 00:00:00 2001 From: Kaktushose Date: Tue, 2 Jul 2024 20:33:23 +0200 Subject: [PATCH] mention rank roles & check roles on indexing --- .../kaktushose/nplaybot/rank/JoinLeaveListener.java | 1 + .../kaktushose/nplaybot/rank/RankService.java | 12 ++++++++---- .../nplaybot/rank/commands/RankInfoCommand.java | 2 +- .../nplaybot/rank/daily/DailyMessageTask.java | 2 +- .../kaktushose/nplaybot/rank/model/UserInfo.java | 13 +++++++++++-- .../nplaybot/rank/model/XpChangeResult.java | 4 ++-- 6 files changed, 24 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/github/kaktushose/nplaybot/rank/JoinLeaveListener.java b/src/main/java/com/github/kaktushose/nplaybot/rank/JoinLeaveListener.java index 548227f..b4cd47b 100644 --- a/src/main/java/com/github/kaktushose/nplaybot/rank/JoinLeaveListener.java +++ b/src/main/java/com/github/kaktushose/nplaybot/rank/JoinLeaveListener.java @@ -16,6 +16,7 @@ public JoinLeaveListener(RankService rankService) { @Override public void onGuildMemberJoin(@NotNull GuildMemberJoinEvent event) { rankService.createUser(event.getUser()); + rankService.updateRankRoles(event.getMember(), rankService.getUserInfo(event.getMember()).currentRank()); } @Override diff --git a/src/main/java/com/github/kaktushose/nplaybot/rank/RankService.java b/src/main/java/com/github/kaktushose/nplaybot/rank/RankService.java index 4996ee2..c616585 100644 --- a/src/main/java/com/github/kaktushose/nplaybot/rank/RankService.java +++ b/src/main/java/com/github/kaktushose/nplaybot/rank/RankService.java @@ -37,12 +37,13 @@ public RankService(DataSource dataSource, ItemService itemService, Bot bot) { this.guild = bot.getGuild(); } - public void createUser(UserSnowflake user) { + public boolean createUser(UserSnowflake user) { log.debug("Inserting user: {}", user); try (Connection connection = dataSource.getConnection()) { - var statement = connection.prepareStatement("INSERT INTO users VALUES(?) ON CONFLICT DO NOTHING"); + var statement = connection.prepareStatement("INSERT INTO users VALUES(?) ON CONFLICT DO NOTHING RETURNING user_id"); statement.setLong(1, user.getIdLong()); - statement.execute(); + var result = statement.executeQuery(); + return result.next(); } catch (SQLException e) { throw new RuntimeException(e); } @@ -60,7 +61,10 @@ public void removeUser(UserSnowflake user) { } public void indexMembers() { - guild.loadMembers(member -> createUser(member.getUser())); + guild.loadMembers(member -> { + createUser(member.getUser()); + updateRankRoles(member, getUserInfo(member).currentRank()); + }); } public Optional getRankInfo(int rankId) { diff --git a/src/main/java/com/github/kaktushose/nplaybot/rank/commands/RankInfoCommand.java b/src/main/java/com/github/kaktushose/nplaybot/rank/commands/RankInfoCommand.java index 4c5b8ec..75caaf9 100644 --- a/src/main/java/com/github/kaktushose/nplaybot/rank/commands/RankInfoCommand.java +++ b/src/main/java/com/github/kaktushose/nplaybot/rank/commands/RankInfoCommand.java @@ -38,7 +38,7 @@ public void onContextRankInfo(CommandEvent event, User user) { private void sendReply(UserInfo userInfo, User user, CommandEvent event) { var embed = embedCache.getEmbed(userInfo.nextRank().isPresent() ? "rankInfo" : "rankInfoMax") - .injectValues(userInfo.getEmbedValues(user)) + .injectValues(userInfo.getEmbedValues(user, false)) .toEmbedBuilder(); if (database.getCollectEventService().isCollectEventActive()) { diff --git a/src/main/java/com/github/kaktushose/nplaybot/rank/daily/DailyMessageTask.java b/src/main/java/com/github/kaktushose/nplaybot/rank/daily/DailyMessageTask.java index b6408ee..8127a45 100644 --- a/src/main/java/com/github/kaktushose/nplaybot/rank/daily/DailyMessageTask.java +++ b/src/main/java/com/github/kaktushose/nplaybot/rank/daily/DailyMessageTask.java @@ -16,7 +16,7 @@ public void onSendDailyMessages(Bot bot) { .flatMap(channel -> channel.sendMessage( bot.getEmbedCache().getEmbed(userInfo.nextRank().isPresent() ? "rankInfo" : "rankInfoMax") - .injectValues(userInfo.getEmbedValues(channel.getUser())).toMessageCreateData() + .injectValues(userInfo.getEmbedValues(channel.getUser(), true)).toMessageCreateData() ) ).queue() ); diff --git a/src/main/java/com/github/kaktushose/nplaybot/rank/model/UserInfo.java b/src/main/java/com/github/kaktushose/nplaybot/rank/model/UserInfo.java index 8fe10bd..51a9173 100644 --- a/src/main/java/com/github/kaktushose/nplaybot/rank/model/UserInfo.java +++ b/src/main/java/com/github/kaktushose/nplaybot/rank/model/UserInfo.java @@ -9,18 +9,27 @@ public record UserInfo(int currentXp, RankInfo currentRank, Optional nextRank, int messageCount, int xpGain, int karma, int lastKarma) { - public Map getEmbedValues(User user) { + public Map getEmbedValues(User user, boolean isDM) { var result = new HashMap() {{ put("user", String.format("<@%d>", user.getIdLong())); put("color", currentRank.color()); put("avatarUrl", user.getEffectiveAvatarUrl()); - put("currentRank", currentRank.name()); + if (isDM) { + put("currentRank", currentRank.name()); + } else { + put("currentRank", String.format("<@&%d>", currentRank.roleId())); + } put("currentXp", currentXp); put("karma", karma); put("xpGain", xpGain); put("messageCount", messageCount); }}; nextRank.ifPresent(rank -> { + if (isDM) { + result.put("nextRank", rank.name()); + } else { + result.put("nextRank", String.format("<@&%d>", rank.roleId())); + } result.put("nextRank", rank.name()); result.put("missingXp", rank.xpBound() - currentXp); }); diff --git a/src/main/java/com/github/kaktushose/nplaybot/rank/model/XpChangeResult.java b/src/main/java/com/github/kaktushose/nplaybot/rank/model/XpChangeResult.java index 93700a5..7106158 100644 --- a/src/main/java/com/github/kaktushose/nplaybot/rank/model/XpChangeResult.java +++ b/src/main/java/com/github/kaktushose/nplaybot/rank/model/XpChangeResult.java @@ -13,10 +13,10 @@ public Map getEmbedValues(UserSnowflake user) { var result = new HashMap() {{ put("user", String.format("<@%d>", user.getIdLong())); put("color", currentRank.color()); - put("currentRank", currentRank.name()); + put("currentRank", String.format("<@&%d>", currentRank.roleId())); }}; nextRank.ifPresent(rank -> { - result.put("nextRank", rank.name()); + result.put("nextRank", String.format("<@&%d>", rank.roleId())); result.put("xp", rank.xpBound() - currentXp); }); return result;