Skip to content

Commit

Permalink
removed Guild from class name for ChannelCategory
Browse files Browse the repository at this point in the history
Renamed all properties to use Category instead of Parent
Throw exception on GetUsers / GetInvites etc for categories
  • Loading branch information
pegasy committed Sep 24, 2017
1 parent e18bd8c commit 5c4777d
Show file tree
Hide file tree
Showing 19 changed files with 145 additions and 140 deletions.
Expand Up @@ -29,6 +29,6 @@ public class GuildChannelProperties
/// <summary>
/// Sets the category for this channel
/// </summary>
public Optional<ulong?> ParentId { get; set; }
public Optional<ulong?> CategoryId { get; set; }
}
}
Expand Up @@ -6,7 +6,7 @@

namespace Discord
{
//public class GuildChannelCategoryProperties : GuildChannelProperties
//{
//}
public interface IChannelCategory : IGuildChannel
{
}
}
4 changes: 2 additions & 2 deletions src/Discord.Net.Core/Entities/Channels/IGuildChannel.cs
Expand Up @@ -10,9 +10,9 @@ public interface IGuildChannel : IChannel, IDeletable
int Position { get; }

/// <summary> Gets the parentid (category) of this channel in the guild's channel list. </summary>
ulong? ParentId { get; }
ulong? CategoryId { get; }
/// <summary> Gets the parent channel (category) of this channel. </summary>
Task<IGuildChannel> GetParentChannelAsync();
Task<IChannelCategory> GetCategory();
/// <summary> Gets the guild this channel is a member of. </summary>
IGuild Guild { get; }
/// <summary> Gets the id of the guild this channel is a member of. </summary>
Expand Down
14 changes: 0 additions & 14 deletions src/Discord.Net.Core/Entities/Channels/IGuildChannelCategory.cs

This file was deleted.

4 changes: 3 additions & 1 deletion src/Discord.Net.Core/Entities/Guilds/IGuild.cs
Expand Up @@ -84,7 +84,7 @@ public interface IGuild : IDeletable, ISnowflakeEntity
Task<IReadOnlyCollection<ITextChannel>> GetTextChannelsAsync(CacheMode mode = CacheMode.AllowDownload, RequestOptions options = null);
Task<ITextChannel> GetTextChannelAsync(ulong id, CacheMode mode = CacheMode.AllowDownload, RequestOptions options = null);
Task<IReadOnlyCollection<IVoiceChannel>> GetVoiceChannelsAsync(CacheMode mode = CacheMode.AllowDownload, RequestOptions options = null);
Task<IReadOnlyCollection<IGuildChannelCategory>> GetChannelCategoriesAsync(CacheMode mode = CacheMode.AllowDownload, RequestOptions options = null);
Task<IReadOnlyCollection<IChannelCategory>> GetChannelCategoriesAsync(CacheMode mode = CacheMode.AllowDownload, RequestOptions options = null);
Task<IVoiceChannel> GetVoiceChannelAsync(ulong id, CacheMode mode = CacheMode.AllowDownload, RequestOptions options = null);
Task<IVoiceChannel> GetAFKChannelAsync(CacheMode mode = CacheMode.AllowDownload, RequestOptions options = null);
Task<ITextChannel> GetSystemChannelAsync(CacheMode mode = CacheMode.AllowDownload, RequestOptions options = null);
Expand All @@ -94,6 +94,8 @@ public interface IGuild : IDeletable, ISnowflakeEntity
Task<ITextChannel> CreateTextChannelAsync(string name, RequestOptions options = null);
/// <summary> Creates a new voice channel. </summary>
Task<IVoiceChannel> CreateVoiceChannelAsync(string name, RequestOptions options = null);
/// <summary> Creates a new channel category. </summary>
Task<IChannelCategory> CreateChannelCategoryAsync(string name, RequestOptions options = null);

Task<IReadOnlyCollection<IGuildIntegration>> GetIntegrationsAsync(RequestOptions options = null);
Task<IGuildIntegration> CreateIntegrationAsync(ulong id, string type, RequestOptions options = null);
Expand Down
2 changes: 1 addition & 1 deletion src/Discord.Net.Rest/API/Common/Channel.cs
Expand Up @@ -24,7 +24,7 @@ internal class Channel
[JsonProperty("permission_overwrites")]
public Optional<Overwrite[]> PermissionOverwrites { get; set; }
[JsonProperty("parent_id")]
public ulong? ParentId { get; set; }
public ulong? CategoryId { get; set; }

//TextChannel
[JsonProperty("topic")]
Expand Down
14 changes: 0 additions & 14 deletions src/Discord.Net.Rest/API/Rest/ModifyGuildChannelCategoryParams.cs

This file was deleted.

