Skip to content

Commit 3e42ff2

Browse files
Merge pull request #355 from Java-Discord/dynxsty/fix_help_account
Fixed NPE when using `/help account`
2 parents 9d2bf1c + 31fc65a commit 3e42ff2

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

src/main/java/net/javadiscord/javabot/systems/help/commands/HelpAccountSubcommand.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,11 @@ public void execute(@NotNull SlashCommandInteractionEvent event) {
8888
}
8989

9090
private String formatExperience(Guild guild, HelpAccount account) {
91-
double currentXp = account.getExperience() - account.getPreviousExperienceGoal(guild).second();
91+
Pair<Role, Double> previousRoleAndXp = account.getPreviousExperienceGoal(guild);
9292
Pair<Role, Double> currentRoleAndXp = account.getCurrentExperienceGoal(guild);
9393
Pair<Role, Double> nextRoleAndXp = account.getNextExperienceGoal(guild);
94-
double goalXp = nextRoleAndXp.second() - account.getPreviousExperienceGoal(guild).second();
94+
double currentXp = account.getExperience() - (previousRoleAndXp == null ? 0 : previousRoleAndXp.second());
95+
double goalXp = nextRoleAndXp.second() - (previousRoleAndXp == null ? 0 : previousRoleAndXp.second());
9596
StringBuilder sb = new StringBuilder();
9697

9798
if (currentRoleAndXp.first() != null) {

src/main/java/net/javadiscord/javabot/systems/help/model/HelpAccount.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import net.dv8tion.jda.api.entities.Role;
66
import net.javadiscord.javabot.Bot;
77
import net.javadiscord.javabot.util.Pair;
8+
import org.jetbrains.annotations.NotNull;
9+
import org.jetbrains.annotations.Nullable;
810

911
import java.util.Comparator;
1012
import java.util.Map;
@@ -28,7 +30,7 @@ public void updateExperience(double change) {
2830
* @param guild The current {@link Guild}.
2931
* @return A {@link Pair} with both the Role, and the experience needed.
3032
*/
31-
public Pair<Role, Double> getCurrentExperienceGoal(Guild guild) {
33+
public @NotNull Pair<Role, Double> getCurrentExperienceGoal(Guild guild) {
3234
Map<Long, Double> experienceRoles = Bot.getConfig().get(guild).getHelpConfig().getExperienceRoles();
3335
Map.Entry<Long, Double> highestExperience = Map.entry(0L, 0.0);
3436
for (Map.Entry<Long, Double> entry : experienceRoles.entrySet()) {
@@ -45,7 +47,7 @@ public Pair<Role, Double> getCurrentExperienceGoal(Guild guild) {
4547
* @param guild The current {@link Guild}.
4648
* @return The {@link Pair} with both the Role, and the experience needed.
4749
*/
48-
public Pair<Role, Double> getPreviousExperienceGoal(Guild guild) {
50+
public @Nullable Pair<Role, Double> getPreviousExperienceGoal(Guild guild) {
4951
Map<Long, Double> experienceRoles = Bot.getConfig().get(guild).getHelpConfig().getExperienceRoles();
5052
Optional<Pair<Role, Double>> experienceOptional = experienceRoles.entrySet().stream()
5153
.filter(r -> r.getValue() < experience)
@@ -60,7 +62,7 @@ public Pair<Role, Double> getPreviousExperienceGoal(Guild guild) {
6062
* @param guild The current {@link Guild}.
6163
* @return A {@link Pair} with both the Role, and the experience needed.
6264
*/
63-
public Pair<Role, Double> getNextExperienceGoal(Guild guild) {
65+
public @NotNull Pair<Role, Double> getNextExperienceGoal(Guild guild) {
6466
Map<Long, Double> experienceRoles = Bot.getConfig().get(guild).getHelpConfig().getExperienceRoles();
6567
Map.Entry<Long, Double> entry = experienceRoles.entrySet()
6668
.stream()

0 commit comments

Comments
 (0)