Skip to content

Commit

Permalink
Add SoundboardSoundManager
Browse files Browse the repository at this point in the history
  • Loading branch information
freya022 committed Oct 16, 2024
1 parent ab48530 commit ef28ed6
Show file tree
Hide file tree
Showing 4 changed files with 319 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -62,4 +63,8 @@ default String getUrl()
@Nonnull
@CheckReturnValue
RestAction<Void> delete();

@Nonnull
@CheckReturnValue
SoundboardSoundManager getManager();
}
154 changes: 154 additions & 0 deletions src/main/java/net/dv8tion/jda/api/managers/SoundboardSoundManager.java
Original file line number Diff line number Diff line change
@@ -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}.
*
* <p><b>Example</b>
* <pre>{@code
* manager.setVolume(0.33)
* .setEmoji(null)
* .queue();
* manager.reset(SoundboardSoundManager.VOLUME | SoundboardSoundManager.EMOJI)
* .setVolume("1")
* .setEmoji(Color.RED)
* .queue();
* }</pre>
*
* @see SoundboardSound#getManager()
*/
public interface SoundboardSoundManager extends Manager<SoundboardSoundManager>
{
/** 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.
* <br>Example: {@code manager.reset(SoundboardSoundManager.VOLUME | SoundboardSoundManager.EMOJI);}
*
* <p><b>Flag Constants:</b>
* <ul>
* <li>{@link #NAME}</li>
* <li>{@link #VOLUME}</li>
* <li>{@link #EMOJI}</li>
* </ul>
*
* @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.
* <br>Example: {@code manager.reset(SoundboardSoundManager.VOLUME | SoundboardSoundManager.EMOJI);}
*
* <p><b>Flag Constants:</b>
* <ul>
* <li>{@link #NAME}</li>
* <li>{@link #VOLUME}</li>
* <li>{@link #EMOJI}</li>
* </ul>
*
* @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 <b><u>name</u></b> of the selected {@link SoundboardSound}.
*
* <p>A role name <b>must not</b> 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 <b><u>volume</u></b> 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 <b><u>emoji</u></b> 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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -138,6 +140,15 @@ public RestAction<Void> 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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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<SoundboardSoundManager> 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);
}
}

0 comments on commit ef28ed6

Please sign in to comment.