-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(events): added message update & delete events
- Loading branch information
Showing
4 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
src/main/java/com/seailz/discordjar/events/model/message/MessageDeleteEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
src/main/java/com/seailz/discordjar/events/model/message/MessageUpdateEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters