diff --git a/src/main/java/net/dv8tion/jda/api/entities/SoundboardSound.java b/src/main/java/net/dv8tion/jda/api/entities/SoundboardSound.java index cf4d67af5c..425d9a022a 100644 --- a/src/main/java/net/dv8tion/jda/api/entities/SoundboardSound.java +++ b/src/main/java/net/dv8tion/jda/api/entities/SoundboardSound.java @@ -19,6 +19,7 @@ import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.entities.channel.middleman.AudioChannel; import net.dv8tion.jda.api.entities.emoji.EmojiUnion; +import net.dv8tion.jda.api.managers.SoundboardSoundManager; import net.dv8tion.jda.api.requests.RestAction; import javax.annotation.CheckReturnValue; @@ -62,4 +63,8 @@ default String getUrl() @Nonnull @CheckReturnValue RestAction delete(); + + @Nonnull + @CheckReturnValue + SoundboardSoundManager getManager(); } diff --git a/src/main/java/net/dv8tion/jda/api/managers/SoundboardSoundManager.java b/src/main/java/net/dv8tion/jda/api/managers/SoundboardSoundManager.java new file mode 100644 index 0000000000..87a6dfccd7 --- /dev/null +++ b/src/main/java/net/dv8tion/jda/api/managers/SoundboardSoundManager.java @@ -0,0 +1,154 @@ +/* + * 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.managers; + +import net.dv8tion.jda.api.entities.Guild; +import net.dv8tion.jda.api.entities.SoundboardSound; +import net.dv8tion.jda.api.entities.emoji.Emoji; + +import javax.annotation.CheckReturnValue; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + +/** + * Manager providing functionality to update one or more fields for a {@link SoundboardSound}. + * + *

Example + *

{@code
+ * manager.setVolume(0.33)
+ *        .setEmoji(null)
+ *        .queue();
+ * manager.reset(SoundboardSoundManager.VOLUME | SoundboardSoundManager.EMOJI)
+ *        .setVolume("1")
+ *        .setEmoji(Color.RED)
+ *        .queue();
+ * }
+ * + * @see SoundboardSound#getManager() + */ +public interface SoundboardSoundManager extends Manager +{ + /** Used to reset the name field */ + long NAME = 1; + /** Used to reset the volume field */ + long VOLUME = 1 << 1; + /** Used to reset the emoji field */ + long EMOJI = 1 << 2; + + /** + * Resets the fields specified by the provided bit-flag pattern. + * You can specify a combination by using a bitwise OR concat of the flag constants. + *
Example: {@code manager.reset(SoundboardSoundManager.VOLUME | SoundboardSoundManager.EMOJI);} + * + *

Flag Constants: + *

+ * + * @param fields + * Integer value containing the flags to reset. + * + * @return SoundboardSoundManager for chaining convenience + */ + @Nonnull + @Override + @CheckReturnValue + SoundboardSoundManager reset(long fields); + + /** + * Resets the fields specified by the provided bit-flag patterns. + *
Example: {@code manager.reset(SoundboardSoundManager.VOLUME | SoundboardSoundManager.EMOJI);} + * + *

Flag Constants: + *

+ * + * @param fields + * Integer values containing the flags to reset. + * + * @return SoundboardSoundManager for chaining convenience + */ + @Nonnull + @Override + @CheckReturnValue + SoundboardSoundManager reset(long... fields); + + /** + * The target {@link SoundboardSound} for this manager + * + * @return The target SoundboardSound + */ + @Nonnull + SoundboardSound getSoundboardSound(); + + /** + * The {@link Guild} this Manager's {@link SoundboardSound} is in. + * + * @return The parent {@link Guild} + */ + @Nonnull + Guild getGuild(); + + /** + * Sets the name of the selected {@link SoundboardSound}. + * + *

A role name must not be {@code null} nor less than 2 characters or more than 32 characters long! + * + * @param name + * The new name for the selected {@link SoundboardSound} + * + * @throws IllegalArgumentException + * If the provided name is {@code null} or not between 2-32 characters long + * + * @return SoundboardSoundManager for chaining convenience + */ + @Nonnull + @CheckReturnValue + SoundboardSoundManager setName(@Nonnull String name); + + /** + * Sets the volume of the selected {@link SoundboardSound}. + * + * @param volume + * The new volume for the selected {@link SoundboardSound} + * + * @throws IllegalArgumentException + * If the provided volume is not between 0-1 + * + * @return SoundboardSoundManager for chaining convenience + */ + @Nonnull + @CheckReturnValue + SoundboardSoundManager setVolume(double volume); + + /** + * Sets the emoji of the selected {@link SoundboardSound}. + * + * @param emoji + * The new emoji for the selected {@link SoundboardSound} + * + * @return SoundboardSoundManager for chaining convenience + */ + @Nonnull + @CheckReturnValue + SoundboardSoundManager setEmoji(@Nullable Emoji emoji); +} diff --git a/src/main/java/net/dv8tion/jda/internal/entities/SoundboardSoundImpl.java b/src/main/java/net/dv8tion/jda/internal/entities/SoundboardSoundImpl.java index b78f87b8ee..d44647ec1d 100644 --- a/src/main/java/net/dv8tion/jda/internal/entities/SoundboardSoundImpl.java +++ b/src/main/java/net/dv8tion/jda/internal/entities/SoundboardSoundImpl.java @@ -25,9 +25,11 @@ import net.dv8tion.jda.api.entities.channel.middleman.AudioChannel; import net.dv8tion.jda.api.entities.emoji.EmojiUnion; import net.dv8tion.jda.api.exceptions.InsufficientPermissionException; +import net.dv8tion.jda.api.managers.SoundboardSoundManager; import net.dv8tion.jda.api.requests.RestAction; import net.dv8tion.jda.api.requests.Route; import net.dv8tion.jda.api.utils.data.DataObject; +import net.dv8tion.jda.internal.managers.SoundboardSoundManagerImpl; import net.dv8tion.jda.internal.requests.RestActionImpl; import net.dv8tion.jda.internal.requests.restaction.AuditableRestActionImpl; import net.dv8tion.jda.internal.utils.Checks; @@ -138,6 +140,15 @@ public RestAction delete() return new AuditableRestActionImpl<>(api, route); } + @Nonnull + @Override + public SoundboardSoundManager getManager() { + Checks.check(getGuild() != null, "Cannot delete default soundboard sounds"); + checkEditPermissions(); + + return new SoundboardSoundManagerImpl(this); + } + private void checkEditPermissions() { final Member selfMember = guild.getSelfMember(); diff --git a/src/main/java/net/dv8tion/jda/internal/managers/SoundboardSoundManagerImpl.java b/src/main/java/net/dv8tion/jda/internal/managers/SoundboardSoundManagerImpl.java new file mode 100644 index 0000000000..518679526a --- /dev/null +++ b/src/main/java/net/dv8tion/jda/internal/managers/SoundboardSoundManagerImpl.java @@ -0,0 +1,149 @@ +/* + * 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.managers; + +import net.dv8tion.jda.api.entities.Guild; +import net.dv8tion.jda.api.entities.SoundboardSound; +import net.dv8tion.jda.api.entities.emoji.CustomEmoji; +import net.dv8tion.jda.api.entities.emoji.Emoji; +import net.dv8tion.jda.api.managers.SoundboardSoundManager; +import net.dv8tion.jda.api.requests.Route; +import net.dv8tion.jda.api.utils.data.DataObject; +import okhttp3.RequestBody; + +import javax.annotation.CheckReturnValue; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + +public class SoundboardSoundManagerImpl extends ManagerBase implements SoundboardSoundManager +{ + private SoundboardSound soundboardSound; + private String name; + private double volume; + private Emoji emoji; + + public SoundboardSoundManagerImpl(SoundboardSound soundboardSound) + { + super(soundboardSound.getJDA(), Route.SoundboardSounds.MODIFY_GUILD_SOUNDBOARD_SOUNDS.compile(soundboardSound.getGuild().getId(), soundboardSound.getId())); + this.soundboardSound = soundboardSound; + } + + @Nonnull + @Override + @SuppressWarnings("DataFlowIssue") + public Guild getGuild() + { + return soundboardSound.getGuild(); + } + + @Nonnull + @Override + public SoundboardSound getSoundboardSound() + { + final SoundboardSound soundboardSound = getGuild().getSoundboardSoundById(this.soundboardSound.getId()); + if (soundboardSound != null) + this.soundboardSound = soundboardSound; + return this.soundboardSound; + } + + @Nonnull + @Override + @CheckReturnValue + public SoundboardSoundManagerImpl reset(long fields) + { + super.reset(fields); + if ((fields & NAME) == NAME) + this.name = null; + if ((fields & VOLUME) == VOLUME) + this.volume = 1; + if ((fields & EMOJI) == EMOJI) + this.emoji = null; + return this; + } + + @Nonnull + @Override + @CheckReturnValue + public SoundboardSoundManagerImpl reset(long... fields) + { + super.reset(fields); + return this; + } + + @Nonnull + @Override + @CheckReturnValue + public SoundboardSoundManagerImpl reset() + { + super.reset(); + this.name = null; + this.volume = 1; + this.emoji = null; + return this; + } + + @Nonnull + @Override + public SoundboardSoundManagerImpl setName(@Nonnull String name) + { + this.name = name; + set |= NAME; + return this; + } + + @Nonnull + @Override + public SoundboardSoundManagerImpl setVolume(double volume) + { + this.volume = volume; + set |= VOLUME; + return this; + } + + @Nonnull + @Override + public SoundboardSoundManagerImpl setEmoji(@Nullable Emoji emoji) + { + this.emoji = emoji; + set |= EMOJI; + return this; + } + + @Override + protected RequestBody finalizeData() + { + DataObject object = DataObject.empty().put("name", getSoundboardSound().getName()); + if (shouldUpdate(NAME)) + object.put("name", name); + if (shouldUpdate(VOLUME)) + object.put("volume", volume); + if (shouldUpdate(EMOJI)) + { + if (emoji instanceof CustomEmoji) + object.put("emoji_id", ((CustomEmoji) emoji).getId()); + else if (emoji != null) + object.put("emoji_name", emoji.getName()); + else + { + object.put("emoji_id", null); + object.put("emoji_name", null); + } + } + reset(); + return getRequestBody(object); + } +}