Skip to content

Commit

Permalink
mention rank roles & check roles on indexing
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaktushose committed Jul 2, 2024
1 parent c10dcc8 commit 597dac6
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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<RankInfo> getRankInfo(int rankId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,27 @@
public record UserInfo(int currentXp, RankInfo currentRank, Optional<RankInfo> nextRank, int messageCount, int xpGain,
int karma, int lastKarma) {

public Map<String, Object> getEmbedValues(User user) {
public Map<String, Object> getEmbedValues(User user, boolean isDM) {
var result = new HashMap<String, Object>() {{
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);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ public Map<String, Object> getEmbedValues(UserSnowflake user) {
var result = new HashMap<String, Object>() {{
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;
Expand Down

0 comments on commit 597dac6

Please sign in to comment.