public async Task ReleaseVoiceChannel([Summary("The id of the observed voice channel")] ulong voiceChannelId) { IReadOnlyCollection <IVoiceChannel> guildVoiceChannels = await Context.Guild.GetVoiceChannelsAsync(); IVoiceChannel voiceChannel = guildVoiceChannels.FirstOrDefault(currentVoiceChannel => currentVoiceChannel.Id == voiceChannelId); if (voiceChannel == null) { await Context.Channel.SendMessageAsync($"Voice channel with id {voiceChannelId} not found on this guild."); } else if (!_observedVoiceChannelsCache.TryGetValue(voiceChannelId, out ObservedVoiceChannel observedVoiceChannel)) { await Context.Channel.SendMessageAsync($"Voice channel {voiceChannel.Name} hasn't been observed."); } else { if (observedVoiceChannel.IsMuted) { IEnumerable <IGuildUser> voiceChannelUsers = await voiceChannel .GetUsersAsync() .FlattenAsync(); IEnumerable <Task> unmuteTasks = voiceChannelUsers .Where(voiceChannelUser => { SocketGuildUser socketGuildUser = voiceChannelUser as SocketGuildUser; bool userIsMuted = socketGuildUser.VoiceState?.IsMuted == true; bool userNotOffline = socketGuildUser.Status != UserStatus.Offline; return(userIsMuted && userNotOffline); }) .Select(voiceChannelUser => voiceChannelUser.ModifyAsync(guildUserProperties => guildUserProperties.Mute = false)); await Task.WhenAll(unmuteTasks); } _observedVoiceChannelsCache.Remove(voiceChannelId); await Context.Channel.SendMessageAsync($"Voice channel {voiceChannel.Name} has been released from observation."); } }
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); } } } } }