Skip to content

Commit

Permalink
Merge pull request #185 from discord-jar/feature/more_gateway_events
Browse files Browse the repository at this point in the history
feat(events): implemented more gateway events
  • Loading branch information
seailz authored Jun 13, 2023
2 parents af7beff + 7efdf1f commit 6631f85
Show file tree
Hide file tree
Showing 10 changed files with 433 additions and 81 deletions.
51 changes: 50 additions & 1 deletion src/main/java/com/seailz/discordjar/events/DiscordListener.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
package com.seailz.discordjar.events;

import com.seailz.discordjar.command.listeners.CommandListener;
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;
import com.seailz.discordjar.events.model.channel.ChannelPinsUpdateEvent;
import com.seailz.discordjar.events.model.channel.edit.ChannelCreateEvent;
import com.seailz.discordjar.events.model.channel.edit.ChannelUpdateEvent;
import com.seailz.discordjar.events.model.command.CommandPermissionUpdateEvent;
import com.seailz.discordjar.events.model.gateway.GatewayResumedEvent;
import com.seailz.discordjar.events.model.general.ReadyEvent;
import com.seailz.discordjar.events.model.guild.GuildCreateEvent;
import com.seailz.discordjar.events.model.guild.GuildDeleteEvent;
import com.seailz.discordjar.events.model.guild.GuildUpdateEvent;
import com.seailz.discordjar.events.model.guild.member.GuildMemberAddEvent;
import com.seailz.discordjar.events.model.guild.member.GuildMemberRemoveEvent;
import com.seailz.discordjar.events.model.guild.member.GuildMemberUpdateEvent;
Expand All @@ -15,6 +24,7 @@
import com.seailz.discordjar.events.model.interaction.select.entity.RoleSelectMenuInteractionEvent;
import com.seailz.discordjar.events.model.interaction.select.entity.UserSelectMenuInteractionEvent;
import com.seailz.discordjar.events.model.message.MessageCreateEvent;
import com.seailz.discordjar.events.model.message.TypingStartEvent;
import org.jetbrains.annotations.NotNull;