2 changes: 1 addition & 1 deletion src/Discord.Net.Rest/API/Rest/ModifyGuildChannelParams.cs
Expand Up @@ -11,6 +11,6 @@ internal class ModifyGuildChannelParams
[JsonProperty("position")]
public Optional<int> Position { get; set; }
[JsonProperty("parent_id")]
public Optional<ulong?> ParentId { get; set; }
public Optional<ulong?> CategoryId { get; set; }
}
}
6 changes: 3 additions & 3 deletions src/Discord.Net.Rest/Entities/Channels/ChannelHelper.cs
Expand Up @@ -28,7 +28,7 @@ internal static class ChannelHelper
{
Name = args.Name,
Position = args.Position,
ParentId = args.ParentId
CategoryId = args.CategoryId
};
return await client.ApiClient.ModifyGuildChannelAsync(channel.Id, apiArgs, options).ConfigureAwait(false);
}
Expand All @@ -42,7 +42,7 @@ internal static class ChannelHelper
{
Name = args.Name,
Position = args.Position,
ParentId = args.ParentId,
CategoryId = args.CategoryId,
Topic = args.Topic,
IsNsfw = args.IsNsfw
};
Expand All @@ -59,7 +59,7 @@ internal static class ChannelHelper
Bitrate = args.Bitrate,
Name = args.Name,
Position = args.Position,
ParentId = args.ParentId,
CategoryId = args.CategoryId,
UserLimit = args.UserLimit.IsSpecified ? (args.UserLimit.Value ?? 0) : Optional.Create<int>()
};
return await client.ApiClient.ModifyGuildChannelAsync(channel.Id, apiArgs, options).ConfigureAwait(false);
Expand Down
45 changes: 45 additions & 0 deletions src/Discord.Net.Rest/Entities/Channels/RestChannelCategory.cs
@@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Model = Discord.API.Channel;

namespace Discord.Rest
{
[DebuggerDisplay(@"{DebuggerDisplay,nq}")]
public class RestChannelCategory : RestGuildChannel, IChannelCategory
{
public string Mention => MentionUtils.MentionChannel(Id);

internal RestChannelCategory(BaseDiscordClient discord, IGuild guild, ulong id)
: base(discord, guild, id)
{
}
internal new static RestChannelCategory Create(BaseDiscordClient discord, IGuild guild, Model model)
{
var entity = new RestChannelCategory(discord, guild, model.Id);
entity.Update(model);
return entity;
}

private string DebuggerDisplay => $"{Name} ({Id}, Category)";

// IGuildChannel
IAsyncEnumerable<IReadOnlyCollection<IGuildUser>> IGuildChannel.GetUsersAsync(CacheMode mode, RequestOptions options)
=> throw new NotSupportedException();
Task<IGuildUser> IGuildChannel.GetUserAsync(ulong id, CacheMode mode, RequestOptions options)
=> throw new NotSupportedException();
Task<IInviteMetadata> IGuildChannel.CreateInviteAsync(int? maxAge, int? maxUses, bool isTemporary, bool isUnique, RequestOptions options)
=> throw new NotSupportedException();
Task<IReadOnlyCollection<IInviteMetadata>> IGuildChannel.GetInvitesAsync(RequestOptions options)
=> throw new NotSupportedException();

//IChannel
IAsyncEnumerable<IReadOnlyCollection<IUser>> IChannel.GetUsersAsync(CacheMode mode, RequestOptions options)
=> throw new NotSupportedException();
Task<IUser> IChannel.GetUserAsync(ulong id, CacheMode mode, RequestOptions options)
=> throw new NotSupportedException();
}
}
6 changes: 4 additions & 2 deletions src/Discord.Net.Rest/Entities/Channels/RestGuildChannel.cs
Expand Up @@ -16,8 +16,8 @@ public class RestGuildChannel : RestChannel, IGuildChannel, IUpdateable
internal IGuild Guild { get; }
public string Name { get; private set; }
public int Position { get; private set; }
public ulong? ParentId { get; private set; }
public Task<IGuildChannel> GetParentChannelAsync() => ParentId == null ? null : Guild.GetChannelAsync(ParentId.Value);
public ulong? CategoryId { get; private set; }
public async Task<IChannelCategory> GetCategory() => CategoryId == null ? null : await Guild.GetChannelAsync(CategoryId.Value) as IChannelCategory;

public ulong GuildId => Guild.Id;

