Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check mute/deafen permissions using the channel #2781

Merged
merged 1 commit into from
Dec 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Check mute/deafen permissions using the channel
Prevents false missing permission exceptions
at the cost of missing exceptions
if the member or the voice state is absent
freya022 committed Dec 24, 2024
commit f083b092a3033c5146e3f3b14dbabe8b26256a97
6 changes: 4 additions & 2 deletions src/main/java/net/dv8tion/jda/api/entities/Guild.java
Original file line number Diff line number Diff line change
@@ -3985,7 +3985,8 @@ default AuditableRestAction<Void> timeoutFor(@Nonnull UserSnowflake user, @Nonnu
* Whether this {@link net.dv8tion.jda.api.entities.Member Member} should be deafened or undeafened.
*
* @throws net.dv8tion.jda.api.exceptions.InsufficientPermissionException
* If the logged in account does not have the {@link net.dv8tion.jda.api.Permission#VOICE_DEAF_OTHERS} permission.
* If the logged in account does not have the {@link net.dv8tion.jda.api.Permission#VOICE_DEAF_OTHERS} permission
* in the given channel.
* @throws IllegalArgumentException
* If the provided user is null.
* @throws java.lang.IllegalStateException
@@ -4024,7 +4025,8 @@ default AuditableRestAction<Void> timeoutFor(@Nonnull UserSnowflake user, @Nonnu
* Whether this {@link net.dv8tion.jda.api.entities.Member Member} should be muted or unmuted.
*
* @throws net.dv8tion.jda.api.exceptions.InsufficientPermissionException
* If the logged in account does not have the {@link net.dv8tion.jda.api.Permission#VOICE_DEAF_OTHERS} permission.
* If the logged in account does not have the {@link net.dv8tion.jda.api.Permission#VOICE_DEAF_OTHERS} permission
* in the given channel.
* @throws java.lang.IllegalArgumentException
* If the provided user is null.
* @throws java.lang.IllegalStateException
12 changes: 8 additions & 4 deletions src/main/java/net/dv8tion/jda/internal/entities/GuildImpl.java
Original file line number Diff line number Diff line change
@@ -32,6 +32,7 @@
import net.dv8tion.jda.api.entities.channel.concrete.*;
import net.dv8tion.jda.api.entities.channel.middleman.AudioChannel;
import net.dv8tion.jda.api.entities.channel.middleman.GuildChannel;
import net.dv8tion.jda.api.entities.channel.unions.AudioChannelUnion;
import net.dv8tion.jda.api.entities.channel.unions.DefaultGuildChannelUnion;
import net.dv8tion.jda.api.entities.emoji.CustomEmoji;
import net.dv8tion.jda.api.entities.emoji.RichCustomEmoji;
@@ -64,6 +65,7 @@
import net.dv8tion.jda.api.utils.data.DataObject;
import net.dv8tion.jda.internal.JDAImpl;
import net.dv8tion.jda.internal.entities.automod.AutoModRuleImpl;
import net.dv8tion.jda.internal.entities.channel.mixin.middleman.GuildChannelMixin;
import net.dv8tion.jda.internal.handle.EventCache;
import net.dv8tion.jda.internal.interactions.CommandDataImpl;
import net.dv8tion.jda.internal.interactions.command.CommandImpl;
@@ -1641,18 +1643,19 @@ private AuditableRestAction<Void> timeoutUntilById0(@Nonnull String userId, @Nul
public AuditableRestAction<Void> deafen(@Nonnull UserSnowflake user, boolean deafen)
{
Checks.notNull(user, "User");
checkPermission(Permission.VOICE_DEAF_OTHERS);

Member member = resolveMember(user);
if (member != null)
{
GuildVoiceState voiceState = member.getVoiceState();
if (voiceState != null)
{
if (voiceState.getChannel() == null)
final AudioChannelUnion channel = voiceState.getChannel();
if (channel == null)
throw new IllegalStateException("Can only deafen members who are currently in a voice channel");
if (voiceState.isGuildDeafened() == deafen)
return new CompletedRestAction<>(getJDA(), null);
((GuildChannelMixin<?>) channel).checkPermission(Permission.VOICE_DEAF_OTHERS);
}
}

@@ -1666,18 +1669,19 @@ public AuditableRestAction<Void> deafen(@Nonnull UserSnowflake user, boolean dea
public AuditableRestAction<Void> mute(@Nonnull UserSnowflake user, boolean mute)
{
Checks.notNull(user, "User");
checkPermission(Permission.VOICE_MUTE_OTHERS);

Member member = resolveMember(user);
if (member != null)
{
GuildVoiceState voiceState = member.getVoiceState();
if (voiceState != null)
{
if (voiceState.getChannel() == null)
final AudioChannelUnion channel = voiceState.getChannel();
if (channel == null)
throw new IllegalStateException("Can only mute members who are currently in a voice channel");
if (voiceState.isGuildMuted() == mute && (mute || !voiceState.isSuppressed()))
return new CompletedRestAction<>(getJDA(), null);
((GuildChannelMixin<?>) channel).checkPermission(Permission.VOICE_MUTE_OTHERS);
}
}