Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/main/java/com/javadiscord/javabot/Bot.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,24 @@
import com.jagrosh.jdautilities.command.Command;
import com.jagrosh.jdautilities.command.CommandClient;
import com.jagrosh.jdautilities.command.CommandClientBuilder;
import com.javadiscord.javabot.commands.other.Version;
import com.javadiscord.javabot.events.*;
import com.javadiscord.javabot.properties.MultiProperties;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.OnlineStatus;
import net.dv8tion.jda.api.entities.Activity;
import net.dv8tion.jda.api.requests.GatewayIntent;
import net.dv8tion.jda.api.utils.MemberCachePolicy;
import net.dv8tion.jda.api.utils.cache.CacheFlag;
import org.reflections.Reflections;

import java.lang.reflect.Modifier;
import java.nio.file.Path;
import java.util.List;
import java.util.Objects;
import java.util.Properties;
import java.util.concurrent.TimeUnit;


public class Bot {
Expand Down Expand Up @@ -48,7 +52,7 @@ public static void main(String[] args) throws Exception {
jda.addEventListener(new UserJoin());
jda.addEventListener(new UserLeave());
jda.addEventListener(new Startup());
jda.addEventListener(new StatusUpdate());
jda.addEventListener(PresenceUpdater.standardActivities());
jda.addEventListener(new ReactionListener());
jda.addEventListener(new SuggestionListener());
jda.addEventListener(new CstmCmdListener());
Expand Down
105 changes: 105 additions & 0 deletions src/main/java/com/javadiscord/javabot/PresenceUpdater.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package com.javadiscord.javabot;

import com.javadiscord.javabot.commands.other.Version;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.OnlineStatus;
import net.dv8tion.jda.api.entities.Activity;
import net.dv8tion.jda.api.events.ReadyEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;

import javax.annotation.Nonnull;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;

/**
* Periodically updates the bot's Discord presence by cycling through a list of
* activities. Also sets the bot to {@link OnlineStatus#DO_NOT_DISTURB} if it's
* not set like that already.
* <p>
* This updater should be added as an event listener to the bot, so that it
* will automatically begin operation when the bot gives the ready event.
* </p>
*/
public class PresenceUpdater extends ListenerAdapter {
/**
* The executor that is responsible for the scheduled updates of the bot's
* presence data.
*/
private final ScheduledExecutorService threadPool = Executors.newSingleThreadScheduledExecutor();

/**
* A list of functions that take a reference to the bot's JDA client, and
* produce an {@link Activity} that should be displayed.
*/
private final List<Function<JDA, Activity>> activities;

/**
* The index of the currently active activity.
*/
private int currentActivityIndex = 0;

/**
* The amount of time the updater should wait before updating the activity.
*/
private final long delay;

/**
* The unit of time that {@link PresenceUpdater#delay} is counted in.
*/
private final TimeUnit delayUnit;

/**
* A reference to the bot's JDA client object. This is set as soon as the
* bot is ready, and so it will never be null when passed to a function.
*/
private JDA jda;

/**
* Constructs an updater using a list of activities, and a delay and time
* unit to describe how frequently to cycle through them.
* @param activities The list of activity-producing functions.
* @param delay The amount of time the updater should wait before updating the activity.
* @param delayUnit The unit of time that {@link PresenceUpdater#delay} is counted in.
*/
public PresenceUpdater(List<Function<JDA, Activity>> activities, long delay, TimeUnit delayUnit) {
this.activities = new CopyOnWriteArrayList<>(activities);
this.delay = delay;
this.delayUnit = delayUnit;
}

/**
* Called when the Discord bot is ready. This triggers the start of the
* scheduled updating of the bot's activities.
* @param event The ready event that's sent. Notably, contains a reference
* to the bot's {@link JDA} client.
*/
@Override
public void onReady(@Nonnull ReadyEvent event) {
this.jda = event.getJDA();
threadPool.scheduleWithFixedDelay(() -> {
if (this.jda.getPresence().getStatus() != OnlineStatus.DO_NOT_DISTURB) {
this.jda.getPresence().setStatus(OnlineStatus.DO_NOT_DISTURB);
}
if (currentActivityIndex >= this.activities.size()) currentActivityIndex = 0;
if (this.activities.size() > 0) {
this.jda.getPresence().setActivity(this.activities.get(currentActivityIndex++).apply(this.jda));
}
}, 0, this.delay, this.delayUnit);
}

/**
* @return A pre-built implementation of the {@link PresenceUpdater} that
* has all the necessary properties defined to reasonable defaults.
*/
public static PresenceUpdater standardActivities() {
return new PresenceUpdater(List.of(
jda -> Activity.watching("javadiscord.net" + " | " + new Version().getVersion()),
jda -> Activity.listening(jda.getGuilds().get(0).getMemberCount() + " members" + " | " + new Version().getVersion()),
jda -> Activity.watching("/help" + " | " + new Version().getVersion())
), 35, TimeUnit.SECONDS);
}
}
32 changes: 0 additions & 32 deletions src/main/java/com/javadiscord/javabot/StatusUpdate.java

This file was deleted.