From 15b593a74dcc02f23494ef0383dbdb42980e65a1 Mon Sep 17 00:00:00 2001 From: caneleex Date: Fri, 3 Dec 2021 18:26:04 +0100 Subject: [PATCH] Add GenericChannelEvent#isFromGuild and GenericChannelEvent#getGuild (#1935) --- .../events/channel/GenericChannelEvent.java | 35 +++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/src/main/java/net/dv8tion/jda/api/events/channel/GenericChannelEvent.java b/src/main/java/net/dv8tion/jda/api/events/channel/GenericChannelEvent.java index f2a064ef3a..e2e72d33a0 100644 --- a/src/main/java/net/dv8tion/jda/api/events/channel/GenericChannelEvent.java +++ b/src/main/java/net/dv8tion/jda/api/events/channel/GenericChannelEvent.java @@ -17,8 +17,7 @@ package net.dv8tion.jda.api.events.channel; import net.dv8tion.jda.api.JDA; -import net.dv8tion.jda.api.entities.Channel; -import net.dv8tion.jda.api.entities.ChannelType; +import net.dv8tion.jda.api.entities.*; import net.dv8tion.jda.api.events.Event; import javax.annotation.Nonnull; @@ -35,6 +34,17 @@ public GenericChannelEvent(@Nonnull JDA api, long responseNumber, Channel channe this.channel = channel; } + /** + * Whether this channel event happened in a {@link net.dv8tion.jda.api.entities.Guild Guild}. + *
If this is {@code false} then {@link #getGuild()} will throw an {@link java.lang.IllegalStateException}. + * + * @return True, if {@link #getChannelType()}.{@link ChannelType#isGuild() isGuild()} is true. + */ + public boolean isFromGuild() + { + return getChannelType().isGuild(); + } + @Nonnull public ChannelType getChannelType() { @@ -52,5 +62,26 @@ public Channel getChannel() return this.channel; } + /** + * The {@link net.dv8tion.jda.api.entities.Guild Guild} in which this channel event happened. + *
If this channel event was not received in a {@link net.dv8tion.jda.api.entities.TextChannel TextChannel}, + * this will throw an {@link java.lang.IllegalStateException}. + * + * @throws java.lang.IllegalStateException + * If this channel event did not happen in a {@link net.dv8tion.jda.api.entities.GuildChannel}. + * + * @return The Guild in which this channel event happened + * + * @see #isFromType(ChannelType) + * @see #getChannelType() + */ + @Nonnull + public Guild getGuild() + { + if (!isFromGuild()) + throw new IllegalStateException("This channel event did not happen in a guild"); + return ((GuildChannel) channel).getGuild(); + } + //TODO-v5: Add getters for all channel types }