99import net .javadiscord .javabot .systems .help .dao .HelpTransactionRepository ;
1010import net .javadiscord .javabot .systems .help .model .HelpAccount ;
1111import net .javadiscord .javabot .systems .help .model .HelpTransaction ;
12- import net .javadiscord .javabot .systems .help .model .HelpTransactionMessage ;
1312import net .javadiscord .javabot .util .ExceptionLogger ;
1413import net .javadiscord .javabot .util .Pair ;
1514import org .jetbrains .annotations .NotNull ;
2120import java .util .List ;
2221import java .util .Optional ;
2322
24- import javax .sql .DataSource ;
25-
2623/**
2724 * Service class that handles Help Experience Transactions.
2825 */
2926@ Slf4j
3027@ RequiredArgsConstructor
3128@ Service
3229public class HelpExperienceService {
33- private final DataSource dataSource ;
3430 private final BotConfig botConfig ;
3531 private final HelpAccountRepository helpAccountRepository ;
3632 private final HelpTransactionRepository helpTransactionRepository ;
@@ -45,73 +41,56 @@ public class HelpExperienceService {
4541 @ Transactional
4642 public HelpAccount getOrCreateAccount (long userId ) throws DataAccessException {
4743 HelpAccount account ;
48- Optional <HelpAccount > optional = helpAccountRepository .getByUserId (userId );
49- if (optional .isPresent ()) {
50- account = optional .get ();
51- } else {
52- account = new HelpAccount (botConfig );
53- account .setUserId (userId );
54- account .setExperience (0 );
55- helpAccountRepository .insert (account );
56- }
57- return account ;
44+ Optional <HelpAccount > optional = helpAccountRepository .getByUserId (userId );
45+ if (optional .isPresent ()) {
46+ account = optional .get ();
47+ } else {
48+ account = new HelpAccount (botConfig );
49+ account .setUserId (userId );
50+ account .setExperience (0 );
51+ helpAccountRepository .insert (account );
52+ }
53+ return account ;
5854 }
5955
6056 /**
6157 * Returns the specified amount of {@link HelpAccount}s, sorted by their experience.
6258 *
6359 * @param amount The amount to retrieve.
64- * @param page The page to get.
60+ * @param page The page to get.
6561 * @return A {@link List} of {@link HelpAccount}s.
6662 */
6763 public List <HelpAccount > getTopAccounts (int amount , int page ) {
68- try {
64+ try {
6965 return helpAccountRepository .getAccounts (page , amount );
7066 } catch (DataAccessException e ) {
7167 ExceptionLogger .capture (e , getClass ().getSimpleName ());
7268 return Collections .emptyList ();
7369 }
7470 }
7571
76- /**
77- * Gets all recent help transactions from a user.
78- *
79- * @param userId The user's id.
80- * @param count The count of transactions to retrieve.
81- * @return A {@link List} with all transactions.
82- * @throws DataAccessException If an error occurs.
83- */
84- public List <HelpTransaction > getRecentTransactions (long userId , int count ) throws DataAccessException {
85- List <HelpTransaction > transactions = helpTransactionRepository .getTransactions (userId , count );
86- return transactions ;
87- }
88-
8972 /**
9073 * Performs a single transaction.
9174 *
92- * @param recipient The recipient's user id.
93- * @param value The transaction's value.
94- * @param message The transaction's message.
95- * @param guild The current guild.
96- * @return A {@link HelpTransaction} object.
75+ * @param recipient The recipient's user id.
76+ * @param value The transaction's value.
77+ * @param guild The current guild.
9778 * @throws DataAccessException If an error occurs.
9879 */
9980 @ Transactional
100- public HelpTransaction performTransaction (long recipient , double value , HelpTransactionMessage message , Guild guild ) throws DataAccessException {
81+ public void performTransaction (long recipient , double value , Guild guild ) throws DataAccessException {
10182 if (value == 0 ) {
10283 log .error ("Cannot make zero-value transactions" );
103- return null ;
84+ return ;
10485 }
10586 HelpTransaction transaction = new HelpTransaction ();
10687 transaction .setRecipient (recipient );
10788 transaction .setWeight (value );
108- transaction .setMessageType (message .ordinal ());
109- HelpAccount account = this .getOrCreateAccount (recipient );
89+ HelpAccount account = getOrCreateAccount (recipient );
11090 account .updateExperience (value );
11191 helpAccountRepository .update (account );
112- transaction = helpTransactionRepository .save (transaction );
113- this .checkExperienceRoles (guild , account );
114- return helpTransactionRepository .getTransaction (transaction .getId ()).orElse (null );
92+ helpTransactionRepository .save (transaction );
93+ checkExperienceRoles (guild , account );
11594 }
11695
11796 private void checkExperienceRoles (@ NotNull Guild guild , @ NotNull HelpAccount account ) {
@@ -122,8 +101,11 @@ private void checkExperienceRoles(@NotNull Guild guild, @NotNull HelpAccount acc
122101 if (key .equals (role .first ().getIdLong ())) {
123102 guild .addRoleToMember (member , role .first ()).queue ();
124103 } else {
125- guild .removeRoleFromMember (member , guild .getRoleById (key )).queue ();
104+ Role remove = guild .getRoleById (key );
105+ if (remove != null ) {
106+ guild .removeRoleFromMember (member , remove ).queue ();
107+ }
126108 }
127- }), e -> {});
109+ }), e -> {});
128110 }
129111}
0 commit comments