Skip to content

Commit ed2ce98

Browse files
committed
add Javadoc comments
1 parent 82c9908 commit ed2ce98

4 files changed

Lines changed: 89 additions & 4 deletions

File tree

‎src/main/java/net/discordjug/javabot/systems/color_roles/ChangeColorCommand.java‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,26 @@
1818
import xyz.dynxsty.dih4jda.interactions.commands.application.SlashCommand;
1919
import xyz.dynxsty.dih4jda.util.AutoCompleteUtils;
2020

21+
/**
22+
* The {@code /color} command which allows users to change their own color with color roles.
23+
*/
2124
public class ChangeColorCommand extends SlashCommand implements AutoCompletable {
2225

2326
private static final Pattern COLOR_PATTERN = Pattern.compile("#[A-F0-9]{6}", Pattern.CASE_INSENSITIVE);
2427

2528
private final BotConfig config;
2629
private final ColorRoleService service;
2730

31+
/**
32+
* The constructor which sets the corresponding {@link net.dv8tion.jda.api.interactions.commands.build.SlashCommandData}.
33+
* @param botConfig The bot's configuration
34+
* @param service The {@link ColorRoleService} to actually change color roles
35+
*/
2836
public ChangeColorCommand(BotConfig botConfig, ColorRoleService service) {
2937
this.config = botConfig;
3038
this.service = service;
3139
setCommandData(Commands.slash("color", "Sets the color of your roles")
32-
.addOption(OptionType.STRING, "color", "The hex code of the role color", false, true));
40+
.addOption(OptionType.STRING, "color", "The hex code of the role color (leave empty to remove your color roles)", false, true));
3341
}
3442

3543
@Override
Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
11
package net.discordjug.javabot.systems.color_roles;
22

33
import java.awt.Color;
4+
import java.util.Objects;
45

6+
/**
7+
* A self-assignable color role.
8+
* @param guildId The Discord ID of the guild the role is a part of
9+
* @param roleId The Discord ID of the role
10+
* @param color The color of the role
11+
* @see ChangeColorCommand
12+
* @see ColorRoleService
13+
*/
514
public record ColorRole(long guildId, long roleId, Color color) {
6-
15+
public ColorRole {
16+
Objects.requireNonNull(color);
17+
}
718
}

‎src/main/java/net/discordjug/javabot/systems/color_roles/ColorRoleService.java‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
import net.dv8tion.jda.api.hooks.ListenerAdapter;
1414
import org.springframework.stereotype.Service;
1515