Expand All @@ -34,6 +34,8 @@ internal static RestGuildChannel Create(BaseDiscordClient discord, IGuild guild,
return RestTextChannel.Create(discord, guild, model);
case ChannelType.Voice:
return RestVoiceChannel.Create(discord, guild, model);
case ChannelType.Category:
return RestChannelCategory.Create(discord, guild, model);
default:
// TODO: Channel categories
return new RestGuildChannel(discord, guild, model.Id);
Expand Down
38 changes: 0 additions & 38 deletions src/Discord.Net.Rest/Entities/Channels/RestGuildChannelCategory.cs

This file was deleted.

9 changes: 9 additions & 0 deletions src/Discord.Net.Rest/Entities/Guilds/GuildHelper.cs
Expand Up @@ -157,6 +157,15 @@ internal static class GuildHelper
var model = await client.ApiClient.CreateGuildChannelAsync(guild.Id, args, options).ConfigureAwait(false);
return RestVoiceChannel.Create(client, guild, model);
}
public static async Task<RestChannelCategory> CreateChannelCategoryAsync(IGuild guild, BaseDiscordClient client,
string name, RequestOptions options)
{
if (name == null) throw new ArgumentNullException(nameof(name));

var args = new CreateGuildChannelParams(name, ChannelType.Category);
var model = await client.ApiClient.CreateGuildChannelAsync(guild.Id, args, options).ConfigureAwait(false);
return RestChannelCategory.Create(client, guild, model);
}

