From bf699f401443edcf8133e0a94ef50747f5707850 Mon Sep 17 00:00:00 2001 From: freya02 <41875020+freya022@users.noreply.github.com> Date: Thu, 17 Oct 2024 00:57:56 +0200 Subject: [PATCH] Add VoiceChannelEffectSendEvent --- .../channel/concrete/VoiceChannel.java | 108 ++++++++++++++++++ .../channel/VoiceChannelEffectSendEvent.java | 49 ++++++++ .../jda/api/hooks/ListenerAdapter.java | 4 + .../handle/VoiceChannelEffectSendHandler.java | 79 +++++++++++++ .../internal/requests/WebSocketClient.java | 1 + 5 files changed, 241 insertions(+) create mode 100644 src/main/java/net/dv8tion/jda/api/events/channel/VoiceChannelEffectSendEvent.java create mode 100644 src/main/java/net/dv8tion/jda/internal/handle/VoiceChannelEffectSendHandler.java diff --git a/src/main/java/net/dv8tion/jda/api/entities/channel/concrete/VoiceChannel.java b/src/main/java/net/dv8tion/jda/api/entities/channel/concrete/VoiceChannel.java index 00a5081891..12f5235494 100644 --- a/src/main/java/net/dv8tion/jda/api/entities/channel/concrete/VoiceChannel.java +++ b/src/main/java/net/dv8tion/jda/api/entities/channel/concrete/VoiceChannel.java @@ -17,6 +17,9 @@ import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.entities.Guild; +import net.dv8tion.jda.api.entities.ISnowflake; +import net.dv8tion.jda.api.entities.SoundboardSound; +import net.dv8tion.jda.api.entities.User; import net.dv8tion.jda.api.entities.channel.attribute.IAgeRestrictedChannel; import net.dv8tion.jda.api.entities.channel.attribute.ISlowmodeChannel; import net.dv8tion.jda.api.entities.channel.attribute.IVoiceStatusChannel; @@ -25,11 +28,13 @@ import net.dv8tion.jda.api.entities.channel.middleman.GuildChannel; import net.dv8tion.jda.api.entities.channel.middleman.GuildMessageChannel; import net.dv8tion.jda.api.entities.channel.middleman.StandardGuildChannel; +import net.dv8tion.jda.api.entities.emoji.EmojiUnion; import net.dv8tion.jda.api.managers.channel.concrete.VoiceChannelManager; import net.dv8tion.jda.api.requests.restaction.ChannelAction; import javax.annotation.CheckReturnValue; import javax.annotation.Nonnull; +import javax.annotation.Nullable; /** * Represents a Discord Voice GuildChannel. @@ -73,4 +78,107 @@ default ChannelAction createCopy() @Override @CheckReturnValue VoiceChannelManager getManager(); + + class Effect + { + private final VoiceChannel channel; + private final User user; + private final EmojiUnion emoji; + private final Animation animation; + private final SoundboardSound soundboardSound; + + public Effect(VoiceChannel channel, User user, EmojiUnion emoji, Animation animation, SoundboardSound soundboardSound) + { + this.channel = channel; + this.user = user; + this.emoji = emoji; + this.animation = animation; + this.soundboardSound = soundboardSound; + } + + @Nonnull + public VoiceChannel getChannel() + { + return channel; + } + + @Nonnull + public User getUser() + { + return user; + } + + @Nullable + public EmojiUnion getEmoji() + { + return emoji; + } + + @Nullable + public Animation getAnimation() + { + return animation; + } + + @Nullable + public SoundboardSound getSoundboardSound() + { + return soundboardSound; + } + + public static class Animation implements ISnowflake + { + public Animation(long id, Type type) + { + this.id = id; + this.type = type; + } + + private final long id; + private final Type type; + + @Override + public long getIdLong() + { + return id; + } + + @Nonnull + public Type getType() + { + return type; + } + + public enum Type + { + UNKNOWN(-1), + PREMIUM(0), + BASIC(1); + + private final int value; + + Type(int value) + { + this.value = value; + } + + public int getValue() + { + return value; + } + + @Nonnull + public static Type fromValue(int value) + { + for (Type type : values()) + { + if (type.value == value) + return type; + } + + return UNKNOWN; + } + } + } + } } diff --git a/src/main/java/net/dv8tion/jda/api/events/channel/VoiceChannelEffectSendEvent.java b/src/main/java/net/dv8tion/jda/api/events/channel/VoiceChannelEffectSendEvent.java new file mode 100644 index 0000000000..eda1703308 --- /dev/null +++ b/src/main/java/net/dv8tion/jda/api/events/channel/VoiceChannelEffectSendEvent.java @@ -0,0 +1,49 @@ +/* + * Copyright 2015 Austin Keener, Michael Ritter, Florian Spieß, and the JDA contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.dv8tion.jda.api.events.channel; + +import net.dv8tion.jda.api.JDA; +import net.dv8tion.jda.api.entities.channel.Channel; +import net.dv8tion.jda.api.entities.channel.concrete.VoiceChannel; + +import javax.annotation.Nonnull; + +/** + * Indicates that a {@link VoiceChannel.Effect voice channel effect} was sent in a {@link VoiceChannel}. + */ +public class VoiceChannelEffectSendEvent extends GenericChannelEvent +{ + private final VoiceChannel.Effect effect; + + public VoiceChannelEffectSendEvent(@Nonnull JDA api, long responseNumber, Channel channel, VoiceChannel.Effect effect) + { + super(api, responseNumber, channel); + this.effect = effect; + } + + @Nonnull + public VoiceChannel getVoiceChannel() + { + return getChannel().asVoiceChannel(); + } + + @Nonnull + public VoiceChannel.Effect getEffect() + { + return effect; + } +} diff --git a/src/main/java/net/dv8tion/jda/api/hooks/ListenerAdapter.java b/src/main/java/net/dv8tion/jda/api/hooks/ListenerAdapter.java index 862d5d8566..d35e13540d 100644 --- a/src/main/java/net/dv8tion/jda/api/hooks/ListenerAdapter.java +++ b/src/main/java/net/dv8tion/jda/api/hooks/ListenerAdapter.java @@ -20,6 +20,7 @@ import net.dv8tion.jda.api.events.channel.ChannelCreateEvent; import net.dv8tion.jda.api.events.channel.ChannelDeleteEvent; import net.dv8tion.jda.api.events.channel.GenericChannelEvent; +import net.dv8tion.jda.api.events.channel.VoiceChannelEffectSendEvent; import net.dv8tion.jda.api.events.channel.forum.ForumTagAddEvent; import net.dv8tion.jda.api.events.channel.forum.ForumTagRemoveEvent; import net.dv8tion.jda.api.events.channel.forum.GenericForumTagEvent; @@ -385,6 +386,9 @@ public void onSoundboardSoundUpdateName(@Nonnull SoundboardSoundUpdateNameEvent public void onSoundboardSoundUpdateVolume(@Nonnull SoundboardSoundUpdateVolumeEvent event) {} public void onSoundboardSoundUpdateEmoji(@Nonnull SoundboardSoundUpdateEmojiEvent event) {} + // Voice channel effect events + public void onVoiceChannelEffectSend(@Nonnull VoiceChannelEffectSendEvent event) {} + // Entitlement events public void onEntitlementCreate(@Nonnull EntitlementCreateEvent event) {} public void onEntitlementUpdate(@Nonnull EntitlementUpdateEvent event) {} diff --git a/src/main/java/net/dv8tion/jda/internal/handle/VoiceChannelEffectSendHandler.java b/src/main/java/net/dv8tion/jda/internal/handle/VoiceChannelEffectSendHandler.java new file mode 100644 index 0000000000..f254c4f37f --- /dev/null +++ b/src/main/java/net/dv8tion/jda/internal/handle/VoiceChannelEffectSendHandler.java @@ -0,0 +1,79 @@ +/* + * Copyright 2015 Austin Keener, Michael Ritter, Florian Spieß, and the JDA contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.dv8tion.jda.internal.handle; + +import net.dv8tion.jda.api.entities.SoundboardSound; +import net.dv8tion.jda.api.entities.User; +import net.dv8tion.jda.api.entities.channel.concrete.VoiceChannel; +import net.dv8tion.jda.api.entities.channel.concrete.VoiceChannel.Effect; +import net.dv8tion.jda.api.entities.emoji.EmojiUnion; +import net.dv8tion.jda.api.events.channel.VoiceChannelEffectSendEvent; +import net.dv8tion.jda.api.utils.data.DataObject; +import net.dv8tion.jda.internal.JDAImpl; +import net.dv8tion.jda.internal.entities.EntityBuilder; +import net.dv8tion.jda.internal.entities.GuildImpl; + +public class VoiceChannelEffectSendHandler extends SocketHandler +{ + public VoiceChannelEffectSendHandler(JDAImpl api) + { + super(api); + } + + @Override + protected Long handleInternally(DataObject content) + { + final long guildId = content.getLong("guild_id"); + if (getJDA().getGuildSetupController().isLocked(guildId)) + return guildId; + + GuildImpl guild = (GuildImpl) getJDA().getGuildById(guildId); + if (guild == null) + { + getJDA().getEventCache().cache(EventCache.Type.GUILD, guildId, responseNumber, allContent, this::handle); + return null; + } + + final long channelId = content.getLong("channel_id"); + VoiceChannel channel = guild.getVoiceChannelById(channelId); + if (channel == null) + { + getJDA().getEventCache().cache(EventCache.Type.CHANNEL, channelId, responseNumber, allContent, this::handle); + return null; + } + + User user = api.getUserById(content.getString("user_id")); + EmojiUnion emoji = content.optObject("emoji").map(EntityBuilder::createEmoji).orElse(null); + Effect.Animation animation = content.opt("animation_type") + .map(rawAnimationType -> + { + long animationId = content.getLong("animation_id"); + Effect.Animation.Type type = Effect.Animation.Type.fromValue(Integer.parseInt(rawAnimationType.toString())); + return new Effect.Animation(animationId, type); + }) + .orElse(null); + SoundboardSound soundboardSound = content.opt("sound_id") + .map(soundId -> guild.getSoundboardSoundById(soundId.toString())) + .orElse(null); + + Effect effect = new Effect(channel, user, emoji, animation, soundboardSound); + + api.handleEvent(new VoiceChannelEffectSendEvent(api, responseNumber, channel, effect)); + + return null; + } +} diff --git a/src/main/java/net/dv8tion/jda/internal/requests/WebSocketClient.java b/src/main/java/net/dv8tion/jda/internal/requests/WebSocketClient.java index 7e223ebbe6..e518644a0f 100644 --- a/src/main/java/net/dv8tion/jda/internal/requests/WebSocketClient.java +++ b/src/main/java/net/dv8tion/jda/internal/requests/WebSocketClient.java @@ -1401,6 +1401,7 @@ protected void setupHandlers() handlers.put("GUILD_SOUNDBOARD_SOUND_UPDATE", new GuildSoundboardSoundUpdateHandler(api)); handlers.put("GUILD_SOUNDBOARD_SOUNDS_UPDATE", new GuildSoundboardSoundsUpdateHandler(api)); handlers.put("GUILD_SOUNDBOARD_SOUND_DELETE", new GuildSoundboardSoundDeleteHandler(api)); + handlers.put("VOICE_CHANNEL_EFFECT_SEND", new VoiceChannelEffectSendHandler(api)); handlers.put("GUILD_UPDATE", new GuildUpdateHandler(api)); handlers.put("INTERACTION_CREATE", new InteractionCreateHandler(api)); handlers.put("INVITE_CREATE", new InviteCreateHandler(api));