2020import net .javadiscord .javabot .util .ExceptionLogger ;
2121import net .javadiscord .javabot .util .Responses ;
2222import org .jetbrains .annotations .NotNull ;
23+ import org .springframework .dao .DataAccessException ;
2324
24- import java .sql .Connection ;
25- import java .sql .SQLException ;
25+ import java .util .Optional ;
2626import java .util .concurrent .ExecutionException ;
2727import java .util .concurrent .ExecutorService ;
2828
@@ -38,6 +38,7 @@ public class StarboardManager extends ListenerAdapter {
3838 private final ExecutorService asyncPool ;
3939 private final DataSource dataSource ;
4040 private final DbHelper dbHelper ;
41+ private final StarboardRepository starboardRepository ;
4142
4243 @ Override
4344 public void onMessageReactionAdd (@ NotNull MessageReactionAddEvent event ) {
@@ -62,14 +63,21 @@ private void handleReactionEvent(Guild guild, Emoji emoji, MessageChannel channe
6263 channel .retrieveMessageById (messageId ).queue (
6364 message -> {
6465 int stars = getReactionCountForEmote (starEmote , message );
65- dbHelper .doDaoAction (StarboardRepository ::new , dao -> {
66- StarboardEntry entry = dao .getEntryByMessageId (message .getIdLong ());
67- if (entry != null ) {
68- updateStarboardMessage (message , stars , config );
69- } else if (stars >= config .getReactionThreshold ()) {
70- addMessageToStarboard (message , stars , config );
71- } else if (stars < 1 && !removeMessageFromStarboard (message .getIdLong (), channel , config )) {
72- log .error ("Could not remove Message from Starboard" );
66+ asyncPool .execute (()->{
67+ try {
68+ starboardRepository
69+ .getEntryByMessageId (message .getIdLong ())
70+ .ifPresentOrElse (entry ->{
71+ updateStarboardMessage (message , stars , config );
72+ }, ()->{
73+ if (stars >= config .getReactionThreshold ()) {
74+ addMessageToStarboard (message , stars , config );
75+ } else if (stars < 1 && !removeMessageFromStarboard (message .getIdLong (), channel , config )) {
76+ log .error ("Could not remove Message from Starboard" );
77+ }
78+ });
79+ } catch (DataAccessException e ) {
80+ ExceptionLogger .capture (e , StarboardManager .class .getSimpleName ());
7381 }
7482 });
7583 }, e -> log .error ("Could not add Message to Starboard" , e )
@@ -85,19 +93,21 @@ private boolean isInvalidChannel(@NotNull MessageChannel channel) {
8593 @ Override
8694 public void onMessageDelete (@ NotNull MessageDeleteEvent event ) {
8795 if (isInvalidChannel (event .getChannel ())) return ;
88- try (Connection con = dataSource .getConnection ()) {
89- StarboardRepository repo = new StarboardRepository (con );
96+ try {
9097 StarboardConfig config = botConfig .get (event .getGuild ()).getStarboardConfig ();
91- StarboardEntry entry ;
98+ Optional < StarboardEntry > entry ;
9299 if (event .getChannel ().getIdLong () == config .getStarboardChannelId ()) {
93- entry = repo .getEntryByStarboardMessageId (event .getMessageIdLong ());
100+ entry = starboardRepository .getEntryByStarboardMessageId (event .getMessageIdLong ());
94101 } else {
95- entry = repo .getEntryByMessageId (event .getMessageIdLong ());
96- }
97- if (entry != null && !removeMessageFromStarboard (entry .getOriginalMessageId (), event .getChannel (), config )) {
98- log .error ("Could not remove Message from Starboard" );
102+ entry = starboardRepository .getEntryByMessageId (event .getMessageIdLong ());
99103 }
100- } catch (SQLException e ) {
104+ entry .ifPresent (e ->{
105+ if (!removeMessageFromStarboard (e .getOriginalMessageId (), event .getChannel (), config )) {
106+ log .error ("Could not remove Message from Starboard" );
107+ }
108+ });
109+
110+ } catch (DataAccessException e ) {
101111 ExceptionLogger .capture (e , getClass ().getSimpleName ());
102112 }
103113 }
@@ -121,7 +131,7 @@ private int getReactionCountForEmote(Emoji emoji, @NotNull Message message) {
121131 .orElse (0 );
122132 }
123133
124- private void addMessageToStarboard (Message message , int stars , @ NotNull StarboardConfig config ) throws SQLException {
134+ private void addMessageToStarboard (Message message , int stars , @ NotNull StarboardConfig config ) throws DataAccessException {
125135 if (stars < config .getReactionThreshold ()) return ;
126136 MessageEmbed embed = buildStarboardEmbed (message );
127137 MessageAction action = config .getStarboardChannel ()
@@ -136,22 +146,27 @@ private void addMessageToStarboard(Message message, int stars, @NotNull Starboar
136146 }
137147 }
138148 action .queue (starboardMessage -> {
139- StarboardEntry entry = new StarboardEntry ();
140- entry .setOriginalMessageId (message .getIdLong ());
141- entry .setGuildId (message .getGuild ().getIdLong ());
142- entry .setChannelId (message .getChannel ().getIdLong ());
143- entry .setAuthorId (message .getAuthor ().getIdLong ());
144- entry .setStarboardMessageId (starboardMessage .getIdLong ());
145- dbHelper .doDaoAction (StarboardRepository ::new , dao -> dao .insert (entry ));
149+ StarboardEntry entry = new StarboardEntry ();
150+ entry .setOriginalMessageId (message .getIdLong ());
151+ entry .setGuildId (message .getGuild ().getIdLong ());
152+ entry .setChannelId (message .getChannel ().getIdLong ());
153+ entry .setAuthorId (message .getAuthor ().getIdLong ());
154+ entry .setStarboardMessageId (starboardMessage .getIdLong ());
155+ asyncPool .execute (()->{
156+ try {
157+ starboardRepository .insert (entry );
158+ } catch (DataAccessException e ) {
159+ ExceptionLogger .capture (e , StarboardManager .class .getSimpleName ());
160+ }
161+ });
146162 }, e -> log .error ("Could not send Message to Starboard" , e )
147163 );
148164 }
149165
150- private void updateStarboardMessage (@ NotNull Message message , int stars , @ NotNull StarboardConfig config ) throws SQLException {
151- try (Connection con = dataSource .getConnection ()) {
152- StarboardRepository repo = new StarboardRepository (con );
153- StarboardEntry starboardEntry = repo .getEntryByMessageId (message .getIdLong ());
154- long starboardId = starboardEntry .getStarboardMessageId ();
166+ private void updateStarboardMessage (@ NotNull Message message , int stars , @ NotNull StarboardConfig config ) throws DataAccessException {
167+ Optional <StarboardEntry > starboardEntry = starboardRepository .getEntryByMessageId (message .getIdLong ());
168+ starboardEntry .ifPresentOrElse (entry ->{
169+ long starboardId = entry .getStarboardMessageId ();
155170 config .getStarboardChannel ().retrieveMessageById (starboardId ).queue (
156171 starboardMessage -> {
157172 if (starboardMessage .getAuthor ().getIdLong () != message .getJDA ().getSelfUser ().getIdLong ()) {
@@ -163,7 +178,7 @@ private void updateStarboardMessage(@NotNull Message message, int stars, @NotNul
163178 if (!removeMessageFromStarboard (message .getIdLong (), message .getChannel (), config )) {
164179 log .error ("Could not remove Message from Starboard" );
165180 }
166- } catch (SQLException e ) {
181+ } catch (DataAccessException e ) {
167182 ExceptionLogger .capture (e , getClass ().getSimpleName ());
168183 }
169184 } else {
@@ -178,28 +193,26 @@ private void updateStarboardMessage(@NotNull Message message, int stars, @NotNul
178193 log .error ("Could not retrieve original Message. Deleting corresponding Starboard Entry..." );
179194 try {
180195 removeMessageFromStarboard (message .getIdLong (), message .getChannel (), config );
181- } catch (SQLException ex ) {
196+ } catch (DataAccessException ex ) {
182197 ex .printStackTrace ();
183198 }
184199 }
185200 );
186- }
201+ }, ()->log .error ("updateStarboardMessage called but StarboardEntry was not found" ));
202+
187203 }
188204
189- private boolean removeMessageFromStarboard (long messageId , MessageChannel channel , StarboardConfig config ) throws SQLException {
190- try (Connection con = dataSource .getConnection ()) {
191- StarboardRepository repo = new StarboardRepository (con );
192- StarboardEntry entry = repo .getEntryByMessageId (messageId );
193- if (entry == null ) return false ;
194- if (!channel .equals (config .getStarboardChannel ())) {
195- config .getStarboardChannel ().retrieveMessageById (entry .getStarboardMessageId ()).queue (
196- starboardMessage -> starboardMessage .delete ().queue (), ExceptionLogger ::capture
197- );
198- }
199- repo .delete (messageId );
200- log .info ("Removed Starboard Entry with message Id {}" , messageId );
201- return true ;
205+ private boolean removeMessageFromStarboard (long messageId , MessageChannel channel , StarboardConfig config ) throws DataAccessException {
206+ Optional <StarboardEntry > entry = starboardRepository .getEntryByMessageId (messageId );
207+ if (entry .isEmpty ()) return false ;
208+ if (!channel .equals (config .getStarboardChannel ())) {
209+ config .getStarboardChannel ().retrieveMessageById (entry .get ().getStarboardMessageId ()).queue (
210+ starboardMessage -> starboardMessage .delete ().queue (), ExceptionLogger ::capture
211+ );
202212 }
213+ starboardRepository .delete (messageId );
214+ log .info ("Removed Starboard Entry with message Id {}" , messageId );
215+ return true ;
203216 }
204217
205218 private @ NotNull MessageEmbed buildStarboardEmbed (@ NotNull Message message ) {
0 commit comments