16+
/**
17+
* Service class for assigning and removing color roles.
18+
* @see ColorRoleService#addColor(Member, Color)
19+
* @see ColorRoleService#removeColor(Member)
20+
*/
1621
@Service
1722
public class ColorRoleService extends ListenerAdapter {
1823

@@ -24,6 +29,13 @@ public ColorRoleService(ColorRoleRepository repo, BotConfig config) {
2429
this.config = config;
2530
}
2631

32+
/**
33+
* Adds a color role to a user.
34+
*
35+
* If a color role with the given color exists already, it will be reused. Otherwise, it will create a new one.
36+
* @param member The member to add the role to
37+
* @param color The color of the role to assign
38+
*/
2739
public synchronized void addColor(Member member, Color color) {
2840
repo.getFromColor(member.getGuild().getIdLong(), color)
2941
.ifPresentOrElse(colorRole -> {
@@ -44,6 +56,13 @@ public synchronized void addColor(Member member, Color color) {
4456
});
4557
}
4658

59+
/**
60+
* Removes all color roles from a user.
61+
*
62+
* If no other members have that role, the role will be deleted.
63+
* @param member The member to remove the role from
64+
* @return {@code true} if any color roles were removed, else {@code false}
65+
*/
4766
public synchronized boolean removeColor(Member member) {
4867
boolean changed = false;
4968
for (ColorRole color : repo.getColorRolesOfMember(member.getGuild().getIdLong(), member.getIdLong())) {

‎src/main/java/net/discordjug/javabot/systems/color_roles/dao/ColorRoleRepository.java‎

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,56 @@
44
import java.util.List;
55
import java.util.Optional;
66

7+
import javax.annotation.CheckReturnValue;
8+
79
import lombok.RequiredArgsConstructor;
810
import net.discordjug.javabot.systems.color_roles.ColorRole;
911
import org.springframework.dao.EmptyResultDataAccessException;
1012
import org.springframework.jdbc.core.JdbcTemplate;
1113
import org.springframework.stereotype.Repository;
1214

15+
/**
16+
* Repository class for the COLOR_ROLES and COLOR_ROLE_MEMBERS tables which are used for role colors.
17+
*/
1318
@Repository
1419
@RequiredArgsConstructor
1520
public class ColorRoleRepository {
1621
private final JdbcTemplate jdbcTemplate;
1722

23+
/**
24+
* Gets all color roles of a guild.
25+
* @param guildId The ID of the guild
26+
* @return A {@link List} of all registered role colors in the guild
27+
*/
1828
public List<ColorRole> getColorRoles(long guildId) {
1929
return jdbcTemplate.query("SELECT ROLE_ID, COLOR FROM COLOR_ROLES WHERE GUILD_ID=?",
2030
(rs, _) -> new ColorRole(guildId, rs.getLong(1), new Color(rs.getInt(2))),
2131
guildId);
2232
}
2333

34+
/**
35+
* Tries to find a color role given the color.
36+
* @param guildId The ID of the guild containing the role
37+
* @param color The color of the role to search for
38+
* @return The color role if it exists or {@link Optional#empty()}
39+
*/
2440
public Optional<ColorRole> getFromColor(long guildId, Color color) {
2541
try {
2642
ColorRole result = jdbcTemplate.queryForObject("SELECT ROLE_ID FROM COLOR_ROLES WHERE GUILD_ID=? AND COLOR=?",
27-
((rs, _) -> new ColorRole(guildId, rs.getLong(1), color)),
43+
(rs, _) -> new ColorRole(guildId, rs.getLong(1), color),
2844
guildId, color.getRGB());
2945
return Optional.ofNullable(result);
3046
} catch(EmptyResultDataAccessException _) {
3147
return Optional.empty();
3248
}
3349
}
3450

51+
/**
52+
* Gets all color roles of a given member.
53+
* @param guildId The ID of the guild the roles should be part of
54+
* @param memberId The ID of the member get the roles of
55+
* @return All color roles of the given member in the given guild
56+
*/
3557
public List<ColorRole> getColorRolesOfMember(long guildId, long memberId) {
3658
return jdbcTemplate.query("""
3759
SELECT cr.ROLE_ID, cr.COLOR
@@ -43,19 +65,44 @@ public List<ColorRole> getColorRolesOfMember(long guildId, long memberId) {
4365
guildId, memberId);
4466
}
4567

68+
/**
69+
* Registers that a member has a given color role.
70+
* @param color The color role
71+
* @param memberId The member the role is being added to
72+
*/
4673
public void addColorRole(ColorRole color, long memberId) {
4774
jdbcTemplate.update("INSERT INTO COLOR_ROLES (ROLE_ID, GUILD_ID, COLOR) VALUES (?,?,?)",
4875
color.roleId(), color.guildId(), color.color().getRGB());
49-
jdbcTemplate.update("INSERT INTO COLOR_ROLE_MEMBERS (ROLE_ID, USER_ID) VALUES (?, ?)", color.roleId(), memberId);
76+
addRoleToMember(color.roleId(), memberId);
5077
}
5178

79+
/**
80+
* Registers that a color role has been added to a member.
81+
* @param roleId The ID of the role the member has
82+
* @param memberId The ID of the member to update
83+
*/
5284
public void addRoleToMember(long roleId, long memberId) {
5385
jdbcTemplate.update("INSERT INTO COLOR_ROLE_MEMBERS (ROLE_ID, USER_ID) VALUES (?,?)", roleId, memberId);
5486
}
87+
88+
/**
89+
* Registers that a color role has been added to a member.
90+
* @param roleId The ID of the role removed from the member
91+
* @param memberId The ID of the member to update
92+
* @see #cleanRoleIfNoMembers(long)
93+
*/
5594
public void removeRoleFromMember(long roleId, long memberId) {
5695
jdbcTemplate.update("DELETE FROM COLOR_ROLE_MEMBERS WHERE ROLE_ID=? AND USER_ID=?", roleId, memberId);
5796
}
5897

98+
/**
99+
* Removes a role from the database if it has no members.
100+
*
101+
* Callers should make sure to actually delete the role if this method returns {@code true}.
102+
* @param roleId The ID of the role to possibly remove
103+
* @return {@code true} if the role is registered as removed due to not having any members, else {@code false}
104+
*/
105+
@CheckReturnValue
59106
public boolean cleanRoleIfNoMembers(long roleId) {
60107
return jdbcTemplate.update("DELETE FROM COLOR_ROLES WHERE ROLE_ID=? AND (SELECT COUNT(*) FROM COLOR_ROLE_MEMBERS WHERE ROLE_ID=?)=0", roleId, roleId) > 0;
61108
}

0 commit comments

Comments
 (0)