Skip to content

Commit

Permalink
feat(gateway events): added proper support for event superclasses, su…
Browse files Browse the repository at this point in the history
…ch as Event.java and InteractionEvent.java, meaning end users can now create their own listeners for those such classes and they'll get the relevant events, for example, all events for Event.java and all interaction events for InteractionEvent.java
  • Loading branch information
seailz committed Feb 25, 2024
1 parent 4a287e0 commit cf3cbb4
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 3 deletions.
21 changes: 21 additions & 0 deletions src/main/java/com/seailz/discordjar/events/DiscordListener.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.seailz.discordjar.events;

import com.seailz.discordjar.command.listeners.CommandListener;
import com.seailz.discordjar.events.model.Event;
import com.seailz.discordjar.events.model.automod.AutoModExecutionEvent;
import com.seailz.discordjar.events.model.automod.rule.AutoModRuleCreateEvent;
import com.seailz.discordjar.events.model.automod.rule.AutoModRuleUpdateEvent;
Expand Down Expand Up @@ -33,13 +34,33 @@
* Once extended, you can override the methods you want to listen for.
* Then, just register them in your main class.
*
* <br><br>To implement a listener for a method that is not present in this class, such as {@link com.seailz.discordjar.events.model.interaction.InteractionEvent InteractionEvent},
* you can use create your own method, and it should work provided you still use the {@link com.seailz.discordjar.events.annotation.EventMethod EventMethod} annotation.
* Example:
* <code>
* <br>public class MyListener extends DiscordListener {
* <br> @EventMethod
* <br> public void onInteractionEvent(@NotNull InteractionEvent event) {
* <br> // Do something
* <br> }
* <br>}
* <br>
* </code>
*
* @author Seailz
* @see com.seailz.discordjar.events.EventDispatcher
* @see com.seailz.discordjar.events.annotation.EventMethod
* @since 1.0
*/
public abstract class DiscordListener {
// General Events

/**
* Any event that is fired by the Discord API will be sent to this method.
*/
public void onEvent(@NotNull Event event) {
}

public void onReady(@NotNull ReadyEvent event) {
}

Expand Down
20 changes: 19 additions & 1 deletion src/main/java/com/seailz/discordjar/events/EventDispatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.seailz.discordjar.events.model.interaction.CustomIdable;
import com.seailz.discordjar.utils.annotation.RequireCustomId;
import com.seailz.discordjar.utils.thread.DiscordJarThreadAllocator;
import org.jetbrains.annotations.NotNull;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
Expand Down Expand Up @@ -66,6 +67,23 @@ public void addListener(DiscordListener... listeners) {
}
}

/**
* Returns a list of all listeners that are registered to the dispatcher relating to the given event type.
* <br>This includes superclasses, so if someone registered a listener for {@link Event}, they'll get all events, or {@link com.seailz.discordjar.events.model.interaction.InteractionEvent InteractionEvents} and they'll get all interaction events, etc.
* @param eventType The type of event to get listeners for
* @return A list of all listeners that are registered to the dispatcher relating to the given event type
*/
@NotNull
private List<ListenerMethodPair> findListenersForEvent(@NotNull Class<? extends Event> eventType) {
List<ListenerMethodPair> listeners = new ArrayList<>();
listenersByEventType.forEach((event, listenerMethodPairs) -> {
if (event.isAssignableFrom(eventType)) {
listeners.addAll(listenerMethodPairs);
}
});
return listeners;
}

/**
* Dispatches an event to all registered listeners.
* This method is called by the {@link DiscordJar} class & other internal classes and should not be called by the end user.
Expand All @@ -78,7 +96,7 @@ public void dispatchEvent(Event event, Class<? extends Event> type, DiscordJar d
if (event == null) return;
new Thread(() -> {
long start = System.currentTimeMillis();
List<ListenerMethodPair> listenersForEventType = listenersByEventType.get(type);
List<ListenerMethodPair> listenersForEventType = findListenersForEvent(type);
if (listenersForEventType == null) {
return;
}
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/com/seailz/discordjar/events/model/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
public class Event {

private String name;
private final DiscordJar bot;
private final long sequence;
private final JSONObject json;
Expand All @@ -27,6 +28,18 @@ public Event(@NotNull DiscordJar bot, long sequence, @NotNull JSONObject data) {
this.json = data;
}

/**
* Returns the literal name of the event - e.g. {@code MESSAGE_CREATE}
*/
@NotNull
public String getName() {
return name;
}

public void setName(@NotNull String name) {
this.name = name;
}

@NotNull
public DiscordJar getBot() {
return bot;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package com.seailz.discordjar.events.model.guild.member;

import com.seailz.discordjar.DiscordJar;
import com.seailz.discordjar.events.model.Event;
import com.seailz.discordjar.events.model.guild.GuildEvent;
import com.seailz.discordjar.model.guild.Member;
import com.seailz.discordjar.utils.rest.DiscordRequest;
import org.jetbrains.annotations.NotNull;
import org.json.JSONObject;

Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/seailz/discordjar/gateway/Gateway.java
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,8 @@ private void handleDispatch(@NotNull JSONObject payload) {
return;
}

event.setName(payload.getString("t"));

bot.getEventDispatcher().dispatchEvent(event, eventClass, bot);

if (bot.isDebug()) {
Expand Down

0 comments on commit cf3cbb4

Please sign in to comment.