using Discord.Audio; using Discord.Rest; using System; using System.Collections.Generic; using System.Collections.Immutable; using System.Diagnostics; using System.IO; using System.Linq; using System.Threading.Tasks; using Model = Discord.API.Channel; namespace Discord.WebSocket { /// /// Represents a WebSocket-based voice channel in a guild. /// [DebuggerDisplay(@"{DebuggerDisplay,nq}")] public class SocketVoiceChannel : SocketTextChannel, IVoiceChannel, ISocketAudioChannel { #region SocketVoiceChannel /// /// Gets whether or not the guild has Text-In-Voice enabled and the voice channel is a TiV channel. /// /// /// Discord currently doesn't have a way to disable Text-In-Voice yet so this field is always /// on s and on /// s. /// public virtual bool IsTextInVoice => true; /// public int Bitrate { get; private set; } /// public int? UserLimit { get; private set; } /// public string RTCRegion { get; private set; } /// public VideoQualityMode VideoQualityMode { get; private set; } /// /// Gets a collection of users that are currently connected to this voice channel. /// /// /// A read-only collection of users that are currently connected to this voice channel. /// public IReadOnlyCollection ConnectedUsers => Guild.Users.Where(x => x.VoiceChannel?.Id == Id).ToImmutableArray(); internal SocketVoiceChannel(DiscordSocketClient discord, ulong id, SocketGuild guild) : base(discord, id, guild) { } internal new static SocketVoiceChannel Create(SocketGuild guild, ClientState state, Model model) { var entity = new SocketVoiceChannel(guild?.Discord, model.Id, guild); entity.Update(state, model); return entity; } /// internal override void Update(ClientState state, Model model) { base.Update(state, model); Bitrate = model.Bitrate.GetValueOrDefault(64000); UserLimit = model.UserLimit.GetValueOrDefault() != 0 ? model.UserLimit.Value : (int?)null; VideoQualityMode = model.VideoQualityMode.GetValueOrDefault(VideoQualityMode.Auto); RTCRegion = model.RTCRegion.GetValueOrDefault(null); } /// public Task ModifyAsync(Action func, RequestOptions options = null) => ChannelHelper.ModifyAsync(this, Discord, func, options); /// public async Task ConnectAsync(bool selfDeaf = false, bool selfMute = false, bool external = false) { return await Guild.ConnectAudioAsync(Id, selfDeaf, selfMute, external).ConfigureAwait(false); } /// public async Task DisconnectAsync() => await Guild.DisconnectAudioAsync(); /// public async Task ModifyAsync(Action func, RequestOptions options = null) { await Guild.ModifyAudioAsync(Id, func, options).ConfigureAwait(false); } /// public override SocketGuildUser GetUser(ulong id) { var user = Guild.GetUser(id); if (user?.VoiceChannel?.Id == Id) return user; return null; } /// Cannot create threads in voice channels. public override Task CreateThreadAsync(string name, ThreadType type = ThreadType.PublicThread, ThreadArchiveDuration autoArchiveDuration = ThreadArchiveDuration.OneDay, IMessage message = null, bool? invitable = null, int? slowmode = null, RequestOptions options = null) => throw new InvalidOperationException("Voice channels cannot contain threads."); /// Cannot modify text channel properties for voice channels. public override Task ModifyAsync(Action func, RequestOptions options = null) => throw new InvalidOperationException("Cannot modify text channel properties for voice channels."); #endregion #region TextOverrides /// This function is only supported in Text-In-Voice channels. public override Task GetMessageAsync(ulong id, RequestOptions options = null) { if (!IsTextInVoice) throw new NotSupportedException("This function is only supported in Text-In-Voice channels"); return base.GetMessageAsync(id, options); } /// This function is only supported in Text-In-Voice channels. public override Task DeleteMessageAsync(IMessage message, RequestOptions options = null) { if (!IsTextInVoice) throw new NotSupportedException("This function is only supported in Text-In-Voice channels"); return base.DeleteMessageAsync(message, options); } /// This function is only supported in Text-In-Voice channels. public override Task DeleteMessageAsync(ulong messageId, RequestOptions options = null) { if (!IsTextInVoice) throw new NotSupportedException("This function is only supported in Text-In-Voice channels"); return base.DeleteMessageAsync(messageId, options); } /// This function is only supported in Text-In-Voice channels. public override Task DeleteMessagesAsync(IEnumerable messages, RequestOptions options = null) { if (!IsTextInVoice) throw new NotSupportedException("This function is only supported in Text-In-Voice channels"); return base.DeleteMessagesAsync(messages, options); } /// This function is only supported in Text-In-Voice channels. public override Task DeleteMessagesAsync(IEnumerable messageIds, RequestOptions options = null) { if (!IsTextInVoice) throw new NotSupportedException("This function is only supported in Text-In-Voice channels"); return base.DeleteMessagesAsync(messageIds, options); } /// This function is only supported in Text-In-Voice channels. public override IDisposable EnterTypingState(RequestOptions options = null) { if (!IsTextInVoice) throw new NotSupportedException("This function is only supported in Text-In-Voice channels"); return base.EnterTypingState(options); } /// This function is only supported in Text-In-Voice channels. public override SocketMessage GetCachedMessage(ulong id) { if (!IsTextInVoice) throw new NotSupportedException("This function is only supported in Text-In-Voice channels"); return base.GetCachedMessage(id); } /// This function is only supported in Text-In-Voice channels. public override IReadOnlyCollection GetCachedMessages(IMessage fromMessage, Direction dir, int limit = 100) { if (!IsTextInVoice) throw new NotSupportedException("This function is only supported in Text-In-Voice channels"); return base.GetCachedMessages(fromMessage, dir, limit); } /// This function is only supported in Text-In-Voice channels. public override IReadOnlyCollection GetCachedMessages(int limit = 100) { if (!IsTextInVoice) throw new NotSupportedException("This function is only supported in Text-In-Voice channels"); return base.GetCachedMessages(limit); } /// This function is only supported in Text-In-Voice channels. public override IReadOnlyCollection GetCachedMessages(ulong fromMessageId, Direction dir, int limit = 100) { if (!IsTextInVoice) throw new NotSupportedException("This function is only supported in Text-In-Voice channels"); return base.GetCachedMessages(fromMessageId, dir, limit); } /// This function is only supported in Text-In-Voice channels. public override IAsyncEnumerable> GetMessagesAsync(IMessage fromMessage, Direction dir, int limit = 100, RequestOptions options = null) { if (!IsTextInVoice) throw new NotSupportedException("This function is only supported in Text-In-Voice channels"); return base.GetMessagesAsync(fromMessage, dir, limit, options); } /// This function is only supported in Text-In-Voice channels. public override IAsyncEnumerable> GetMessagesAsync(int limit = 100, RequestOptions options = null) { if (!IsTextInVoice) throw new NotSupportedException("This function is only supported in Text-In-Voice channels"); return base.GetMessagesAsync(limit, options); } /// This function is only supported in Text-In-Voice channels. public override IAsyncEnumerable> GetMessagesAsync(ulong fromMessageId, Direction dir, int limit = 100, RequestOptions options = null) { if (!IsTextInVoice) throw new NotSupportedException("This function is only supported in Text-In-Voice channels"); return base.GetMessagesAsync(fromMessageId, dir, limit, options); } /// This function is only supported in Text-In-Voice channels. public override Task> GetPinnedMessagesAsync(RequestOptions options = null) { if (!IsTextInVoice) throw new NotSupportedException("This function is only supported in Text-In-Voice channels"); return base.GetPinnedMessagesAsync(options); } /// This function is only supported in Text-In-Voice channels. public override Task GetWebhookAsync(ulong id, RequestOptions options = null) { if (!IsTextInVoice) throw new NotSupportedException("This function is only supported in Text-In-Voice channels"); return base.GetWebhookAsync(id, options); } /// This function is only supported in Text-In-Voice channels. public override Task> GetWebhooksAsync(RequestOptions options = null) { if (!IsTextInVoice) throw new NotSupportedException("This function is only supported in Text-In-Voice channels"); return base.GetWebhooksAsync(options); } /// This function is only supported in Text-In-Voice channels. public override Task CreateWebhookAsync(string name, Stream avatar = null, RequestOptions options = null) { if (!IsTextInVoice) throw new NotSupportedException("This function is only supported in Text-In-Voice channels"); return base.CreateWebhookAsync(name, avatar, options); } /// This function is only supported in Text-In-Voice channels. public override Task ModifyMessageAsync(ulong messageId, Action func, RequestOptions options = null) { if (!IsTextInVoice) throw new NotSupportedException("This function is only supported in Text-In-Voice channels"); return base.ModifyMessageAsync(messageId, func, options); } /// This function is only supported in Text-In-Voice channels. public override Task SendFileAsync(FileAttachment attachment, string text, bool isTTS = false, Embed embed = null, RequestOptions options = null, AllowedMentions allowedMentions = null, MessageReference messageReference = null, MessageComponent components = null, ISticker[] stickers = null, Embed[] embeds = null, MessageFlags flags = MessageFlags.None) { if (!IsTextInVoice) throw new NotSupportedException("This function is only supported in Text-In-Voice channels"); return base.SendFileAsync(attachment, text, isTTS, embed, options, allowedMentions, messageReference, components, stickers, embeds, flags); } /// This function is only supported in Text-In-Voice channels. public override Task SendFileAsync(Stream stream, string filename, string text, bool isTTS = false, Embed embed = null, RequestOptions options = null, bool isSpoiler = false, AllowedMentions allowedMentions = null, MessageReference messageReference = null, MessageComponent components = null, ISticker[] stickers = null, Embed[] embeds = null, MessageFlags flags = MessageFlags.None) { if (!IsTextInVoice) throw new NotSupportedException("This function is only supported in Text-In-Voice channels"); return base.SendFileAsync(stream, filename, text, isTTS, embed, options, isSpoiler, allowedMentions, messageReference, components, stickers, embeds, flags); } /// This function is only supported in Text-In-Voice channels. public override Task SendFileAsync(string filePath, string text, bool isTTS = false, Embed embed = null, RequestOptions options = null, bool isSpoiler = false, AllowedMentions allowedMentions = null, MessageReference messageReference = null, MessageComponent components = null, ISticker[] stickers = null, Embed[] embeds = null, MessageFlags flags = MessageFlags.None) { if (!IsTextInVoice) throw new NotSupportedException("This function is only supported in Text-In-Voice channels"); return base.SendFileAsync(filePath, text, isTTS, embed, options, isSpoiler, allowedMentions, messageReference, components, stickers, embeds, flags); } /// This function is only supported in Text-In-Voice channels. public override Task SendFilesAsync(IEnumerable attachments, string text, bool isTTS = false, Embed embed = null, RequestOptions options = null, AllowedMentions allowedMentions = null, MessageReference messageReference = null, MessageComponent components = null, ISticker[] stickers = null, Embed[] embeds = null, MessageFlags flags = MessageFlags.None) { if (!IsTextInVoice) throw new NotSupportedException("This function is only supported in Text-In-Voice channels"); return base.SendFilesAsync(attachments, text, isTTS, embed, options, allowedMentions, messageReference, components, stickers, embeds, flags); } /// This function is only supported in Text-In-Voice channels. public override Task SendMessageAsync(string text = null, bool isTTS = false, Embed embed = null, RequestOptions options = null, AllowedMentions allowedMentions = null, MessageReference messageReference = null, MessageComponent components = null, ISticker[] stickers = null, Embed[] embeds = null, MessageFlags flags = MessageFlags.None) { if (!IsTextInVoice) throw new NotSupportedException("This function is only supported in Text-In-Voice channels"); return base.SendMessageAsync(text, isTTS, embed, options, allowedMentions, messageReference, components, stickers, embeds, flags); } /// This function is only supported in Text-In-Voice channels. public override Task TriggerTypingAsync(RequestOptions options = null) { if (!IsTextInVoice) throw new NotSupportedException("This function is only supported in Text-In-Voice channels"); return base.TriggerTypingAsync(options); } /// Threads are not supported in voice channels public override Task> GetActiveThreadsAsync(RequestOptions options = null) => throw new NotSupportedException("Threads are not supported in voice channels"); #endregion private string DebuggerDisplay => $"{Name} ({Id}, Voice)"; internal new SocketVoiceChannel Clone() => MemberwiseClone() as SocketVoiceChannel; #region IGuildChannel /// Task IGuildChannel.GetUserAsync(ulong id, CacheMode mode, RequestOptions options) => Task.FromResult(GetUser(id)); /// IAsyncEnumerable> IGuildChannel.GetUsersAsync(CacheMode mode, RequestOptions options) => ImmutableArray.Create>(Users).ToAsyncEnumerable(); #endregion #region INestedChannel /// Task INestedChannel.GetCategoryAsync(CacheMode mode, RequestOptions options) => Task.FromResult(Category); #endregion } }