Skip to content

Commit

Permalink
Check mute/deafen permissions using the channel
Browse files Browse the repository at this point in the history
Prevents false missing permission exceptions
at the cost of missing exceptions
if the member or the voice state is absent
  • Loading branch information
freya022 committed Dec 24, 2024
1 parent 5ec2d8e commit f083b09
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
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
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
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
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}

Expand All @@ -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);
}
}

Expand Down

0 comments on commit f083b09

Please sign in to comment.