Skip to content

Commit

Permalink
feat(events): added message update & delete events
Browse files Browse the repository at this point in the history
  • Loading branch information
seailz committed Apr 8, 2024
1 parent a4a7f32 commit 3236466
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 0 deletions.
Expand Up @@ -25,6 +25,8 @@
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.MessageDeleteEvent;
import com.seailz.discordjar.events.model.message.MessageUpdateEvent;
import com.seailz.discordjar.events.model.message.TypingStartEvent;
import org.jetbrains.annotations.NotNull;

Expand Down Expand Up @@ -71,6 +73,12 @@ public void onGatewayResume(@NotNull GatewayResumedEvent event) {
public void onMessageReceived(@NotNull MessageCreateEvent event) {
}

public void onMessageUpdate(@NotNull MessageUpdateEvent event) {
}

public void onMessageDelete(@NotNull MessageDeleteEvent event) {
}

public void onTypingStart(@NotNull TypingStartEvent event) {
}

Expand Down
@@ -0,0 +1,66 @@
package com.seailz.discordjar.events.model.message;

import com.seailz.discordjar.DiscordJar;
import com.seailz.discordjar.model.guild.Guild;
import com.seailz.discordjar.utils.rest.DiscordRequest;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.json.JSONObject;

/**
* This event will fire when a message is deleted.
* You can listen for this event by extending the {@link com.seailz.discordjar.events.DiscordListener} class
* and overriding the {@link com.seailz.discordjar.events.DiscordListener#onMessageDelete(MessageDeleteEvent)} method.
*
* @author Seailz
* @see com.seailz.discordjar.events.DiscordListener
* @since 1.0
*/
public class MessageDeleteEvent extends MessageEvent {

/**
* Creates a new MessageDeleteEvent
*
* @param bot The current bot instance
* @param sequence The sequence number of the event
* @param data The data of the event
*/
public MessageDeleteEvent(DiscordJar bot, long sequence, JSONObject data) {
super(bot, sequence, data);
}

/**
* Returns the ID of the message that was deleted
*/
@NotNull
public String getMessageId() {
return getJson().getJSONObject("d").getString("id");
}

@NotNull
public String getChannelId() {
return getJson().getJSONObject("d").getString("channel_id");
}

@Nullable
public String getGuildId() {
if (!getJson().getJSONObject("d").has("guild_id") || getJson().getJSONObject("d").isNull("guild_id")) return null;
return getJson().getJSONObject("d").getString("guild_id");
}

/**
* The {@link Guild} the message was sent in
* This will return null if the message was sent in a DM.
*
* @return A {@link Guild} object
*/
@Nullable
public Guild getGuild() {
if (!getJson().getJSONObject("d").has("guild_id") || getJson().getJSONObject("d").isNull("guild_id")) return null;
try {
return getBot().getGuildCache().getById((getJson().getJSONObject("d").getString("guild_id")));
} catch (DiscordRequest.UnhandledDiscordAPIErrorException e) {
throw new DiscordRequest.DiscordAPIErrorException(e);
}
}
}
@@ -0,0 +1,57 @@
package com.seailz.discordjar.events.model.message;

import com.seailz.discordjar.DiscordJar;
import com.seailz.discordjar.model.guild.Guild;
import com.seailz.discordjar.model.message.Message;
import com.seailz.discordjar.utils.rest.DiscordRequest;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.json.JSONObject;

/**
* This event will fire when a message is edited.
* You can listen for this event by extending the {@link com.seailz.discordjar.events.DiscordListener} class
* and overriding the {@link com.seailz.discordjar.events.DiscordListener#onMessageUpdate(MessageUpdateEvent)} method.
*
* @author Seailz
* @see com.seailz.discordjar.events.DiscordListener
* @since 1.0
*/
public class MessageUpdateEvent extends MessageEvent {

/**
* Creates a new MessageEditEvent
*
* @param bot The current bot instance
* @param sequence The sequence number of the event
* @param data The data of the event
*/
public MessageUpdateEvent(DiscordJar bot, long sequence, JSONObject data) {
super(bot, sequence, data);
}

/**
* Returns the message that was edited
*/
@NotNull
public Message getMessage() {
return Message.decompile(getJson().getJSONObject("d"), getBot());
}

/**
* The {@link Guild} the message was sent in
* This will return null if the message was sent in a DM.
*
* @return A {@link Guild} object
*/
@Nullable
public Guild getGuild() {
if (!getJson().getJSONObject("d").has("guild_id") || getJson().getJSONObject("d").isNull("guild_id")) return null;
try {
return getBot().getGuildCache().getById((getJson().getJSONObject("d").getString("guild_id")));
} catch (DiscordRequest.UnhandledDiscordAPIErrorException e) {
throw new DiscordRequest.DiscordAPIErrorException(e);
}
}
}

Expand Up @@ -30,6 +30,8 @@
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.MessageDeleteEvent;
import com.seailz.discordjar.events.model.message.MessageUpdateEvent;
import com.seailz.discordjar.events.model.message.TypingStartEvent;
import com.seailz.discordjar.gateway.Gateway;
import com.seailz.discordjar.command.CommandType;
Expand Down Expand Up @@ -305,6 +307,8 @@ public enum DispatchedEvents {

/* MESSAGES */
MESSAGE_CREATE((p, d, g) -> MessageCreateEvent.class),
MESSAGE_UPDATE((p, d, g) -> MessageUpdateEvent.class),
MESSAGE_DELETE((p, d, g) -> MessageDeleteEvent.class),
// TODO: other message events

// TODO: Presence update
Expand Down

0 comments on commit 3236466

Please sign in to comment.