/**
Expand All @@ -33,12 +43,24 @@ public abstract class DiscordListener {
public void onReady(@NotNull ReadyEvent event) {
}

public void onGatewayResume(@NotNull GatewayResumedEvent event) {
}

// Message Events
public void onMessageReceived(@NotNull MessageCreateEvent event) {
}

public void onTypingStart(@NotNull TypingStartEvent event) {
}

// Guild Events
public void onGuildCreated(@NotNull GuildCreateEvent event) {
public void onGuildCreate(@NotNull GuildCreateEvent event) {
}

public void onGuildUpdate(@NotNull GuildUpdateEvent event) {
}

public void onGuildDelete(@NotNull GuildDeleteEvent event) {
}

// Guild Member Events
Expand All @@ -51,6 +73,19 @@ public void onGuildMemberUpdate(@NotNull GuildMemberUpdateEvent event) {
public void onGuildMemberRemove(@NotNull GuildMemberRemoveEvent event) {
}

// Channel Events
public void onChannelCreate(@NotNull ChannelCreateEvent event) {
}

public void onChannelUpdate(@NotNull ChannelUpdateEvent event) {
}

public void onChannelDelete(@NotNull ChannelCreateEvent event) {
}

public void onChannelPinsUpdate(@NotNull ChannelPinsUpdateEvent event) {
}

// Command Events
public void onCommandPermissionUpdate(@NotNull CommandPermissionUpdateEvent event) {
}
Expand Down Expand Up @@ -86,4 +121,18 @@ public void onButtonClickInteractionEvent(@NotNull ButtonInteractionEvent event)
public void onModalInteractionEvent(@NotNull ModalInteractionEvent event) {
}


// Automod
public void onAutoModRuleCreate(@NotNull AutoModRuleCreateEvent event) {
}

public void onAutoModRuleUpdate(@NotNull AutoModRuleUpdateEvent event) {
}

public void onAutoModRuleDelete(@NotNull AutoModRuleUpdateEvent event) {
}

public void onAutoModActionExecution(@NotNull AutoModExecutionEvent event) {
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.seailz.discordjar.events.model.channel;

import com.seailz.discordjar.DiscordJar;
import com.seailz.discordjar.events.model.Event;
import com.seailz.discordjar.model.channel.Channel;
import org.jetbrains.annotations.NotNull;
import org.json.JSONObject;

public class ChannelEvent extends Event {
public ChannelEvent(@NotNull DiscordJar bot, long sequence, @NotNull JSONObject data) {
super(bot, sequence, data);
}

public Channel getChannel() {
return Channel.decompile(getJson(), getBot());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.seailz.discordjar.events.model.channel;

import com.seailz.discordjar.DiscordJar;
import com.seailz.discordjar.events.model.Event;
import com.seailz.discordjar.model.channel.Channel;
import com.seailz.discordjar.model.channel.MessagingChannel;
import com.seailz.discordjar.model.guild.Guild;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.json.JSONObject;

import java.util.Date;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
* Dispatched when the pins for a channel are updated. This could be because:
* <ul>
* <li>A message was pinned</li>
* <li>A message was unpinned</li>
* </ul>
* <p>It is not dispatched when a pinned message is deleted.</p>
*/
public class ChannelPinsUpdateEvent extends Event {

private String guildId;
private Guild guild;
private String channelId;
private Channel channel;
private LocalDateTime lastPinTimestamp;


public ChannelPinsUpdateEvent(@NotNull DiscordJar bot, long sequence, @NotNull JSONObject data) {
super(bot, sequence, data);

guildId = data.has("guild_id") ? data.getString("guild_id") : null;
channelId = data.getString("channel_id");
// last_pin_timestamp is a iso8601 timestamp, so we need to get a Date object from it
if (data.has("last_pin_timestamp")) {
String timestampString = data.getString("last_pin_timestamp");
DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE_TIME;
lastPinTimestamp = LocalDateTime.parse(timestampString, formatter);
}
}

@Nullable
public String getGuildId() {
return guildId;
}

@Nullable
public Guild getGuild() {
if (guild == null && guildId != null)
guild = getBot().getGuildById(guildId);
return guild;
}

@NotNull
public String getChannelId() {
return channelId;
}

@Nullable
public Channel getChannel() {
if (channel == null)
channel = getBot().getChannelById(channelId);
return channel;
}

/**
* The time at which the most recent pinned msg was pinned.
*/
@Nullable
public LocalDateTime getLastPinTimestamp() {
return lastPinTimestamp;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.seailz.discordjar.events.model.channel.edit;

import com.seailz.discordjar.DiscordJar;
import com.seailz.discordjar.events.model.Event;
import com.seailz.discordjar.events.model.channel.ChannelEvent;
import org.jetbrains.annotations.NotNull;
import org.json.JSONObject;

public class ChannelCreateEvent extends ChannelEvent {
public ChannelCreateEvent(@NotNull DiscordJar bot, long sequence, @NotNull JSONObject data) {
super(bot, sequence, data);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.seailz.discordjar.events.model.channel.edit;

import com.seailz.discordjar.DiscordJar;
import com.seailz.discordjar.events.model.channel.ChannelEvent;
import org.jetbrains.annotations.NotNull;
import org.json.JSONObject;

public class ChannelDeleteEvent extends ChannelEvent {
public ChannelDeleteEvent(@NotNull DiscordJar bot, long sequence, @NotNull JSONObject data) {
super(bot, sequence, data);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.seailz.discordjar.events.model.channel.edit;

import com.seailz.discordjar.DiscordJar;
import com.seailz.discordjar.events.model.channel.ChannelEvent;
import org.jetbrains.annotations.NotNull;
import org.json.JSONObject;

public class ChannelUpdateEvent extends ChannelEvent {
public ChannelUpdateEvent(@NotNull DiscordJar bot, long sequence, @NotNull JSONObject data) {
super(bot, sequence, data);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.seailz.discordjar.events.model.guild;

import com.seailz.discordjar.DiscordJar;
import com.seailz.discordjar.events.model.Event;
import com.seailz.discordjar.model.guild.UnavailableGuild;
import org.jetbrains.annotations.NotNull;
import org.json.JSONObject;

/**
* Sent when the guild becomes or was already unavailable due to an outage, or when the user leaves or is removed from a guild.
*/
public class GuildDeleteEvent extends Event {

private UnavailableGuild guild;

public GuildDeleteEvent(@NotNull DiscordJar bot, long sequence, @NotNull JSONObject data) {
super(bot, sequence, data);
guild = UnavailableGuild.decompile(getJson().getJSONObject("d"), getBot());
}

/**
* Returns the {@link UnavailableGuild} that was deleted.
* @return {@link UnavailableGuild} object.
*/
public UnavailableGuild getGuild() {
return guild;
}

/**
* Returns true if the current user was removed from the guild.
*/
public boolean wasRemoved() {
return !getJson().getJSONObject("d").has("unavailable") || !getJson().getJSONObject("d").getBoolean("unavailable");
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.seailz.discordjar.events.model.guild;

import com.seailz.discordjar.DiscordJar;
import com.seailz.discordjar.events.model.Event;
import com.seailz.discordjar.model.guild.Guild;
import org.jetbrains.annotations.NotNull;
import org.json.JSONObject;

public class GuildUpdateEvent extends Event {
public GuildUpdateEvent(@NotNull DiscordJar bot, long sequence, @NotNull JSONObject data) {
super(bot, sequence, data);
}

@NotNull
public Guild getGuild() {
return Guild.decompile(getJson().getJSONObject("d"), getBot());
}
}
Loading

0 comments on commit 6631f85

Please sign in to comment.