| @@ -1,7 +1,3 @@ | |||||
| using System; | |||||
| using System.Collections.Generic; | |||||
| using System.Linq; | |||||
| using System.Text; | |||||
| using System.Threading.Tasks; | using System.Threading.Tasks; | ||||
| namespace Discord | namespace Discord | ||||
| @@ -313,6 +313,16 @@ namespace Discord.Rest | |||||
| return models.Select(x => RestWebhook.Create(client, channel, x)) | return models.Select(x => RestWebhook.Create(client, channel, x)) | ||||
| .ToImmutableArray(); | .ToImmutableArray(); | ||||
| } | } | ||||
| // Categories | |||||
| public static async Task<ICategoryChannel> GetCategoryAsync(INestedChannel channel, BaseDiscordClient client, RequestOptions options) | |||||
| { | |||||
| // if no category id specified, return null | |||||
| if (!channel.CategoryId.HasValue) | |||||
| return null; | |||||
| // CategoryId will contain a value here | |||||
| var model = await client.ApiClient.GetChannelAsync(channel.CategoryId.Value, options).ConfigureAwait(false); | |||||
| return RestCategoryChannel.Create(client, model) as ICategoryChannel; | |||||
| } | |||||
| //Helpers | //Helpers | ||||
| private static IUser GetAuthor(BaseDiscordClient client, IGuild guild, UserModel model, ulong? webhookId) | private static IUser GetAuthor(BaseDiscordClient client, IGuild guild, UserModel model, ulong? webhookId) | ||||
| @@ -32,7 +32,7 @@ namespace Discord.Rest | |||||
| internal override void Update(Model model) | internal override void Update(Model model) | ||||
| { | { | ||||
| base.Update(model); | base.Update(model); | ||||
| CategoryId = model.CategoryId; | |||||
| Topic = model.Topic.Value; | Topic = model.Topic.Value; | ||||
| _nsfw = model.Nsfw.GetValueOrDefault(); | _nsfw = model.Nsfw.GetValueOrDefault(); | ||||
| } | } | ||||
| @@ -47,7 +47,7 @@ namespace Discord.Rest | |||||
| => ChannelHelper.GetUserAsync(this, Guild, Discord, id, options); | => ChannelHelper.GetUserAsync(this, Guild, Discord, id, options); | ||||
| public IAsyncEnumerable<IReadOnlyCollection<RestGuildUser>> GetUsersAsync(RequestOptions options = null) | public IAsyncEnumerable<IReadOnlyCollection<RestGuildUser>> GetUsersAsync(RequestOptions options = null) | ||||
| => ChannelHelper.GetUsersAsync(this, Guild, Discord, null, null, options); | => ChannelHelper.GetUsersAsync(this, Guild, Discord, null, null, options); | ||||
| public Task<RestMessage> GetMessageAsync(ulong id, RequestOptions options = null) | public Task<RestMessage> GetMessageAsync(ulong id, RequestOptions options = null) | ||||
| => ChannelHelper.GetMessageAsync(this, Discord, id, options); | => ChannelHelper.GetMessageAsync(this, Discord, id, options); | ||||
| public IAsyncEnumerable<IReadOnlyCollection<RestMessage>> GetMessagesAsync(int limit = DiscordConfig.MaxMessagesPerBatch, RequestOptions options = null) | public IAsyncEnumerable<IReadOnlyCollection<RestMessage>> GetMessagesAsync(int limit = DiscordConfig.MaxMessagesPerBatch, RequestOptions options = null) | ||||
| @@ -84,6 +84,9 @@ namespace Discord.Rest | |||||
| => ChannelHelper.GetWebhookAsync(this, Discord, id, options); | => ChannelHelper.GetWebhookAsync(this, Discord, id, options); | ||||
| public Task<IReadOnlyCollection<RestWebhook>> GetWebhooksAsync(RequestOptions options = null) | public Task<IReadOnlyCollection<RestWebhook>> GetWebhooksAsync(RequestOptions options = null) | ||||
| => ChannelHelper.GetWebhooksAsync(this, Discord, options); | => ChannelHelper.GetWebhooksAsync(this, Discord, options); | ||||
| public Task<ICategoryChannel> GetCategoryAsync(RequestOptions options = null) | |||||
| => ChannelHelper.GetCategoryAsync(this, Discord, options); | |||||
| private string DebuggerDisplay => $"{Name} ({Id}, Text)"; | private string DebuggerDisplay => $"{Name} ({Id}, Text)"; | ||||
| @@ -110,6 +113,7 @@ namespace Discord.Rest | |||||
| else | else | ||||
| return AsyncEnumerable.Empty<IReadOnlyCollection<IMessage>>(); | return AsyncEnumerable.Empty<IReadOnlyCollection<IMessage>>(); | ||||
| } | } | ||||
| IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(ulong fromMessageId, Direction dir, int limit, CacheMode mode, RequestOptions options) | IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(ulong fromMessageId, Direction dir, int limit, CacheMode mode, RequestOptions options) | ||||
| { | { | ||||
| if (mode == CacheMode.AllowDownload) | if (mode == CacheMode.AllowDownload) | ||||
| @@ -39,6 +39,9 @@ namespace Discord.Rest | |||||
| Update(model); | Update(model); | ||||
| } | } | ||||
| public Task<ICategoryChannel> GetCategoryAsync(RequestOptions options = null) | |||||
| => ChannelHelper.GetCategoryAsync(this, Discord, options); | |||||
| private string DebuggerDisplay => $"{Name} ({Id}, Voice)"; | private string DebuggerDisplay => $"{Name} ({Id}, Voice)"; | ||||
| //IAudioChannel | //IAudioChannel | ||||
| @@ -20,7 +20,7 @@ namespace Discord.WebSocket | |||||
| ChannelPermission.ViewChannel)).ToImmutableArray(); | ChannelPermission.ViewChannel)).ToImmutableArray(); | ||||
| public IReadOnlyCollection<SocketGuildChannel> Channels | public IReadOnlyCollection<SocketGuildChannel> Channels | ||||
| => Guild.Channels.Where(x => x.CategoryId == Id).ToImmutableArray(); | |||||
| => Guild.Channels.Where(x => x is INestedChannel && (x as INestedChannel).CategoryId == Id).ToImmutableArray(); | |||||
| internal SocketCategoryChannel(DiscordSocketClient discord, ulong id, SocketGuild guild) | internal SocketCategoryChannel(DiscordSocketClient discord, ulong id, SocketGuild guild) | ||||
| : base(discord, id, guild) | : base(discord, id, guild) | ||||
| @@ -51,19 +51,6 @@ namespace Discord.WebSocket | |||||
| internal new SocketCategoryChannel Clone() => MemberwiseClone() as SocketCategoryChannel; | internal new SocketCategoryChannel Clone() => MemberwiseClone() as SocketCategoryChannel; | ||||
| // IGuildChannel | // IGuildChannel | ||||
| /// <summary> | |||||
| /// Throws a NotSupportedException because Channel Categories cannot be the child of another Channel Category. | |||||
| /// </summary> | |||||
| /// <exception cref="NotSupportedException">A NotSupportedException is always thrown because Channel Categories do not support being nested.</exception> | |||||
| ulong? IGuildChannel.CategoryId | |||||
| => throw new NotSupportedException(); | |||||
| /// <summary> | |||||
| /// Throws a NotSupportedException because Channel Categories cannot be the child of another Channel Category. | |||||
| /// </summary> | |||||
| /// <exception cref="NotSupportedException">A NotSupportedException is always thrown because Channel Categories do not support being nested.</exception> | |||||
| Task<ICategoryChannel> IGuildChannel.GetCategoryAsync() | |||||
| => throw new NotSupportedException(); | |||||
| IAsyncEnumerable<IReadOnlyCollection<IGuildUser>> IGuildChannel.GetUsersAsync(CacheMode mode, RequestOptions options) | IAsyncEnumerable<IReadOnlyCollection<IGuildUser>> IGuildChannel.GetUsersAsync(CacheMode mode, RequestOptions options) | ||||
| => ImmutableArray.Create<IReadOnlyCollection<IGuildUser>>(Users).ToAsyncEnumerable(); | => ImmutableArray.Create<IReadOnlyCollection<IGuildUser>>(Users).ToAsyncEnumerable(); | ||||
| Task<IGuildUser> IGuildChannel.GetUserAsync(ulong id, CacheMode mode, RequestOptions options) | Task<IGuildUser> IGuildChannel.GetUserAsync(ulong id, CacheMode mode, RequestOptions options) | ||||
| @@ -1,4 +1,4 @@ | |||||
| using Discord.Rest; | |||||
| using Discord.Rest; | |||||
| using System; | using System; | ||||
| using System.Collections.Generic; | using System.Collections.Generic; | ||||
| using System.Collections.Immutable; | using System.Collections.Immutable; | ||||
| @@ -16,10 +16,7 @@ namespace Discord.WebSocket | |||||
| public SocketGuild Guild { get; } | public SocketGuild Guild { get; } | ||||
| public string Name { get; private set; } | public string Name { get; private set; } | ||||
| public int Position { get; private set; } | |||||
| public ulong? CategoryId { get; private set; } | |||||
| public ICategoryChannel Category | |||||
| => CategoryId.HasValue ? Guild.GetChannel(CategoryId.Value) as ICategoryChannel : null; | |||||
| public int Position { get; private set; } | |||||
| public IReadOnlyCollection<Overwrite> PermissionOverwrites => _overwrites; | public IReadOnlyCollection<Overwrite> PermissionOverwrites => _overwrites; | ||||
| public new virtual IReadOnlyCollection<SocketGuildUser> Users => ImmutableArray.Create<SocketGuildUser>(); | public new virtual IReadOnlyCollection<SocketGuildUser> Users => ImmutableArray.Create<SocketGuildUser>(); | ||||
| @@ -48,8 +45,7 @@ namespace Discord.WebSocket | |||||
| { | { | ||||
| Name = model.Name.Value; | Name = model.Name.Value; | ||||
| Position = model.Position.Value; | Position = model.Position.Value; | ||||
| CategoryId = model.CategoryId; | |||||
| var overwrites = model.PermissionOverwrites.Value; | var overwrites = model.PermissionOverwrites.Value; | ||||
| var newOverwrites = ImmutableArray.CreateBuilder<Overwrite>(overwrites.Length); | var newOverwrites = ImmutableArray.CreateBuilder<Overwrite>(overwrites.Length); | ||||
| for (int i = 0; i < overwrites.Length; i++) | for (int i = 0; i < overwrites.Length; i++) | ||||
| @@ -135,9 +131,6 @@ namespace Discord.WebSocket | |||||
| IGuild IGuildChannel.Guild => Guild; | IGuild IGuildChannel.Guild => Guild; | ||||
| ulong IGuildChannel.GuildId => Guild.Id; | ulong IGuildChannel.GuildId => Guild.Id; | ||||
| Task<ICategoryChannel> IGuildChannel.GetCategoryAsync() | |||||
| => Task.FromResult(Category); | |||||
| async Task<IReadOnlyCollection<IInviteMetadata>> IGuildChannel.GetInvitesAsync(RequestOptions options) | async Task<IReadOnlyCollection<IInviteMetadata>> IGuildChannel.GetInvitesAsync(RequestOptions options) | ||||
| => await GetInvitesAsync(options).ConfigureAwait(false); | => await GetInvitesAsync(options).ConfigureAwait(false); | ||||
| async Task<IInviteMetadata> IGuildChannel.CreateInviteAsync(int? maxAge, int? maxUses, bool isTemporary, bool isUnique, RequestOptions options) | async Task<IInviteMetadata> IGuildChannel.CreateInviteAsync(int? maxAge, int? maxUses, bool isTemporary, bool isUnique, RequestOptions options) | ||||
| @@ -45,7 +45,7 @@ namespace Discord.WebSocket | |||||
| internal override void Update(ClientState state, Model model) | internal override void Update(ClientState state, Model model) | ||||
| { | { | ||||
| base.Update(state, model); | base.Update(state, model); | ||||
| CategoryId = model.CategoryId; | |||||
| Topic = model.Topic.Value; | Topic = model.Topic.Value; | ||||
| _nsfw = model.Nsfw.GetValueOrDefault(); | _nsfw = model.Nsfw.GetValueOrDefault(); | ||||
| } | } | ||||
| @@ -124,6 +124,10 @@ namespace Discord.WebSocket | |||||
| public Task<IReadOnlyCollection<RestWebhook>> GetWebhooksAsync(RequestOptions options = null) | public Task<IReadOnlyCollection<RestWebhook>> GetWebhooksAsync(RequestOptions options = null) | ||||
| => ChannelHelper.GetWebhooksAsync(this, Discord, options); | => ChannelHelper.GetWebhooksAsync(this, Discord, options); | ||||
| // Categories | |||||
| public Task<ICategoryChannel> GetCategoryAsync(RequestOptions options = null) | |||||
| => ChannelHelper.GetCategoryAsync(this, Discord, options); | |||||
| private string DebuggerDisplay => $"{Name} ({Id}, Text)"; | private string DebuggerDisplay => $"{Name} ({Id}, Text)"; | ||||
| internal new SocketTextChannel Clone() => MemberwiseClone() as SocketTextChannel; | internal new SocketTextChannel Clone() => MemberwiseClone() as SocketTextChannel; | ||||
| @@ -35,7 +35,7 @@ namespace Discord.WebSocket | |||||
| internal override void Update(ClientState state, Model model) | internal override void Update(ClientState state, Model model) | ||||
| { | { | ||||
| base.Update(state, model); | base.Update(state, model); | ||||
| CategoryId = model.CategoryId; | |||||
| Bitrate = model.Bitrate.Value; | Bitrate = model.Bitrate.Value; | ||||
| UserLimit = model.UserLimit.Value != 0 ? model.UserLimit.Value : (int?)null; | UserLimit = model.UserLimit.Value != 0 ? model.UserLimit.Value : (int?)null; | ||||
| } | } | ||||
| @@ -55,7 +55,10 @@ namespace Discord.WebSocket | |||||
| return user; | return user; | ||||
| return null; | return null; | ||||
| } | } | ||||
| public Task<ICategoryChannel> GetCategoryAsync(RequestOptions options = null) | |||||
| => ChannelHelper.GetCategoryAsync(this, Discord, options); | |||||
| private string DebuggerDisplay => $"{Name} ({Id}, Voice)"; | private string DebuggerDisplay => $"{Name} ({Id}, Voice)"; | ||||
| internal new SocketVoiceChannel Clone() => MemberwiseClone() as SocketVoiceChannel; | internal new SocketVoiceChannel Clone() => MemberwiseClone() as SocketVoiceChannel; | ||||
| @@ -156,18 +156,6 @@ namespace Discord | |||||
| var cat1 = await guild.CreateCategoryChannelAsync("Cat1"); | var cat1 = await guild.CreateCategoryChannelAsync("Cat1"); | ||||
| var cat2 = await guild.CreateCategoryChannelAsync("Cat2"); | var cat2 = await guild.CreateCategoryChannelAsync("Cat2"); | ||||
| // check that both CategoryID and GetCategoryID throw NotSupportedException | |||||
| // because Categories cannot be nested | |||||
| Assert.Throws<NotSupportedException>(() => | |||||
| { | |||||
| var x = cat1.CategoryId; | |||||
| }); | |||||
| Assert.Throws<NotSupportedException>(() => | |||||
| { | |||||
| var x = cat2.GetCategoryAsync(); | |||||
| }); | |||||
| var text1 = await guild.CreateTextChannelAsync("nestedText1"); | var text1 = await guild.CreateTextChannelAsync("nestedText1"); | ||||
| var voice1 = await guild.CreateVoiceChannelAsync("nestedVoice1"); | var voice1 = await guild.CreateVoiceChannelAsync("nestedVoice1"); | ||||
| // set the text channel parent to Cat 1 | // set the text channel parent to Cat 1 | ||||
| @@ -185,10 +173,17 @@ namespace Discord | |||||
| // assert that CategoryId works for text channels | // assert that CategoryId works for text channels | ||||
| Assert.Equal(text1.CategoryId, cat1.Id); | Assert.Equal(text1.CategoryId, cat1.Id); | ||||
| Assert.True(text1 is INestedChannel); | |||||
| Assert.Equal((await (text1 as INestedChannel).GetCategoryAsync()).Id, cat1.Id); | |||||
| Assert.Equal((await text1.GetCategoryAsync()).Id, cat1.Id); | Assert.Equal((await text1.GetCategoryAsync()).Id, cat1.Id); | ||||
| Assert.Equal(text1.CategoryId, cat1.Id); | |||||
| // and for voice channels | // and for voice channels | ||||
| Assert.Equal(voice1.CategoryId, cat2.Id); | Assert.Equal(voice1.CategoryId, cat2.Id); | ||||
| Assert.True(voice1 is INestedChannel); | |||||
| Assert.Equal((await (voice1 as INestedChannel).GetCategoryAsync()).Id, cat2.Id); | |||||
| Assert.Equal((await voice1.GetCategoryAsync()).Id, cat2.Id); | Assert.Equal((await voice1.GetCategoryAsync()).Id, cat2.Id); | ||||
| Assert.Equal(voice1.CategoryId, cat1.Id); | |||||
| // incomplete test, could use more coverage of other methods | // incomplete test, could use more coverage of other methods | ||||
| } | } | ||||