1010import net .dv8tion .jda .api .entities .Activity ;
1111
1212import org .jetbrains .annotations .NotNull ;
13+ import org .jetbrains .annotations .Nullable ;
1314
1415import org .slf4j .Logger ;
1516import org .slf4j .LoggerFactory ;
2223
2324import java .sql .SQLException ;
2425import java .util .Scanner ;
26+ import java .util .concurrent .Executors ;
27+ import java .util .concurrent .ScheduledExecutorService ;
2528import java .util .concurrent .TimeUnit ;
2629import java .util .function .Supplier ;
2730
@@ -44,6 +47,10 @@ public class LazyLibrary extends Stringable {
4447 * The {@link JDA} instance
4548 */
4649 public JDA jda ;
50+ /**
51+ * @see #updateActivityRotation()
52+ */
53+ @ Nullable private ScheduledExecutorService activityScheduler ;
4754
4855 /**
4956 * Starts the bot
@@ -115,11 +122,8 @@ public LazyLibrary() {
115122 // All necessary tasks are done
116123 onNecessaryTasksDone ();
117124
118- // Rotating status
119- if (!settings .activities .isEmpty ()) {
120- final Activity [] array = settings .activities .toArray (new Activity [0 ]);
121- LazyUtilities .CPU_SCHEDULER .scheduleAtFixedRate (() -> jda .getPresence ().setActivity (array [LazyUtilities .RANDOM .nextInt (array .length )]), 0 , 1 , TimeUnit .MINUTES );
122- }
125+ // Rotating activity
126+ updateActivityRotation ();
123127 }
124128
125129 /**
@@ -167,6 +171,15 @@ public void onStop() {
167171 // Should be overridden
168172 }
169173
174+ /**
175+ * Called when a command is sent in the console
176+ *
177+ * @param command the {@link ConsoleCommand} that was sent
178+ */
179+ public void onConsoleCommand (@ NotNull ConsoleCommand command ) {
180+ // Should be overridden
181+ }
182+
170183 /**
171184 * Stops the bot (calls {@link #onStop()} and exits the program)
172185 */
@@ -176,12 +189,39 @@ public void stopBot() {
176189 }
177190
178191 /**
179- * Called when a command is sent in the console
192+ * Updates the activity rotation (stops the old scheduler and starts a new one)
180193 *
181- * @param command the {@link ConsoleCommand} that was sent
194+ * @see LazySettings#activities(Activity...)
195+ * @see LazySettings#activities(java.util.Collection)
182196 */
183- public void onConsoleCommand (@ NotNull ConsoleCommand command ) {
184- // Should be overridden
197+ public void updateActivityRotation () {
198+ // Stop old scheduler
199+ if (activityScheduler != null ) activityScheduler .shutdown ();
200+
201+ // Stop activity rotation
202+ if (settings .activities == null ) {
203+ activityScheduler = null ;
204+ return ;
205+ }
206+
207+ // Check if JDA is ready
208+ if (jda == null || jda .getStatus () != JDA .Status .CONNECTED ) return ;
209+
210+ // Start new scheduler
211+ activityScheduler = Executors .newSingleThreadScheduledExecutor ();
212+ activityScheduler .scheduleAtFixedRate (() -> {
213+ // Stop if activities is null
214+ if (settings .activities == null ) {
215+ if (activityScheduler != null ) {
216+ activityScheduler .shutdown ();
217+ activityScheduler = null ;
218+ }
219+ return ;
220+ }
221+
222+ // Set random activity
223+ if (!settings .activities .isEmpty ()) jda .getPresence ().setActivity (settings .activities .get (LazyUtilities .RANDOM .nextInt (settings .activities .size ())));
224+ }, 0 , 1 , TimeUnit .MINUTES );
185225 }
186226
187227 /**
0 commit comments