//Integrations
public static async Task<IReadOnlyCollection<RestGuildIntegration>> GetIntegrationsAsync(IGuild guild, BaseDiscordClient client,
Expand Down
10 changes: 7 additions & 3 deletions src/Discord.Net.Rest/Entities/Guilds/RestGuild.cs
Expand Up @@ -176,10 +176,10 @@ public async Task<IReadOnlyCollection<RestVoiceChannel>> GetVoiceChannelsAsync(R
var channels = await GuildHelper.GetChannelsAsync(this, Discord, options).ConfigureAwait(false);
return channels.Select(x => x as RestVoiceChannel).Where(x => x != null).ToImmutableArray();
}
public async Task<IReadOnlyCollection<RestGuildChannelCategory>> GetChannelCategoriesAsync(RequestOptions options = null)
public async Task<IReadOnlyCollection<RestChannelCategory>> GetChannelCategoriesAsync(RequestOptions options = null)
{
var channels = await GuildHelper.GetChannelsAsync(this, Discord, options).ConfigureAwait(false);
return channels.Select(x => x as RestGuildChannelCategory).Where(x => x != null).ToImmutableArray();
return channels.Select(x => x as RestChannelCategory).Where(x => x != null).ToImmutableArray();
}

public async Task<RestVoiceChannel> GetAFKChannelAsync(RequestOptions options = null)
Expand Down Expand Up @@ -222,6 +222,8 @@ public Task<RestTextChannel> CreateTextChannelAsync(string name, RequestOptions
=> GuildHelper.CreateTextChannelAsync(this, Discord, name, options);
public Task<RestVoiceChannel> CreateVoiceChannelAsync(string name, RequestOptions options = null)
=> GuildHelper.CreateVoiceChannelAsync(this, Discord, name, options);
public Task<RestChannelCategory> CreateChannelCategoryAsync(string name, RequestOptions options = null)
=> GuildHelper.CreateChannelCategoryAsync(this, Discord, name, options);

//Integrations
public Task<IReadOnlyCollection<RestGuildIntegration>> GetIntegrationsAsync(RequestOptions options = null)
Expand Down Expand Up @@ -309,7 +311,7 @@ async Task<IReadOnlyCollection<IVoiceChannel>> IGuild.GetVoiceChannelsAsync(Cach
else
return ImmutableArray.Create<IVoiceChannel>();
}
async Task<IReadOnlyCollection<IGuildChannelCategory>> IGuild.GetChannelCategoriesAsync(CacheMode mode , RequestOptions options)
async Task<IReadOnlyCollection<IChannelCategory>> IGuild.GetChannelCategoriesAsync(CacheMode mode , RequestOptions options)
{
if (mode == CacheMode.AllowDownload)
return await GetChannelCategoriesAsync(options).ConfigureAwait(false);
Expand Down Expand Up @@ -355,6 +357,8 @@ async Task<ITextChannel> IGuild.CreateTextChannelAsync(string name, RequestOptio
=> await CreateTextChannelAsync(name, options).ConfigureAwait(false);
async Task<IVoiceChannel> IGuild.CreateVoiceChannelAsync(string name, RequestOptions options)
=> await CreateVoiceChannelAsync(name, options).ConfigureAwait(false);
async Task<IChannelCategory> IGuild.CreateChannelCategoryAsync(string name, RequestOptions options)
=> await CreateChannelCategoryAsync(name, options).ConfigureAwait(false);

async Task<IReadOnlyCollection<IGuildIntegration>> IGuild.GetIntegrationsAsync(RequestOptions options)
=> await GetIntegrationsAsync(options).ConfigureAwait(false);
Expand Down
4 changes: 2 additions & 2 deletions src/Discord.Net.Rpc/Entities/Channels/RpcGuildChannel.cs
Expand Up @@ -10,7 +10,7 @@ public class RpcGuildChannel : RpcChannel, IGuildChannel
{
public ulong GuildId { get; }
public int Position { get; private set; }
public ulong? ParentId { get; private set; }
public ulong? CategoryId { get; private set; }

internal RpcGuildChannel(DiscordRpcClient discord, ulong id, ulong guildId)
: base(discord, id)
Expand Down Expand Up @@ -58,7 +58,7 @@ public async Task<RestInviteMetadata> CreateInviteAsync(int? maxAge = 3600, int?
public override string ToString() => Name;

//IGuildChannel
public Task<IGuildChannel> GetParentChannelAsync()
public Task<IChannelCategory> GetCategory()
{
//Always fails
throw new InvalidOperationException("Unable to return this entity's parent unless it was fetched through that object.");
Expand Down
@@ -0,0 +1,50 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Discord.Audio;
using Discord.Rest;
using Model = Discord.API.Channel;

namespace Discord.WebSocket
{
[DebuggerDisplay(@"{DebuggerDisplay,nq}")]
public class SocketChannelCategory : SocketGuildChannel, IChannelCategory
{
public override IReadOnlyCollection<SocketGuildUser> Users
=> Guild.Users.Where(x => x.VoiceChannel?.Id == Id).ToImmutableArray();

internal SocketChannelCategory(DiscordSocketClient discord, ulong id, SocketGuild guild)
: base(discord, id, guild)
{
}
internal new static SocketChannelCategory Create(SocketGuild guild, ClientState state, Model model)
{
var entity = new SocketChannelCategory(guild.Discord, model.Id, guild);
entity.Update(state, model);
return entity;
}

private string DebuggerDisplay => $"{Name} ({Id}, Category)";
internal new SocketChannelCategory Clone() => MemberwiseClone() as SocketChannelCategory;

// IGuildChannel
IAsyncEnumerable<IReadOnlyCollection<IGuildUser>> IGuildChannel.GetUsersAsync(CacheMode mode, RequestOptions options)
=> throw new NotSupportedException();
Task<IGuildUser> IGuildChannel.GetUserAsync(ulong id, CacheMode mode, RequestOptions options)
=> throw new NotSupportedException();
Task<IInviteMetadata> IGuildChannel.CreateInviteAsync(int? maxAge, int? maxUses, bool isTemporary, bool isUnique, RequestOptions options)
=> throw new NotSupportedException();
Task<IReadOnlyCollection<IInviteMetadata>> IGuildChannel.GetInvitesAsync(RequestOptions options)
=> throw new NotSupportedException();

//IChannel
IAsyncEnumerable<IReadOnlyCollection<IUser>> IChannel.GetUsersAsync(CacheMode mode, RequestOptions options)
=> throw new NotSupportedException();
Task<IUser> IChannel.GetUserAsync(ulong id, CacheMode mode, RequestOptions options)
=> throw new NotSupportedException();
}
}
Expand Up @@ -17,9 +17,9 @@ public class SocketGuildChannel : SocketChannel, IGuildChannel
public SocketGuild Guild { get; }
public string Name { get; private set; }
public int Position { get; private set; }
public ulong? ParentId { get; private set; }
public IGuildChannel ParentChannel => ParentId == null ? null : Guild.GetChannel(ParentId.Value);
public Task<IGuildChannel> GetParentChannelAsync() => Task.FromResult(ParentChannel);
public ulong? CategoryId { get; private set; }
public IChannelCategory Category => CategoryId == null ? null : Guild.GetChannel(CategoryId.Value) as IChannelCategory;
Task<IChannelCategory> IGuildChannel.GetCategory() => Task.FromResult(Category);

public IReadOnlyCollection<Overwrite> PermissionOverwrites => _overwrites;
public new virtual IReadOnlyCollection<SocketGuildUser> Users => ImmutableArray.Create<SocketGuildUser>();
Expand All @@ -38,7 +38,7 @@ internal static SocketGuildChannel Create(SocketGuild guild, ClientState state,
case ChannelType.Voice:
return SocketVoiceChannel.Create(guild, state, model);
case ChannelType.Category:
return SocketGuildChannelCategory.Create(guild, state, model);
return SocketChannelCategory.Create(guild, state, model);
default:
// TODO: Proper implementation for channel categories
return new SocketGuildChannel(guild.Discord, model.Id, guild);
Expand All @@ -48,7 +48,7 @@ internal override void Update(ClientState state, Model model)
{
Name = model.Name.Value;
Position = model.Position.Value;
ParentId = model.ParentId;
CategoryId = model.CategoryId;

var overwrites = model.PermissionOverwrites.Value;
var newOverwrites = ImmutableArray.CreateBuilder<Overwrite>(overwrites.Length);
Expand Down

0 comments on commit 5c4777d

Please sign in to comment.