如何确定用户是不是加入/切换/离开了语音频道?

Posted

技术标签:

【中文标题】如何确定用户是不是加入/切换/离开了语音频道?【英文标题】:How to determine if a user joined/switched/left a voice channel?如何确定用户是否加入/切换/离开了语音频道? 【发布时间】:2021-06-05 16:11:12 【问题描述】:

我正在使用 Discord.Net 并观察多个语音通道。如果这些语音通道具有由机器人设置的静音状态(而不是通过权限),则该语音通道中的用户也应该被静音。

如您在此处所见,仅从语音频道中删除发言权限不会立即影响人们

https://support.discord.com/hc/en-us/community/posts/360052856033-Directly-affect-people-in-channels-on-permission-changes

如果他们离开它,他们应该取消静音。

所以这个包含所有必需的信息

public sealed class ObservedVoiceChannel

    public ulong VoiceChannelId  get; set; 
    public bool IsMuted  get; set; 
    // ... other information go here ...

而且我有一个服务来保存所有观察到的语音通道

public sealed class ObservedVoiceChannelsCache : Dictionary<ulong, ObservedVoiceChannel>


由于只有UserVoiceStateUpdated 事件,我想出了以下代码。

经过一些测试,我认为这段代码对我来说很好。虽然我知道使用“或”运算符可以提高 if 语句的可读性,但我会在解决最后一个问题后进行。

离开观察到的静音频道时,请查看评论

// 用户离开观察到静音的语音通道

用户不会被机器人取消静音。有时,当加入和离开的速度足够快时,处理程序会抛出异常

服务器响应错误 400:BadRequest

在 Discord.Net.Queue.RequestBucket.SendAsync(RestRequest 请求) 在 Discord.Net.Queue.RequestQueue.SendAsync(RestRequest 请求) 在 Discord.API.DiscordRestApiClient.SendInternalAsync(字符串方法, 字符串端点,RestRequest 请求)在 Discord.API.DiscordRestApiClient.SendJsonAsync(字符串方法,字符串 端点、对象负载、BucketId bucketId、ClientBucketType clientBucket,RequestOptions 选项)在 Discord.API.DiscordRestApiClient.ModifyGuildMemberAsync(UInt64 guildId, UInt64 userId, ModifyGuildMemberParams args, RequestOptions 选项)在 Discord.Rest.UserHelper.ModifyAsync(IGuildUser 用户, BaseDiscordClient 客户端,Action`1 func,RequestOptions 选项)在 ...OnUserVoiceStateUpdated(SocketUser socketUser, SocketVoiceState oldSocketVoiceState, SocketVoiceState newSocketVoiceState) 中 /.../UserVoiceStateUpdatedEventHandler.cs:第 52 行

这是我目前正在使用的代码

public sealed class UserVoiceStateUpdatedEventHandler

    private readonly ObservedVoiceChannelsCache _observedVoiceChannelsCache;
    
    public UserVoiceStateUpdatedEventHandler(ObservedVoiceChannelsCache observedVoiceChannelsCache)
    
        _observedVoiceChannelsCache = observedVoiceChannelsCache;
    
    
    public async Task OnUserVoiceStateUpdated(
        SocketUser socketUser, 
        SocketVoiceState oldSocketVoiceState,
        SocketVoiceState newSocketVoiceState)
    
        if (socketUser is SocketGuildUser socketGuildUser)
        
            bool userIsMuted = socketGuildUser.VoiceState?.IsMuted == true;
            bool userIsNotOffline = socketGuildUser.Status != UserStatus.Offline;
            
            // user left observed muted voice channel
            if (oldSocketVoiceState.VoiceChannel != null && 
                newSocketVoiceState.VoiceChannel == null &&
                _observedVoiceChannelsCache.TryGetValue(oldSocketVoiceState.VoiceChannel.Id, out ObservedVoiceChannel observedLeftVoiceChannel) &&
                observedLeftVoiceChannel.IsMuted &&
                userIsMuted &&
                userIsNotOffline
                )
            
                await SetUserMuteState(socketGuildUser, false);
            
            // user joined observed muted voice channel
            else if (oldSocketVoiceState.VoiceChannel == null && 
                     newSocketVoiceState.VoiceChannel != null &&
                     _observedVoiceChannelsCache.TryGetValue(newSocketVoiceState.VoiceChannel.Id, out ObservedVoiceChannel observedJoinedVoiceChannel) &&
                     observedJoinedVoiceChannel.IsMuted &&
                     !userIsMuted &&
                     userIsNotOffline)
            
                await SetUserMuteState(socketGuildUser, true);
            
            // user changed voice channels
            else if (oldSocketVoiceState.VoiceChannel != null && 
                     newSocketVoiceState.VoiceChannel != null &&
                     userIsNotOffline)
            
                bool oldVoiceChannelObserved = _observedVoiceChannelsCache.TryGetValue(
                    oldSocketVoiceState.VoiceChannel.Id, out ObservedVoiceChannel oldObservedVoiceChannel);
                
                bool newVoiceChannelObserved = _observedVoiceChannelsCache.TryGetValue(
                    newSocketVoiceState.VoiceChannel.Id, out ObservedVoiceChannel newObservedVoiceChannel);

                // user moved from observed muted voice channel to unobserved voice channel
                if (oldVoiceChannelObserved && 
                    !newVoiceChannelObserved &&
                    oldObservedVoiceChannel.IsMuted &&
                    userIsMuted)
                
                    await SetUserMuteState(socketGuildUser, false);
                
                // user moved from unobserved voice channel to observed muted voice channel
                else if (!oldVoiceChannelObserved && 
                         newVoiceChannelObserved &&
                         newObservedVoiceChannel.IsMuted &&
                         !userIsMuted)
                
                    await SetUserMuteState(socketGuildUser, true);
                
                // both voice channels are observed
                else if (oldVoiceChannelObserved && newVoiceChannelObserved)
                
                    // user moved from muted to unmuted voice channel
                    if (oldObservedVoiceChannel.IsMuted && 
                        !newObservedVoiceChannel.IsMuted &&
                        userIsMuted)
                    
                        await SetUserMuteState(socketGuildUser, false);
                    
                    // user moved from unmuted to muted voice channel
                    else if (!oldObservedVoiceChannel.IsMuted && 
                             newObservedVoiceChannel.IsMuted && 
                             !userIsMuted)
                    
                        await SetUserMuteState(socketGuildUser, true);
                    
                    // user moved from muted to muted voice channel
                    else if (oldObservedVoiceChannel.IsMuted && 
                             newObservedVoiceChannel.IsMuted && 
                             !userIsMuted)
                    
                        await SetUserMuteState(socketGuildUser, true);
                    
                
            
        
    

    private Task SetUserMuteState(SocketGuildUser socketGuildUser, bool muteUser)
        => socketGuildUser.ModifyAsync(guildUserProperties => guildUserProperties.Mute = muteUser);

我想知道如何将离开观察到的静音语音频道的用户取消静音。

我在这里发现了这一行

bool userIsMuted = socketGuildUser.VoiceState?.IsMuted == true;

离开语音通道后返回false,因为语音状态为空。所以似乎没有办法检查用户再次加入时是否会被静音。

【问题讨论】:

【参考方案1】:

您确定某人是否加入、移动或离开语音频道的方法是分别查看SocketVoiceState oldSocketVoiceStateSocketVoiceState newSocketVoiceState 参数的VoiceChannel 属性。 (oldSocketVoiceState.VoiceChannel -> newSocketVoiceState.VoiceChannel 以下示例):

进入未连接的新语音通道(null -> 通道 A) 在语音通道之间移动(通道 A -> 通道 B) 从语音通道断开连接(通道 B -> 空)

要将加入语音频道的某人静音,然后在他们断开连接后取消静音,您可以编写以下代码:

public async Task LogUserVoiceStateUpdatedAsync(SocketUser user, SocketVoiceState curVoiceState,
    SocketVoiceState nextVoiceState)

    if (user is not SocketGuildUser guildUser)
    
        // They aren't a guild user, so we can't do anything to them.
        return;
    
    
    // Note, you should make a method for the two switches below as in 
    // reality you're only changing one true/false flag depending on 
    // the voice states.
    
    // The user is leaving the voice channel.
    if (curVoiceState.VoiceChannel != null && nextVoiceState.VoiceChannel == null)
    
        // Unmute the user.
        try
        
            // Surround in try-catch in the event we lack permissions.
            await guildUser.ModifyAsync(x => x.Mute = false);
        
        catch (Exception e)
        
            // Will ALWAYS throw 400 bad request. I don't exactly know why, 
            // but it has to do with the modification being done after the user leaves the voice channel.
            
            // The warning can be safely be ignored.
            // _logger.LogWarning(e, $"Failed to unmute user in guild guildUser.Guild.Id.");
        
    
    else if (curVoiceState.VoiceChannel == null && nextVoiceState.VoiceChannel != null)
    
        // Mute the user.
        try
        
            // Surround in try-catch in the event we lack permissions.
            await guildUser.ModifyAsync(x => x.Mute = true);
        
        catch (Exception e)
        
            _logger.LogWarning(e, $"Failed to mute user in guild guildUser.Guild.Id.");
        
    

【讨论】:

取消静音离开用户总是抛出 400?如果是这样的话,那么我认为 OP 的代码应该没问题... @StageCodes 感谢您的回复,但如果用户切换语音通道怎么办?那么curVoiceState.VoiceChannel 不会为空。如果是这样的话,你的代码和我的有什么不同呢? @Question3r 是的,你是对的。我们的代码不同之处在于您的引用了缓存,而我的没有。您的代码使问题变得比需要的更复杂,我的代码更易于阅读并安全地使用户静音。使用您的代码,如果您按原样静音用户并且没有权限在服务器中这样做,您将遇到未处理的异常,我相信程序会崩溃。 @StageCodes 是的,问题是当用户离开语音频道时,我无法再取消静音。我也试过你和我的代码,都在苦苦挣扎 在我的测试中,我从语音通道断开后立即取消静音,并在我重新加入时再次静音。

以上是关于如何确定用户是不是加入/切换/离开了语音频道?的主要内容,如果未能解决你的问题,请参考以下文章

当有人加入或离开其语音频道时,让机器人向文本频道发送消息

Discord.js 机器人无法离开语音频道

Discord.js 在文本频道中加入/离开语音频道通知

如何检测用户何时加入语音频道?

为啥我在尝试编辑离开语音频道的用户时会收到 DiscordAPIError?

创建通道切换记录器